POJ训练记录4
1694.An Old Stone Game
f[x]表示x为根的树至少需要的石头,把子树按f排序后贪心即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
#include<map> #include<cmath> #include<ctime> #include<queue> #include<cstdio> #include<vector> #include<bitset> #include<cstring> #include<iostream> #include<algorithm> #define ll long long using namespace std; ll read() { ll x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } int T,n,ans[205]; vector<int>e[205]; bool cmp(int a,int b) { return ans[a]>ans[b]; } void dp(int x) { if(!e[x].size())ans[x]=1; else ans[x]=0; for(int i=0;i<e[x].size();i++)dp(e[x][i]); sort(e[x].begin(),e[x].end(),cmp); for(int i=0;i<e[x].size();i++) ans[x]=max(ans[x],i+ans[e[x][i]]); } int main() { T=read(); while(T--) { n=read(); for(int i=0;i<n;i++) { int x=read(),y=read(),z; e[x].clear(); while(y--) { z=read(); e[x].push_back(z); } } dp(1); printf("%d\n",ans[1]); } return 0; } |
poj1738.An old Stone Game
参见discuss的神算法,据说是knuth提出的?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
#include<map> #include<cmath> #include<ctime> #include<queue> #include<cstdio> #include<vector> #include<bitset> #include<cstring> #include<iostream> #include<algorithm> #define inf 1000000000 #define ll long long using namespace std; ll read() { ll x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } int T,n,ans,last; int a[50005]; int pre[50005],nxt[50005]; void solve() { for(int i=last;i!=-1;i=nxt[i]) if(pre[i]!=-1&&a[pre[i]]<=a[nxt[i]]) { int t=a[pre[i]]+a[i],l=pre[pre[i]]; ans+=t;a[i]=t; nxt[l]=nxt[i];pre[nxt[i]]=l; for(int j=l;j!=-1;j=pre[j]) if(a[j]>t) { nxt[i]=nxt[j]; pre[i]=j; pre[nxt[j]]=i; nxt[j]=i; last=j; return; } } } int main() { while(scanf("%d",&n)) { if(n==0)return 0; a[0]=a[n+1]=inf; for(int i=1;i<=n;i++)a[i]=read(); for(int i=0;i<=n;i++)nxt[i]=i+1; for(int i=1;i<=n+1;i++)pre[i]=i-1; pre[0]=nxt[n+1]=-1; ans=last=0; for(int i=1;i<n;i++)solve(); printf("%d\n",ans); } return 0; } |
1737.Connected Graph
跪大爷
http://blog.csdn.net/PoPoQQQ/article/details/43525019
1 2 3 4 5 6 7 8 9 10 11 12 |
w=open("1.out","w"); f=[0]*60; C=[[0]*60 for i in range(60)] for i in range(0,51): C[i][0]=1; for j in range(1,i+1): C[i][j]=C[i-1][j]+C[i-1][j-1]; for i in range(1,51): f[i]=2**(i*(i-1)//2) for j in range(1,i): f[i]-=C[i-1][j-1]*(2**((i-j)*(i-j-1)/2))*f[j] w.write("\"%d\",\n" %f[i]) |
1742.Coins
二进制拆分+bitset竟然过不了…我被题解骗了
突然一想这不是以前做过的JoyOI的题目么,nm复杂度dp即可
dp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
#include<map> #include<cmath> #include<ctime> #include<queue> #include<cstdio> #include<vector> #include<bitset> #include<cstring> #include<iostream> #include<algorithm> #define inf 1000000000 #define ll long long using namespace std; ll read() { ll x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } int n,m,a[105],c[105]; int f[100005],g[100005]; int main() { while(scanf("%d%d",&n,&m)) { if(n==0&&m==0)return 0; for(int i=1;i<=n;i++)a[i]=read(); for(int i=1;i<=n;i++)c[i]=read(); f[0]=1; for(int i=1;i<=n;i++) { memset(g,0,sizeof(g)); for(int j=0;j<=m;j++) if(f[j]&&!f[j+a[i]]&&g[j]+1<=c[i]&&j+a[i]<=m) f[j+a[i]]=1,g[j+a[i]]=g[j]+1; } int ans=0; for(int i=1;i<=m;i++) if(f[i]) { ans++; f[i]=0; } printf("%d\n",ans); } return 0; } |
bitset
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
#include<map> #include<cmath> #include<ctime> #include<queue> #include<cstdio> #include<vector> #include<bitset> #include<cstring> #include<iostream> #include<algorithm> #define inf 1000000000 #define ll long long using namespace std; ll read() { ll x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } int n,m,a[105],c[105]; bitset<100005>mp; int main() { mp[0]=1; while(scanf("%d%d",&n,&m)) { if(n==0&&m==0)return 0; for(int i=1;i<=n;i++)a[i]=read(); for(int i=1;i<=n;i++)c[i]=read(); for(int i=1;i<=n;i++) { int t=c[i]; for(int j=1;;j=j+j) { if(min(t,j)*a[i]<=m&&min(t,j)<=c[i]) mp=mp|mp<<(min(t,j)*a[i]); else break; t-=j; } } int ans=0; for(int i=1;i<=m;i++) if(mp[i]) { ans++; mp[i]=0; } printf("%d\n",ans); } return 0; } |
3425.Customer support
傻逼阅读题
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
#include<map> #include<cmath> #include<ctime> #include<queue> #include<cstdio> #include<vector> #include<bitset> #include<cstring> #include<iostream> #include<algorithm> #define ll long long using namespace std; ll read() { ll x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } bool mark[1000005]; int n,ans; int main() { n=read(); for(int i=1;i<=n;i++) { int a=read(),b=read(),c=read(); if(!b)ans+=10; else { if(mark[a])ans+=10; mark[a]=1; ans+=20; if(c)ans+=20; } } printf("%d\n",ans); return 0; } |
1673.EXOCENTER OF A TRIANGLE
求三角形垂心
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
#include<map> #include<ctime> #include<queue> #include<cstdio> #include<vector> #include<bitset> #include<cstring> #include<iostream> #include<algorithm> #define eps 1e-10 #define ll long long using namespace std; ll read() { ll x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } struct P { double x,y; friend P operator-(P a,P b){ return (P){a.x-b.x,a.y-b.y}; } friend P operator+(P a,P b){ return (P){a.x+b.x,a.y+b.y}; } friend double operator*(P a,P b){ return a.x*b.y-a.y*b.x; } friend P operator*(P a,double b){ return (P){a.x*b,a.y*b}; } }A,B,C; P normal(P a){ return (P){-a.y,a.x}; } P solve(P p,P v,P q,P w){ P u=p-q; return p+v*((w*u)/(v*w)); } int main() { int T=read(); while(T--) { scanf("%lf%lf%lf%lf%lf%lf",&A.x,&A.y,&B.x,&B.y,&C.x,&C.y); P o=solve(A,normal(B-C),B,normal(A-C)); printf("%.4f %.4f\n",o.x+eps,o.y+eps); } return 0; } |
Subscribe