【刷题记录】BZOJ-USACO

接下来要滚去bzoj刷usaco的题目辣=v=在博客记录一下刷题情况,以及存一存代码咯。加油!                                                                                                                                      

1.【bzoj1597】[Usaco2008 Mar]土地购买

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cstring>
 4 using namespace std;
 5 const int N=50050;
 6 int n,cnt,q[N];
 7 long long x[N],y[N],f[N];
 8 struct node{long long x,y;}a[N];
 9 bool cmp(node a,node b){return a.x==b.x?a.y<b.y:a.x<b.x;}
10 long long read()
11 {
12     long long x=0,f=1;char c=getchar();
13     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
14     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
15     return x*f;
16 }
17 double slope(int k,int j){return (double)(f[j]-f[k])/(y[k+1]-y[j+1]);}
18 int main()
19 {
20     n=read();
21     for(int i=1;i<=n;i++)
22         a[i].x=read(),a[i].y=read();
23     sort(a+1,a+n+1,cmp);
24     for(int i=1;i<=n;i++)
25     {
26         while(cnt&&a[i].y>=y[cnt])cnt--;
27         x[++cnt]=a[i].x;y[cnt]=a[i].y;
28     }
29     int l=0,r=0;
30     for(int i=1;i<=n;i++)
31     {
32         while(l<r&&slope(q[l],q[l+1])<x[i])l++;
33         int t=q[l];
34         f[i]=f[t]+y[t+1]*x[i];
35         while(l<r&&slope(q[r-1],q[r])>slope(q[r],i))r--;
36         q[++r]=i;
37     }
38     printf("%lld",f[cnt]);
39     return 0;
40 }
斜率优化dp

2.【bzoj1607】[Usaco2008 Dec]Patting Heads 轻拍牛头

 1 #include<cstdio>
 2 #include<algorithm> 
 3 #include<cstring>
 4 const int N=1e5+5;
 5 using namespace std;
 6 int n,mx,a[N],c[N*10],ans[N*10];
 7 int read()
 8 {
 9     int x=0,f=1;char c=getchar();
10     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
11     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
12     return x*f;
13 }
14 int main()
15 {
16     n=read();
17     for(int i=1;i<=n;i++)
18         a[i]=read(),c[a[i]]++,mx=max(mx,a[i]);
19     for(int i=1;i<=mx;i++)
20         for(int j=i;j<=mx;j+=i)
21             ans[j]+=c[i]; 
22     for(int i=1;i<=n;i++)
23         printf("%d\n",ans[a[i]]-1);
24     return 0;
25 }
筛法

3.【bzoj1610】[Usaco2008 Feb]Line连线游戏

 1 #include<cstdio>
 2 #include<algorithm> 
 3 #include<cstring>
 4 #include<cmath>
 5 using namespace std;
 6 const int N=1e6+5;
 7 int n,ans,cnt,x[1005],y[1005];
 8 double q[N];
 9 int read()
10 {
11     int x=0,f=1;char c=getchar();
12     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
13     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
14     return x*f;
15 }
16 int main()
17 {
18     n=read();
19     for(int i=1;i<=n;i++)
20         x[i]=read(),y[i]=read();
21     for(int i=1;i<=n;i++)
22         for(int j=i+1;j<=n;j++)
23         {
24             if(x[i]==x[j])ans=1;
25             else q[++cnt]=1.0*(y[j]-y[i])/(x[j]-x[i]);
26         }
27     sort(q+1,q+cnt+1);
28     if(cnt>=1)ans++;
29     for(int i=2;i<=cnt;i++)
30         if(fabs(q[i]-q[i-1])>1e-8)ans++;
31     printf("%d",ans);
32     return 0;
33 }
计算几何

4.【bzoj1609】[Usaco2008 Feb]Eating Together麻烦的聚餐

 1 #include<cstdio>
 2 #include<algorithm> 
 3 #include<cstring>
 4 #define ll long long
 5 using namespace std;
 6 const int N=3e4+5;
 7 const int inf=0x3f3f3f3f;
 8 int n,a[N],dp[N][4],ans=inf;
 9 int read()
10 {
11     int x=0,f=1;char c=getchar();
12     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
13     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
14     return x*f;
15 }
16 int main()
17 {
18     n=read();
19     for(int i=1;i<=n;i++)a[i]=read();
20     memset(dp,0x3f,sizeof(dp));
21     dp[0][1]=dp[0][2]=dp[0][3]=0;
22     for(int i=1;i<=n;i++)
23     {
24         for(int j=1;j<=3;j++)
25         {
26             for(int k=1;k<=j;k++)
27                 dp[i][j]=min(dp[i][j],dp[i-1][k]);
28             if(j!=a[i])dp[i][j]++;
29         }
30     }
31     for(int i=1;i<=3;i++)ans=min(ans,dp[n][i]);
32     memset(dp,0x3f,sizeof(dp));
33     dp[n+1][1]=dp[n+1][2]=dp[n+1][3]=0;
34     for(int i=n;i>=1;i--)
35     {
36         for(int j=1;j<=3;j++)
37         {
38             for(int k=1;k<=j;k++)
39                 dp[i][j]=min(dp[i][j],dp[i+1][k]);
40             if(j!=a[i])dp[i][j]++;
41         }
42     }
43     for(int i=1;i<=3;i++)ans=min(ans,dp[1][i]);
44     printf("%d",ans);
45     return 0;
46 }
普通dp

5.【bzoj1669】[Usaco2007 Jan]Balanced Lineup排队

 1 #include<cstdio>
 2 #include<algorithm> 
 3 #include<cstring>
 4 #include<cmath>
 5 #define ll long long
 6 using namespace std;
 7 const int N=5e4+5;
 8 const int inf=0x3f3f3f3f;
 9 int n,q,l,r,mx[N][20],mn[N][20];
10 int read()
11 {
12     int x=0,f=1;char c=getchar();
13     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
14     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
15     return x*f;
16 }
17 int rmq(int l,int r)
18 {
19     int a=log(r-l+1)/log(2);
20     int mxx=max(mx[l][a],mx[r-(1<<a)+1][a]);
21     int mnn=min(mn[l][a],mn[r-(1<<a)+1][a]);
22     return mxx-mnn;
23 }
24 int main()
25 {
26     n=read();q=read();
27     for(int i=1;i<=n;i++)mx[i][0]=mn[i][0]=read();
28     int a=log(n)/log(2);
29     for(int i=1;i<=a;i++)
30         for(int j=1;j<=n-(1<<(i-1));j++)
31         {
32             mx[j][i]=mx[j][i-1];
33             mx[j][i]=max(mx[j][i],mx[j+(1<<(i-1))][i-1]);
34             mn[j][i]=mn[j][i-1];
35             mn[j][i]=min(mn[j][i],mn[j+(1<<(i-1))][i-1]);
36         }
37     while(q--)
38     {
39         l=read();r=read();
40         printf("%d\n",rmq(l,r));
41     }
42     return 0;
43 }
44 
RMQ-ST

 6.【bzoj1606】[Usaco2008 Dec]Hay For Sale 购买干草

 1 #include<cstdio>
 2 #include<algorithm> 
 3 #include<cstring>
 4 #include<cmath>
 5 #define ll long long
 6 using namespace std;
 7 const int inf=0x3f3f3f3f;
 8 int c,h,v[5010],dp[50010]; 
 9 int read()
10 {
11     int x=0,f=1;char c=getchar();
12     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
13     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
14     return x*f;
15 }
16 int main()
17 {
18     c=read();h=read();
19     for(int i=1;i<=h;i++)v[i]=read();
20     for(int i=1;i<=h;i++)
21         for(int j=c;j>=v[i];j--)
22             dp[j]=max(dp[j],dp[j-v[i]]+v[i]);
23     printf("%d",dp[c]);
24     return 0;
25 }
背包dp

7.【bzoj1625】 [Usaco2007 Dec]宝石手镯

 1 #include<cstdio>
 2 #include<algorithm> 
 3 #include<cstring>
 4 #include<cmath>
 5 #define ll long long
 6 using namespace std;
 7 const int inf=0x3f3f3f3f;
 8 int n,m,v[3510],d[3510],dp[13010]; 
 9 int read()
10 {
11     int x=0,f=1;char c=getchar();
12     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
13     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
14     return x*f;
15 }
16 int main()
17 {
18     n=read();m=read();
19     for(int i=1;i<=n;i++)v[i]=read(),d[i]=read();
20     for(int i=1;i<=n;i++)
21         for(int j=m;j>=v[i];j--)
22             dp[j]=max(dp[j],dp[j-v[i]]+d[i]);
23     printf("%d",dp[m]);
24     return 0;
25 }
背包dp

8.【bzoj1617】[Usaco2008 Mar]River Crossing渡河问题

 1 #include<cstdio>
 2 #include<algorithm> 
 3 #include<cstring>
 4 #include<cmath>
 5 #define ll long long
 6 using namespace std;
 7 const int inf=0x3f3f3f3f;
 8 int n,m[2510],dp[2510]; 
 9 int read()
10 {
11     int x=0,f=1;char c=getchar();
12     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
13     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
14     return x*f;
15 }
16 int main()
17 {
18     memset(dp,0x3f,sizeof(dp));
19     dp[0]=0;
20     n=read();m[0]=read();
21     for(int i=1;i<=n;i++)
22         m[i]=read()+m[i-1];
23     for(int i=1;i<=n;i++)
24         for(int j=0;j<=i;j++)
25             dp[i]=min(dp[i],dp[j]+m[i-j]+m[0]);
26     printf("%d",dp[n]-m[0]);
27     return 0;
28 }
29 
普通dp

9.【bzoj1612】[Usaco2008 Jan]Cow Contest奶牛的比赛

 1 #include<cstdio>
 2 #include<algorithm> 
 3 #include<cstring>
 4 #include<cmath>
 5 #define ll long long
 6 using namespace std;
 7 const int inf=0x3f3f3f3f;
 8 int n,m,a,b,ans,l[105],r[105];
 9 bool map[105][105];
10 int read()
11 {
12     int x=0,f=1;char c=getchar();
13     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
14     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
15     return x*f;
16 }
17 int main()
18 {
19     n=read();m=read();
20     while(m--)
21     {
22         a=read();b=read();
23         map[a][b]=true;
24         r[a]++;l[b]++;
25     }
26     for(int k=1;k<=n;k++)
27         for(int i=1;i<=n;i++)
28             for(int j=1;j<=n;j++)
29                 if(map[i][k]&&map[k][j]&&!map[i][j])
30                     map[i][j]=true,r[i]++,l[j]++;
31     for(int i=1;i<=n;i++)
32         if(l[i]+r[i]==n-1)ans++;
33     printf("%d",ans);
34     return 0;
35 }
Floyd

10.【bzoj1614】[Usaco2007 Jan]Telephone Lines架设电话线

 1 #include<cstdio>
 2 #include<algorithm> 
 3 #include<cstring>
 4 #include<cmath>
 5 #define ll long long
 6 using namespace std;
 7 const int inf=0x3f3f3f3f;
 8 int n,m,k,u,v,w,cnt,ans=-1;
 9 int first[1010],dis[1010],q[1010];
10 bool f[1010];
11 struct edge{int to,next,w;}e[20010];
12 int read()
13 {
14     int x=0,f=1;char c=getchar();
15     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
16     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
17     return x*f;
18 }
19 void insert(int u,int v,int w)
20 {
21     cnt++;e[cnt].next=first[u];first[u]=cnt;
22     e[cnt].to=v;e[cnt].w=w;
23 }
24 bool check(int x)
25 {
26     memset(dis,0x3f,sizeof(dis));
27     int h=0,t=1;
28     dis[1]=0;q[0]=1;f[1]=true;
29     while(h!=t)
30     {
31         u=q[h++];f[u]=false;if(h>1000)h=0;
32         for(int i=first[u];i;i=e[i].next)
33         {
34             v=e[i].to;
35             if(e[i].w>x)w=1;
36             else w=0;
37             if(dis[v]>dis[u]+w)
38             {
39                 dis[v]=dis[u]+w;
40                 if(!f[v])
41                 {
42                     f[v]=true;q[t++]=v;
43                     if(t>1000)t=0;
44                 }
45             }
46         }
47     }
48     return dis[n]<=k;
49 }
50 int main()
51 {
52     n=read();m=read();k=read();
53     while(m--)
54     {
55         u=read();v=read();w=read();
56         insert(u,v,w);insert(v,u,w);
57     }
58     int l=0,r=1000000;
59     while(l<=r)
60     {
61         int mid=(l+r)>>1;
62         if(check(mid))ans=mid,r=mid-1;
63         else l=mid+1;
64     }
65     printf("%d",ans);
66     return 0;
二分+SPFA

11.【bzoj1601】[Usaco2008 Oct]灌水

 1 #include<cstdio>
 2 #include<algorithm> 
 3 #include<cstring>
 4 #define ll long long
 5 using namespace std;
 6 const int inf=0x3f3f3f3f;
 7 const int N=1e5+5;
 8 struct edge{int a,b,w;}e[N];
 9 int n,w,cnt,ans,f[305];
10 int read()
11 {
12     int x=0,f=1;char c=getchar();
13     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
14     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
15     return x*f;
16 }
17 void ins(int a,int b,int w){e[++cnt].a=a;e[cnt].b=b;e[cnt].w=w;}
18 int find(int t){return f[t]==t?t:f[t]=find(f[t]);}
19 bool cmp(edge a,edge b){return a.w<b.w;}
20 int main()
21 {
22     n=read();
23     for(int i=1;i<=n;i++)
24         ins(0,i,read()),f[i]=i;
25     for(int i=1;i<=n;i++)
26         for(int j=1;j<=n;j++)
27             {ins(i,j,read());if(i==j)cnt--;}
28     sort(e+1,e+cnt+1,cmp);
29     for(int i=1;i<=cnt;i++)
30     {
31         int x=find(e[i].a),y=find(e[i].b);
32         if(x==y)continue;
33         ans+=e[i].w;f[x]=y; 
34     }
35     printf("%d",ans);
36     return 0;
37 }
最小生成树

12.【bzoj1602】[Usaco2008 Oct]牧场行走

 1 #include<cstdio>
 2 #include<algorithm> 
 3 #include<cstring>
 4 #define ll long long
 5 using namespace std;
 6 const int inf=0x3f3f3f3f;
 7 const int N=1e3+5;
 8 struct edge{int next,to,w;}e[N*2];
 9 int n,cnt,u,v,w,q,first[N],deep[N],di[N],x[N][20];
10 int read()
11 {
12     int x=0,f=1;char c=getchar();
13     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
14     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
15     return x*f;
16 }
17 void ins(int u,int v,int w)
18 {
19     cnt++;e[cnt].next=first[u];first[u]=cnt;
20     e[cnt].to=v;e[cnt].w=w;
21 }
22 void dfs(int k)
23 {
24     for(int i=1;(1<<i)<=deep[k];i++)
25         x[k][i]=x[x[k][i-1]][i-1];
26     for(int i=first[k];i;i=e[i].next) 
27     {
28         if(deep[e[i].to])continue;
29         x[e[i].to][0]=k;
30         deep[e[i].to]=deep[k]+1;
31         di[e[i].to]=di[k]+e[i].w;
32         dfs(e[i].to);
33     }
34 }
35 int lca(int ri,int rj)
36 {
37     if(deep[ri]<deep[rj])swap(ri,rj);
38     int d=deep[ri]-deep[rj]; 
39     for(int i=0;(1<<i)<=d;i++)
40         if((1<<i)&d)ri=x[ri][i];
41     if(ri==rj)return ri;
42     for(int i=16;i>=0;i--)
43         if((1<<i)<=deep[rj]&&x[ri][i]!=x[rj][i])
44             ri=x[ri][i],rj=x[rj][i];
45     return x[ri][0];
46 }
47 int main()
48 {
49     n=read();q=read();
50     for(int i=1;i<n;i++)
51     {
52         u=read();v=read();w=read();
53         ins(u,v,w);ins(v,u,w);
54     }
55     deep[1]=1;dfs(1);
56     while(q--)
57     {
58         u=read();v=read();
59         printf("%d\n",di[u]+di[v]-2*di[lca(u,v)]);
60     }
61     return 0;
62 }
LCA

13.【bzoj1613】[Usaco2007 Jan]Running贝茜的晨练计划

 1 #include<cstdio>
 2 #include<algorithm> 
 3 #include<cstring>
 4 #define ll long long
 5 using namespace std;
 6 const int inf=0x3f3f3f3f;
 7 const int N=1e4+5;
 8 int n,m,v,f[N][505];
 9 int read()
10 {
11     int x=0,f=1;char c=getchar();
12     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
13     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
14     return x*f;
15 }
16 int main()
17 {
18     n=read();m=read();
19     for(int i=1;i<=n;i++)
20     {
21         v=read();
22         for(int j=1;j<=m;j++)
23         {
24             f[i][j]=f[i-1][j-1]+v;
25             if(i+j<=n)f[i+j][0]=max(f[i+j][0],f[i][j]);
26         }
27         f[i][0]=max(f[i-1][0],f[i][0]);
28     }
29     printf("%d",f[n][0]);
30     return 0;
31 }
普通dp

14.【bzoj1230】[Usaco2008 Nov]lites 开关灯

 1 #include<cstdio>
 2 #include<algorithm> 
 3 #include<cstring>
 4 #define ll long long
 5 #define l(x) x<<1
 6 #define r(x) x<<1|1
 7 using namespace std;
 8 const int inf=0x3f3f3f3f;
 9 const int N=1e5+5;
10 int n,m,p,L,R,ans,num[N*4],rev[N*4];
11 int read()
12 {
13     int x=0,f=1;char c=getchar();
14     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
15     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
16     return x*f;
17 }
18 void up(int x)
19 {
20     num[x]=num[l(x)]+num[r(x)];
21 }
22 void crev(int x,int l,int r)
23 {
24     num[x]=(r-l+1)-num[x];
25     rev[x]^=1;
26 }
27 void start(int x,int l,int r)
28 {
29     int mid=(l+r)>>1;
30     if(rev[x])
31     {
32         crev(l(x),l,mid);
33         crev(r(x),mid+1,r);
34         rev[x]=0;
35     }
36 }
37 void change(int x,int l,int r)
38 {
39     if(l!=r)start(x,l,r);
40     if(L<=l&&R>=r){crev(x,l,r);return;}
41     int mid=(l+r)>>1;
42     if(L<=mid)change(l(x),l,mid);
43     if(R>mid)change(r(x),mid+1,r);
44     if(l!=r)up(x);
45 }
46 void ask(int x,int l,int r)
47 {
48     if(l!=r)start(x,l,r);
49     if(L<=l&&R>=r)
50     {
51         ans+=num[x];
52         return;
53     }
54     int mid=(l+r)>>1;
55     if(L<=mid)ask(l(x),l,mid);
56     if(R>mid)ask(r(x),mid+1,r);
57 }
58 int main()
59 {
60     n=read();m=read();
61     while(m--)
62     {
63         p=read();L=read();R=read();
64         if(p==0)change(1,1,n);
65         else
66         {
67             ans=0;
68             ask(1,1,n);
69             printf("%d\n",ans);
70         }
71     }
72     return 0;
73 }
74 
线段树

15.【bzoj1600】[Usaco2008 Oct]建造栅栏

 1 #include<cstdio>
 2 #include<algorithm> 
 3 #include<cstring>
 4 #define ll long long
 5 using namespace std;
 6 const int inf=0x3f3f3f3f;
 7 const int N=2505;
 8 int n,mx,f[N][5];
 9 int read()
10 {
11     int x=0,f=1;char c=getchar();
12     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
13     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
14     return x*f;
15 }
16 int main()
17 {
18     n=read();
19     mx=(n+1)/2-1;f[0][0]=1;
20     for(int i=1;i<=4;i++)
21         for(int j=1;j<=n;j++)
22             for(int k=1;k<=min(j,mx);k++)
23                 f[j][i]+=f[j-k][i-1];
24     printf("%d",f[n][4]);
25     return 0;
26 }
普通dp

16.【bzoj1603】[Usaco2008 Oct]打谷机

 1 #include<cstdio>
 2 #include<algorithm> 
 3 #include<cstring>
 4 #define ll long long
 5 using namespace std;
 6 const int inf=0x3f3f3f3f;
 7 const int N=1e3+5;
 8 int n,u,v,w,cnt,r[N],first[N];
 9 bool f[N];
10 struct edge{int to,next,w;}e[N*2];
11 int read()
12 {
13     int x=0,f=1;char c=getchar();
14     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
15     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
16     return x*f;
17 }
18 void ins(int u,int v,int w)
19 {
20     cnt++;e[cnt].next=first[u];first[u]=cnt;
21     e[cnt].to=v;e[cnt].w=w;
22 }
23 void dfs(int x)
24 {
25     f[x]=true;
26     for(int i=first[x];i;i=e[i].next)
27     {
28         if(f[e[i].to])continue;
29         r[e[i].to]=r[x]^e[i].w;
30         dfs(e[i].to);
31     }
32 }
33 int main()
34 {
35     n=read();
36     for(int i=1;i<n;i++)
37     {
38         u=read();v=read();w=read();
39         ins(u,v,w);ins(v,u,w);
40     }
41     dfs(1);
42     printf("%d",r[n]);
43     return 0;
44 }
45 
DFS

17.【bzoj1599】[Usaco2008 Oct]笨重的石子

 1 #include<cstdio>
 2 #include<algorithm> 
 3 #include<cstring>
 4 #define ll long long
 5 using namespace std;
 6 const int inf=0x3f3f3f3f;
 7 int s1,s2,s3,s[90],ans,mn; 
 8 int read()
 9 {
10     int x=0,f=1;char c=getchar();
11     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
12     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
13     return x*f;
14 }
15 int main()
16 {
17     s1=read();s2=read();s3=read();
18     for(int i=1;i<=s1;i++)
19         for(int j=1;j<=s2;j++)
20             for(int k=1;k<=s3;k++)
21                 s[i+j+k]++;
22     for(int i=1;i<=s1+s2+s3;i++)
23         if(s[i]>mn)mn=s[i],ans=i;
24     printf("%d",ans);
25     return 0;
26 }
枚举

18.【bzoj1692】[Usaco2007 Dec]队列变换

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #define long long LL
 5 using namespace std;
 6 const int N=60010;
 7 int n,m,l,r,cnt,t1[N],t2[N],sa[N],c[N],rk[N];
 8 char s[N];
 9 void build()
10 {
11     int *x=t1,*y=t2;
12     for(int i=0;i<n;i++)c[x[i]]++;
13     for(int i=1;i<m;i++)c[i]+=c[i-1];
14     for(int i=n-1;i>=0;i--)sa[--c[x[i]]]=i;
15     for(int k=1;k<=n;k<<=1)
16     {
17         int p=0;
18         for(int i=n-k;i<n;i++)y[p++]=i;
19         for(int i=0;i<n;i++)if(sa[i]>=k)y[p++]=sa[i]-k;
20         memset(c,0,sizeof(c));
21         for(int i=0;i<n;i++)c[x[y[i]]]++;
22         for(int i=1;i<m;i++)c[i]+=c[i-1];
23         for(int i=n-1;i>=0;i--)sa[--c[x[y[i]]]]=y[i];
24         swap(x,y);
25         p=1;x[sa[0]]=0;
26         for(int i=1;i<n;i++)
27             x[sa[i]]=y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+k]==y[sa[i]+k]?p-1:p++;
28         if(p>=n)break;
29         m=p;
30     }
31     for(int i=0;i<n;i++)rk[i]=x[i];
32 }
33 int main()
34 {
35     scanf("%d",&n);
36     for(int i=0;i<n;i++)
37     {
38         s[i]=getchar();
39         while(s[i]<'A'||s[i]>'Z')s[i]=getchar();
40     }
41     for(int i=0;i<n;i++)t1[i]=t1[n*2-i]=s[i]-'A'+1;
42     n=(n+1)<<1;m=27;build();
43     n=(n>>1)-1;l=0;r=n-1;
44     while(l<=r)
45     {
46         if(rk[n*2-r]<rk[l])printf("%c",s[r--]);
47         else printf("%c",s[l++]);
48         cnt++;
49         if(cnt==80)printf("\n"),cnt=0;
50     }
51     return 0;
52 }
53 
后缀数组+贪心

19.【bzoj1666】[Usaco2006 Oct]Another Cow Number Game 奶牛的数字游戏

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #define LL long long
 5 using namespace std;
 6 LL n,ans;
 7 int read()
 8 {
 9     int x=0,f=1;char c=getchar();
10     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
11     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
12     return x*f;
13 }
14 int main()
15 {
16     scanf("%lld",&n);
17     while(n!=1)
18     {
19         if(n%2)n=n*3+1;
20         else n/=2;
21         ans++;
22     }
23     printf("%lld",ans);
24     return 0;
25 }
模拟

20.【bzoj1717】[Usaco2006 Dec]Milk Patterns 产奶的模式

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #define LL long long
 5 using namespace std;
 6 const int N=20010;
 7 int cnt=1,n,m,k,ans;
 8 int que[N],s[N],rk[N],hei[N],t1[N],t2[N],c[N],sa[N];
 9 struct node{int v,pos;}q[N];
10 int read()
11 {
12     int x=0,f=1;char c=getchar();
13     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
14     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
15     return x*f;
16 }
17 bool cmp(node a,node b){return a.v<b.v;}
18 void build()
19 {
20     int *x=t1,*y=t2;
21     for(int i=0;i<n;i++)c[x[i]]++;
22     for(int i=1;i<m;i++)c[i]+=c[i-1];
23     for(int i=n-1;i>=0;i--)sa[--c[x[i]]]=i;
24     for(int k=1;k<=n;k<<=1)
25     {
26         int p=0;
27         for(int i=n-k;i<n;i++)y[p++]=i;
28         for(int i=0;i<n;i++)if(sa[i]>=k)y[p++]=sa[i]-k;
29         memset(c,0,sizeof(c));
30         for(int i=0;i<n;i++)c[x[y[i]]]++;
31         for(int i=1;i<m;i++)c[i]+=c[i-1];
32         for(int i=n-1;i>=0;i--)sa[--c[x[y[i]]]]=y[i];
33         swap(x,y);
34         p=1;x[sa[0]]=0;
35         for(int i=1;i<n;i++)
36             x[sa[i]]=y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+k]==y[sa[i]+k]?p-1:p++;
37         if(p>=n)break;
38         m=p;
39     }
40 }
41 void get_height()
42 {
43     int k=0;
44     for(int i=0;i<n;i++)rk[sa[i]]=i;
45     for(int i=0;i<n;i++)
46     {
47         if(k)k--;
48         int j=sa[rk[i]-1];
49         while(s[i+k]==s[j+k])k++;
50         hei[rk[i]]=k;
51     }
52 }
53 int main()
54 {
55     n=read();k=read();
56     for(int i=0;i<n;i++)
57         q[i].pos=i,q[i].v=read();
58     sort(q,q+n,cmp);
59     t1[q[0].pos]=1;
60     for(int i=1;i<n;i++)
61     {
62         if(q[i].v!=q[i-1].v)t1[q[i].pos]=++cnt;
63         else t1[q[i].pos]=cnt;
64     }
65     for(int i=0;i<n;i++)s[i]=t1[i];
66     n++;m=cnt+1;build();
67     get_height();
68     int h=1,t=0;k--;
69     for(int i=1;i<n;i++)
70     {
71         while(h<=t&&que[h]<=i-k)h++;
72         while(h<=t&&hei[i]<=hei[que[t]])t--;
73         que[++t]=i;
74         if(i>=k)ans=max(ans,hei[que[h]]);
75     }
76     printf("%d",ans);
77     return 0;
78 }
79 
后缀数组

21.【bzoj1724】[Usaco2006 Nov]Fence Repair 切割木板

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #include<queue>
 5 #define LL long long
 6 using namespace std;
 7 const int N=20010;
 8 int n;
 9 LL sum,ans;
10 priority_queue<LL,vector<LL>,greater<LL> >q;
11 LL read()
12 {
13     LL x=0,f=1;char c=getchar();
14     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
15     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
16     return x*f;
17 }
18 int main()
19 {
20     n=read();
21     for(int i=1;i<=n;i++)q.push(read());
22     for(int i=1;i<n;i++)
23     {
24         sum=q.top();q.pop();
25         sum+=q.top();q.pop();
26         ans+=sum;q.push(sum);
27     }
28     printf("%lld",ans);
29     return 0;
30 }
31 
优先队列

22.【bzoj1572】[Usaco2009 Open]工作安排Job

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #include<queue>
 5 #define LL long long
 6 using namespace std;
 7 const int N=100010;
 8 int n,cnt;
 9 LL ans,sum;
10 struct node{int d;LL p;}a[N];
11 priority_queue<LL,vector<LL>,greater<LL> >q;
12 LL read()
13 {
14     LL x=0,f=1;char c=getchar();
15     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
16     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
17     return x*f;
18 }
19 bool cmp(node a,node b){return a.d<b.d;}
20 int main()
21 {
22     n=read();
23     for(int i=1;i<=n;i++)a[i].d=read(),a[i].p=read();
24     sort(a+1,a+n+1,cmp);
25     for(int i=1;i<=n;i++)
26     {
27         if(cnt<a[i].d)
28         {
29             ans+=a[i].p;
30             q.push(a[i].p);
31             cnt++;
32         }
33         else
34         {
35             sum=q.top();
36             if(sum>=a[i].p)continue;
37             q.pop();
38             ans=ans-sum+a[i].p;
39             q.push(a[i].p);
40         }
41     }
42     printf("%lld",ans);
43     return 0;
44 }
45 
优先队列

23.【bzoj1726】[Usaco2006 Nov]Roadblocks第二短路

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #include<queue>
 5 #define LL long long
 6 using namespace std;
 7 const int inf=0x3f3f3f3f;
 8 int n,m,cnt,a,b,d,s,sum,ans=inf;
 9 int first[5010],q[5010],d1[5010],d2[5010];
10 bool f[5010];
11 struct edge{int from,to,next,w;}e[200010];
12 int read()
13 {
14     int x=0,f=1;char c=getchar();
15     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
16     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
17     return x*f;
18 }
19 void ins(int u,int v,int w)
20 {
21     e[++cnt].to=v;e[cnt].from=u;e[cnt].w=w;
22     e[cnt].next=first[u];first[u]=cnt;
23 }
24 void spfa(int s,int d[])
25 {
26     int h=0,t=1;
27     memset(q,0,sizeof(q));
28     memset(f,0,sizeof(f));
29     for(int i=0;i<=n;i++)d[i]=inf;
30     q[h]=s;f[s]=true;d[s]=0;
31     while(h!=t)
32     {
33         int u=q[h++];f[u]=false;if(h>5000)h=0;
34         for(int i=first[u];i;i=e[i].next)
35         {
36             int v=e[i].to;
37             if(d[u]+e[i].w<d[v])
38             {
39                 d[v]=d[u]+e[i].w;
40                 if(!f[v])
41                 {
42                     if(d[v]<d[q[h]]){h--;if(h<0)h=5000;q[h]=v;}
43                     f[v]=true;q[t++]=v;
44                     if(t>5000)t=0;
45                 }
46             }
47         }
48     }
49 }
50 int main()
51 {
52     n=read();m=read();
53     for(int i=1;i<=m;i++)
54     {
55         a=read();b=read();d=read();
56         ins(a,b,d);ins(b,a,d);
57     }
58     spfa(1,d1);spfa(n,d2);
59     sum=d1[n];
60     for(int i=1;i<=m;i++)
61     {
62         s=e[i<<1].w+d1[e[i<<1].from]+d2[e[i<<1].to];
63         if(s<ans&&s>sum)ans=s;
64         s=e[i<<1].w+d2[e[i<<1].from]+d1[e[i<<1].to];
65         if(s<ans&&s>sum)ans=s;
66     }
67     printf("%d",ans);
68     return 0;
69 }
70 
SPFA

24.【bzoj1579】[Usaco2009 Feb]Revamping Trails 道路升级

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #include<queue>
 5 #define LL long long
 6 using namespace std;
 7 const int N=10010;
 8 const LL inf=1e15;
 9 int n,m,k,cnt,a,b,w,first[N];
10 LL dis[N][25];
11 struct edge{int to,next;LL w;}e[100010];
12 struct node
13 {
14     int p,k;LL w;
15     bool operator < (const node& t)const{return w>t.w;} 
16 };
17 priority_queue<node>q;
18 int read()
19 {
20     int x=0,f=1;char c=getchar();
21     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
22     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
23     return x*f;
24 }
25 void ins(int u,int v,LL w)
26 {
27     e[++cnt].to=v;e[cnt].w=w;
28     e[cnt].next=first[u];first[u]=cnt;
29 }
30 void dijkstra()
31 {
32     for(int i=2;i<=n;i++)
33         for(int j=0;j<=k;j++)
34             dis[i][j]=inf;
35     q.push((node){1,0,0});
36     while(!q.empty())
37     {
38         node t=q.top();q.pop();
39         int u=t.p,kk=t.k;
40         if(dis[u][kk]!=t.w)continue;
41         if(u==n){dis[n][k]=t.w;break;}
42         for(int i=first[u];i;i=e[i].next)
43         {
44             int v=e[i].to;
45             if(dis[v][kk]>t.w+e[i].w)
46             {
47                 dis[v][kk]=t.w+e[i].w;
48                 q.push((node){v,kk,dis[v][kk]});
49             }
50             if(kk<k&&dis[v][kk+1]>t.w)
51             {
52                 dis[v][kk+1]=t.w;
53                 q.push((node){v,kk+1,dis[v][kk+1]});
54             }
55         }
56     }
57 }
58 int main()
59 {
60     n=read();m=read();k=read();
61     for(int i=1;i<=m;i++)
62     {
63         a=read();b=read();w=read();
64         ins(a,b,w);ins(b,a,w);
65     }
66     dijkstra();
67     printf("%lld",dis[n][k]);
68     return 0;
69 }
分层图+Dijkstra

25.【bzoj1711】[Usaco2007 Open]Dining吃饭

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #define LL long long
 5 using namespace std;
 6 const int N=405;
 7 int n,f,d,t,fi,di,ans,cnt=1,S,T;
 8 int cur[N],first[N],dis[N],q[1005];
 9 struct edge{int to,next,flow;}e[100010];
10 int read()
11 {
12     int x=0,f=1;char c=getchar();
13     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
14     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
15     return x*f;
16 }
17 void ins(int u,int v,int w)
18 {
19     e[++cnt].to=v;e[cnt].flow=w;
20     e[cnt].next=first[u];first[u]=cnt;
21 }
22 void init()
23 {
24     n=read();f=read();d=read();
25     S=0;T=f+n+n+d+1;
26     for(int i=1;i<=n;i++)
27         ins(f+i,f+n+i,1),ins(f+n+i,f+i,0);
28     for(int i=1;i<=f;i++)
29         ins(S,i,1),ins(i,S,0);
30     for(int i=1;i<=d;i++)
31         ins(f+n+n+i,T,1),ins(T,f+n+n+i,0);
32     for(int i=1;i<=n;i++)
33     {
34         fi=read();di=read();
35         while(fi--)
36         {
37             t=read();
38             ins(t,f+i,1),ins(f+i,t,0);
39         }
40         while(di--)
41         {
42             t=read();
43             ins(f+n+i,f+n+n+t,1),ins(f+n+n+t,f+n+i,0);
44         }
45     }
46 }
47 bool bfs()
48 {
49     memset(dis,-1,sizeof(dis));
50     int head=0,tail=1;
51     q[head]=S;dis[S]=0;
52     while(head!=tail)
53     {
54         int u=q[head++];if(head>1000)head=0;
55         for(int i=first[u];i;i=e[i].next)
56         {
57             int v=e[i].to;
58             if(dis[v]!=-1||!e[i].flow)continue;
59             dis[v]=dis[u]+1;
60             q[tail++]=v;if(tail>1000)tail=0;
61         }
62     }
63     return dis[T]>-1;
64 }
65 int dfs(int u,int a)
66 {
67     if(u==T||a==0)return a;
68     int f,flow=0;
69     for(int& i=cur[u];i;i=e[i].next)
70     {
71         int v=e[i].to;
72         if(dis[v]==dis[u]+1&&(f=dfs(v,min(e[i].flow,a)))>0)
73         {
74             e[i].flow-=f;e[i^1].flow+=f;
75             flow+=f;a-=f;if(a==0)break;
76         }
77     }
78     return flow;
79 }
80 int main()
81 {
82     init();
83     while(bfs())
84     {
85         for(int i=S;i<=T;i++)cur[i]=first[i];
86         ans+=dfs(S,(int)1e9);
87     }
88     printf("%d",ans);
89     return 0;
90 }
网络流

26.【bzoj1690】 [Usaco2007 Dec]奶牛的旅行

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #define LL long long
 5 using namespace std;
 6 const int N=1005;
 7 int n,m,a,b,ww,cnt;
 8 int first[N],w[N];
 9 double dis[N];
10 bool f[N],flag;
11 struct edge{int to,next,w;double v;}e[10010];
12 int read()
13 {
14     int x=0,f=1;char c=getchar();
15     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
16     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
17     return x*f;
18 }
19 void ins(int u,int v,int w)
20 {
21     e[++cnt].to=v;e[cnt].w=w;
22     e[cnt].next=first[u];first[u]=cnt;
23 }
24 void spfa(int u)
25 {
26     if(flag)return;
27     f[u]=true;
28     for(int i=first[u];i;i=e[i].next)
29     {
30         int v=e[i].to;
31         if(dis[u]+e[i].v<dis[v])
32         {
33             if(f[v]){flag=true;return;}
34             dis[v]=dis[u]+e[i].v;
35             spfa(v);
36         }
37     }
38     f[u]=false;
39 }
40 bool check(double x)
41 {
42     for(int i=1;i<=n;i++)
43         for(int j=first[i];j;j=e[j].next)
44             e[j].v=e[j].w*x-w[e[j].to];
45     flag=false;
46     for(int i=1;i<=n;i++)dis[i]=f[i]=0;
47     for(int i=1;i<=n;i++)
48     {
49         spfa(i);
50         if(flag)return true;
51     }
52     return false;
53 }
54 int main()
55 {
56     n=read();m=read();
57     for(int i=1;i<=n;i++)w[i]=read();
58     for(int i=1;i<=m;i++)
59     {
60         a=read();b=read();ww=read();
61         ins(a,b,ww);
62     }
63     double l=0,r=10000,mid;
64     while(r-l>0.001)
65     {
66         mid=(l+r)/2;
67         if(check(mid))l=mid;
68         else r=mid;
69     }
70     printf("%.2lf",l);
71     return 0;
72 }
SPFA+01分数规划

27.【bzoj1708】[Usaco2007 Oct]Money奶牛的硬币

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #define LL long long
 5 using namespace std;
 6 const int N=1005;
 7 LL n,m,v[30],dp[10010];
 8 LL read()
 9 {
10     LL x=0,f=1;char c=getchar();
11     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
12     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
13     return x*f;
14 }
15 int main()
16 {
17     n=read();m=read();
18     for(int i=1;i<=n;i++)v[i]=read();
19     dp[0]=1;
20     for(int i=1;i<=n;i++)
21         for(int j=v[i];j<=m;j++)
22             dp[j]+=dp[j-v[i]];
23     printf("%lld",dp[m]);
24     return 0;
25 }
完全背包

28.【bzoj1725】[Usaco2006 Nov]Corn Fields牧场的安排

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #define LL long long
 5 using namespace std;
 6 const int N=1<<13;
 7 const int mod=100000000;
 8 int n,m,mx,x,ans,mp[15],f[15][N];
 9 int read()
10 {
11     int x=0,f=1;char c=getchar();
12     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
13     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
14     return x*f;
15 }
16 int main()
17 {
18     n=read();m=read();mx=(1<<m)-1;
19     for(int i=1;i<=n;i++)
20         for(int j=1;j<=m;j++)
21         {
22             x=read();
23             mp[i]=(mp[i]<<1)+x;
24         }
25     for(int i=0;i<=mx;i++)
26         if((i&(i>>1))==0&&(mp[1]|i)==mp[1])f[1][i]=1;
27     for(int i=2;i<=n;i++)
28         for(int j=0;j<=mx;j++)
29             for(int k=0;k<=mx;k++)
30                 if((k&(k>>1))==0&&(j&k)==0&&(mp[i]|k)==mp[i])
31                     f[i][k]=(f[i][k]+f[i-1][j])%mod;
32     for(int i=0;i<=mx;i++)
33         ans=(ans+f[n][i])%mod;
34     printf("%d",ans);
35     return 0;
36 }
状压dp

29.【bzoj1827】[Usaco2010 Mar]gather 奶牛大集会

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #define LL long long
 5 using namespace std;
 6 const int N=1e5+10;
 7 int n,a,b,v,sum,cnt,first[N],w[N],size[N];
 8 LL ans,dis[N];
 9 bool f[N];
10 struct edge{int to,next,w;}e[N<<1];
11 int read()
12 {
13     int x=0,f=1;char c=getchar();
14     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
15     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
16     return x*f;
17 }
18 void ins(int u,int v,int w)
19 {e[++cnt]=(edge){v,first[u],w};first[u]=cnt;}
20 int dfs1(int x)
21 {
22     f[x]=true;
23     ans+=dis[x]*w[x];
24     for(int i=first[x];i;i=e[i].next)
25         if(!f[e[i].to])
26         {
27             dis[e[i].to]=dis[x]+e[i].w;
28             size[x]+=dfs1(e[i].to);
29         }
30     size[x]+=w[x];
31     return size[x];
32 }
33 void dfs2(int x)
34 {
35     f[x]=true;
36     for(int i=first[x];i;i=e[i].next)
37     {
38         if(f[e[i].to]||sum-size[e[i].to]>size[e[i].to])continue;
39         ans+=((LL)sum-size[e[i].to]*2)*e[i].w;
40         dfs2(e[i].to);
41     }
42 }
43 int main()
44 {
45     n=read();
46     for(int i=1;i<=n;i++)w[i]=read();
47     for(int i=1;i<n;i++)
48     {
49         a=read();b=read();v=read();
50         ins(a,b,v);ins(b,a,v);
51     }
52     dfs1(1);sum=size[1];
53     memset(f,0,sizeof(f));dfs2(1);
54     printf("%lld",ans);
55     return 0;
56 }
57 
贪心

30.【bzoj2442】[Usaco2011 Open]修剪草坪

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #define LL long long
 5 using namespace std;
 6 const int N=1e5+10;
 7 int n,k,h,t,v[N],q[N];
 8 LL sum,ans=1e15,f[N];
 9 int read()
10 {
11     int x=0,f=1;char c=getchar();
12     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
13     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
14     return x*f;
15 }
16 int main()
17 {
18     n=read();k=read();
19     for(int i=1;i<=n;i++)
20         v[i]=read(),sum+=v[i];
21     for(int i=1;i<=n;i++)
22     {
23         f[i]=f[q[h]]+v[i];
24         while(h<=t&&f[q[t]]>f[i])t--;
25         q[++t]=i;
26         while(h<=t&&i-q[h]>k)h++;
27     }
28     for(int i=max(1,n-k);i<=n;i++)
29         ans=min(ans,f[i]);
30     printf("%lld",sum-ans);
31     return 0;
32 }
33 
单调队列优化dp

31.【bzoj1576】[Usaco2009 Jan]安全路经Travel

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #include<queue>
 5 using namespace std;
 6 const int N=1e5+10;
 7 int n,m,a,b,w,cnt,tot;
 8 int first[N],dis[N],fa[N],f[N],deep[N],ans[N];
 9 struct edge{int from,to,next,w;}e[N*4],ee[N*4];
10 struct node
11 {
12     int id,w;
13     bool operator < (const node& t)const{return w>t.w;} 
14 };
15 priority_queue<node>q;
16 int read()
17 {
18     int x=0,f=1;char c=getchar();
19     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
20     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
21     return x*f;
22 }
23 void ins(int u,int v,int w){e[++cnt]=(edge){u,v,first[u],w};first[u]=cnt;}
24 bool cmp(edge a,edge b){return a.w<b.w;}
25 int find(int t){return f[t]==t?t:f[t]=find(f[t]);}
26 void dijkstra()
27 {
28     memset(dis,0x3f,sizeof(dis));
29     dis[1]=0;deep[1]=1;q.push((node){1,0});
30     while(!q.empty())
31     {
32         node t=q.top();q.pop();
33         int u=t.id;
34         if(dis[u]!=t.w)continue;
35         for(int i=first[u];i;i=e[i].next)
36         {
37             int v=e[i].to;
38             if(dis[v]>dis[u]+e[i].w)
39             {
40                 dis[v]=dis[u]+e[i].w;
41                 deep[v]=deep[u]+1;
42                 fa[v]=u;
43                 q.push((node){v,dis[v]});
44             }
45         }
46     }
47 }
48 int main()
49 {
50     n=read();m=read();
51     for(int i=1;i<=n;i++)f[i]=i,ans[i]=-1;
52     for(int i=1;i<=m;i++)
53     {
54         a=read();b=read();w=read();
55         ins(a,b,w);ins(b,a,w);
56     }
57     dijkstra();
58 //  for(int i=1;i<=n;i++)printf("[%d] [dis]%d [deep]%d [fa]%d\n",i,dis[i],deep[i],fa[i]);
59     for(int i=1;i<=cnt;i++)
60     {
61         a=e[i].from;b=e[i].to;
62         if(deep[a]>deep[b])swap(a,b);
63         if(dis[a]+e[i].w==dis[b])continue;
64         ee[++tot]=(edge){a,b,0,dis[a]+dis[b]+e[i].w};
65     }
66     sort(ee+1,ee+tot+1,cmp);
67     for(int i=1;i<=tot;i++)
68     {
69         a=ee[i].from;b=ee[i].to;
70         while(a!=b)
71         {
72             if(deep[a]<deep[b])swap(a,b);
73             if(ans[a]==-1)ans[a]=ee[i].w-dis[a];
74             a=f[a]=find(fa[a]);
75         }
76     }
77     for(int i=2;i<=n;i++)printf("%d\n",ans[i]);
78     return 0;
79 }
并查集+dijkstra

32.【bzoj1592】[Usaco2008 Feb]Making the Grade 路面修整

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cstring>
 4 #include<cstdlib>
 5 #define LL long long
 6 using namespace std;
 7 const int N=2010;
 8 int n,sum,w[N],v[N],ans[N];
 9 int read()
10 {
11     int x=0,f=1;char c=getchar();
12     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
13     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
14     return x*f;
15 }
16 bool cmp(int a,int b){return a>b;}
17 int main()
18 {
19     n=read();
20     for(int i=1;i<=n;i++)v[i]=w[i]=read();
21     sort(v+1,v+n+1);
22     for(int i=1;i<=n;i++)
23         for(int j=1;j<=n;j++)
24         {
25             ans[j]+=abs(w[i]-v[j]);
26             if(j>1)ans[j]=min(ans[j],ans[j-1]);
27         }
28     sum=ans[n];
29     memset(ans,0,sizeof(ans));
30     for(int i=n;i>=1;i--)
31         for(int j=1;j<=n;j++)
32         {
33             ans[j]+=abs(w[i]-v[j]);
34             if(j>1)ans[j]=min(ans[j],ans[j-1]);
35         }
36     sum=min(sum,ans[n]);
37     printf("%d",sum);
38     return 0;
39 }
普通dp

33.【bzoj1782】[Usaco2010 Feb]slowdown 慢慢游

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cstring>
 4 #define LL long long
 5 using namespace std;
 6 const int N=100010;
 7 int n,a,b,cnt,tot,p;
 8 int first[N],l[N],r[N],t[N<<1];
 9 struct edge{int to,next;}e[N<<1];
10 int read()
11 {
12     int x=0,f=1;char c=getchar();
13     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
14     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
15     return x*f;
16 }
17 void ins(int u,int v){e[++cnt]=(edge){v,first[u]};first[u]=cnt;}
18 void dfs(int u)
19 {
20     l[u]=++tot;
21     for(int i=first[u];i;i=e[i].next)
22         if(!l[e[i].to])dfs(e[i].to);
23     r[u]=++tot;
24 }
25 int lowbit(int x){return x&(-x);}
26 void insert(int x,int p)
27 {
28     while(x<=2*n)
29     {
30         t[x]+=p;
31         x+=lowbit(x);
32     }
33 }
34 int query(int x)
35 {
36     int ans=0;
37     while(x){ans+=t[x];x-=lowbit(x);}
38     return ans;
39 }
40 int main()
41 {
42     n=read();
43     for(int i=1;i<n;i++)
44     {
45         a=read();b=read();
46         ins(a,b);ins(b,a);
47     }
48     dfs(1);
49     for(int i=1;i<=n;i++)
50     {
51         p=read();
52 //      printf("\n%d %d\n",l[p],r[p]);
53         printf("%d\n",query(l[p]));
54         insert(l[p],1);insert(r[p],-1);
55     }
56     return 0;
57 }
58 
dfs序+树状数组

34.【bzoj1715】[Usaco2006 Dec]Wormholes 虫洞

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cstring>
 4 #define LL long long
 5 using namespace std;
 6 const int N=600;
 7 int T,n,m,w,a,b,t,cnt,first[N],dis[N];
 8 bool flag,f[N];
 9 struct edge{int to,next,v;}e[N*10];
10 int read()
11 {
12     int x=0,f=1;char c=getchar();
13     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
14     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
15     return x*f;
16 }
17 void ins(int u,int v,int w){e[++cnt]=(edge){v,first[u],w};first[u]=cnt;}
18 void spfa(int u)
19 {
20     if(flag)return;
21     f[u]=true;
22     for(int i=first[u];i;i=e[i].next)
23     {
24         int v=e[i].to;
25         if(dis[u]+e[i].v<dis[v])
26         {
27             if(f[v]){flag=true;return;}
28             dis[v]=dis[u]+e[i].v;
29             spfa(v);
30         }
31     }
32     f[u]=false;
33 }
34 int main()
35 {
36     T=read();
37     while(T--)
38     {
39         memset(first,0,sizeof(first));
40         memset(f,0,sizeof(f));
41         n=read();m=read();w=read();
42         cnt=0;flag=false;
43         for(int i=1;i<=m;i++)
44         {
45             a=read();b=read();t=read();
46             ins(a,b,t);ins(b,a,t);
47         }
48         for(int i=1;i<=w;i++)
49         {
50             a=read();b=read();t=read();
51             ins(a,b,-t);
52         }
53         for(int i=1;i<=n;i++)
54         {
55             spfa(i);
56             if(flag)break;
57         }
58         if(flag)printf("YES\n");
59         else printf("NO\n");
60     }
61     return 0;
62 }
SPFA

35.【bzoj1596】[Usaco2008 Jan]电话网络

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cstring>
 4 #define LL long long
 5 using namespace std;
 6 const int N=1e4+10;
 7 int n,a,b,ans,cnt,first[N],fa[N];
 8 bool f[N];
 9 struct edge{int to,next;}e[N<<1];
10 int read()
11 {
12     int x=0,f=1;char c=getchar();
13     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
14     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
15     return x*f;
16 }
17 void ins(int u,int v){e[++cnt]=(edge){v,first[u]};first[u]=cnt;}
18 void dfs(int x)
19 {
20     bool flag=false;
21     for(int i=first[x];i;i=e[i].next)
22     {
23         int v=e[i].to;
24         if(fa[v])continue;
25         fa[v]=x;
26         dfs(v);
27         if(f[v])flag=true;
28     }
29     if(!flag&&!f[fa[x]]&&!f[x])f[fa[x]]=true,ans++;
30 }
31 int main()
32 {
33     n=read();
34     for(int i=1;i<n;i++)
35     {
36         a=read();b=read();
37         ins(a,b);ins(b,a);
38     }
39     fa[1]=1;dfs(1);
40     printf("%d",ans);
41     return 0;
42 }
43 
贪心

36.【bzoj1571】[Usaco2009 Open]滑雪课Ski

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cstring>
 4 #define LL long long
 5 using namespace std;
 6 const int N=1e4+10;
 7 int T,k,n,cnt=1;
 8 int g[105],f[105];
 9 struct lesson{int m,l,a;}c[105];
10 struct snow{int c,d;}s[N];
11 int read()
12 {
13     int x=0,f=1;char c=getchar();
14     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
15     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
16     return x*f;
17 }
18 bool cmp1(snow a,snow b){return a.c<b.c;}
19 bool cmp2(lesson a,lesson b){return a.m<b.m;}
20 int main()
21 {
22     memset(g,0x3f,sizeof(g));
23     T=read();k=read();n=read();
24     for(int i=1;i<=k;i++)
25         c[i].m=read(),c[i].l=read(),c[i].a=read();
26     for(int i=1;i<=n;i++)
27         s[i].c=read(),s[i].d=read();
28     sort(s+1,s+n+1,cmp1);
29     sort(c+1,c+k+1,cmp2);
30     c[0].m=0;c[0].l=0;c[0].a=1;
31     c[++k].m=T;c[k].l=0;c[k].a=0x3f3f3f3f;
32     for(int i=1;i<=100;i++)
33     {
34         while(cnt<=n&&s[cnt].c<=i)g[i]=min(g[i],s[cnt++].d);
35         g[i]=min(g[i],g[i-1]);
36     }
37     for(int i=1;i<=k;i++)
38     {
39         for(int j=0;j<i;j++)
40         {
41             if(c[j].a>=c[i].a||c[j].m+c[j].l>c[i].m)continue;
42             f[i]=max(f[i],f[j]+(c[i].m-c[j].m-c[j].l)/g[c[j].a]);
43         }
44     }
45     printf("%d",f[k]);
46     return 0;
47 }
普通dp

37.【bzoj1770】[Usaco2009 Nov]lights 燈

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cstring>
 4 #include<cmath>
 5 using namespace std;
 6 int n,m,u,v,ans,sum,f[40][40],p[40];
 7 int read()
 8 {
 9     int x=0,f=1;char c=getchar();
10     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
11     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
12     return x*f;
13 }
14 void gauss()
15 {
16     int t;
17     for(int i=1;i<=n;i++)
18     {
19         for(t=i;t<=n;t++)if(f[t][i])break;
20         if(t>n)continue;
21         if(t!=i)
22             for(int j=i;j<=n+1;j++)
23                 swap(f[i][j],f[t][j]);
24         for(int k=i+1;k<=n;k++)
25             if(f[k][i])
26                 for(int j=i;j<=n+1;j++)
27                     f[k][j]^=f[i][j];
28     }
29 }
30 void dfs(int x)
31 {
32     if(sum>=ans)return;
33     if(!x){ans=sum;return;}
34     if(f[x][x])
35     {
36         int t=f[x][n+1];
37         for(int i=x+1;i<=n;i++)
38             t^=f[x][i]*p[i];
39         p[x]=t;
40         if(t)sum++;
41         dfs(x-1);
42         if(t)sum--;
43     }
44     else
45     {
46         p[x]=0;dfs(x-1);
47         p[x]=1;sum++;dfs(x-1);sum--;
48     }
49 }
50 int main()
51 {
52     n=read();m=read();
53     for(int i=1;i<=m;i++)
54     {
55         u=read();v=read();
56         f[u][v]=f[v][u]=1;
57     }
58     for(int i=1;i<=n;i++)
59         f[i][i]=f[i][n+1]=1;
60     gauss();
61     ans=0x3f3f3f3f;sum=0;
62     dfs(n);
63     printf("%d",ans);
64     return 0;
65 }
高斯消元

38.【bzoj1593】[Usaco2008 Feb]Hotel 旅馆

  1 #include<cstdio>
  2 #include<cstring>
  3 #include<algorithm>
  4 #define l(x) x<<1
  5 #define r(x) x<<1|1
  6 using namespace std;
  7 const int N=2e5+10;
  8 int n,m,f,p,q;
  9 struct node{int l,r,m,lm,rm,sum,tag;}t[N];
 10 int read()
 11 {
 12     int x=0,f=1;char c=getchar();
 13     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
 14     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
 15     return x*f;
 16 }
 17 void up(int x)
 18 {
 19 //  if(t[x].l==t[x].r)return;
 20     if(t[l(x)].m==t[l(x)].sum)t[x].lm=t[l(x)].sum+t[r(x)].lm;
 21     else t[x].lm=t[l(x)].lm;
 22     if(t[r(x)].m==t[r(x)].sum)t[x].rm=t[r(x)].sum+t[l(x)].rm;
 23     else t[x].rm=t[r(x)].rm;
 24     t[x].m=max(t[l(x)].m,t[r(x)].m);
 25     t[x].m=max(t[x].m,t[l(x)].rm+t[r(x)].lm);
 26 }
 27 void dn(int x)
 28 {
 29     int f=t[x].tag;t[x].tag=0;
 30     if(t[x].l==t[x].r)return;
 31     if(f==1)
 32     {
 33         t[l(x)].lm=t[l(x)].rm=t[l(x)].m=t[l(x)].sum;
 34         t[r(x)].lm=t[r(x)].rm=t[r(x)].m=t[r(x)].sum;
 35         t[l(x)].tag=t[r(x)].tag=1;
 36     }
 37     if(f==2)
 38     {
 39         t[l(x)].lm=t[l(x)].rm=t[l(x)].m=0;
 40         t[r(x)].lm=t[r(x)].rm=t[r(x)].m=0;
 41         t[l(x)].tag=t[r(x)].tag=2;
 42     }
 43 }
 44 void build(int x,int l,int r)
 45 {
 46     t[x].lm=t[x].rm=t[x].m=t[x].sum=r-l+1;
 47     t[x].l=l;t[x].r=r;
 48     if(l==r)return;
 49     int mid=(l+r)>>1;
 50     build(l(x),l,mid);build(r(x),mid+1,r);
 51 }
 52 void change(int x,int a,int b,int f)
 53 {
 54     dn(x);
 55     int l=t[x].l,r=t[x].r,mid=(l+r)>>1;
 56     if(a==l&&b==r)
 57     {
 58         if(f==1)t[x].lm=t[x].rm=t[x].m=t[x].sum;
 59         else t[x].lm=t[x].rm=t[x].m=0;
 60         t[x].tag=f;
 61         return;
 62     }
 63     if(mid>=b)change(l(x),a,b,f);
 64     else if(mid<a)change(r(x),a,b,f);
 65     else
 66     {
 67         change(l(x),a,mid,f);
 68         change(r(x),mid+1,b,f);
 69     }
 70     up(x);
 71 }
 72 int ask(int x,int w)
 73 {
 74     dn(x);
 75     int l=t[x].l,r=t[x].r,mid=(l+r)>>1;
 76     if(l==r)return l;
 77     if(t[l(x)].m>=w)return ask(l(x),w);
 78     if(t[l(x)].rm+t[r(x)].lm>=w)return mid-t[l(x)].rm+1;
 79     if(t[r(x)].m>=w)return ask(r(x),w);
 80 }
 81 int main()
 82 {
 83     n=read();m=read();
 84     build(1,1,n);
 85     for(int i=1;i<=m;i++)
 86     {
 87         f=read();
 88         if(f==1)
 89         {
 90             p=read();
 91             if(t[1].m<p)printf("0\n");
 92             else
 93             {
 94                 q=ask(1,p);
 95                 printf("%d\n",q);
 96                 change(1,q,q+p-1,2);
 97             }
 98         }
 99         else
100         {
101             p=read();q=read();
102             change(1,p,p+q-1,1);
103         }
104     }
105     return 0;
106 }
线段树

39.【bzoj1668】[Usaco2006 Oct]Cow Pie Treasures 馅饼里的财富

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 using namespace std;
 5 const int N=105;
 6 const int inf=0x3f3f3f3f;
 7 int r,c,mp[N][N],f[N][N];
 8 int read()
 9 {
10     int x=0,f=1;char c=getchar();
11     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
12     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
13     return x*f;
14 }
15 int main()
16 {
17     r=read();c=read();
18     for(int i=1;i<=r;i++)
19         for(int j=1;j<=c;j++)
20         {
21             mp[i][j]=read();
22             f[i][j]=-inf;
23         }
24     f[1][1]=mp[1][1];
25     for(int i=2;i<=c;i++)
26         for(int j=1;j<=r;j++)
27         {
28             if(j>1)f[j][i]=max(f[j][i],f[j-1][i-1]);
29             f[j][i]=max(f[j][i],f[j][i-1]);
30             if(j<r)f[j][i]=max(f[j][i],f[j+1][i-1]);
31             f[j][i]+=mp[j][i];
32         }
33     printf("%d",f[r][c]);
34     return 0;
35 }
普通dp

40.【bzoj1604】[Usaco2008 Open]Cow Neighborhoods 奶牛的邻居

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #include<set>
 5 #define LL long long
 6 using namespace std;
 7 const int N=1e5+10;
 8 const LL inf=1e15;
 9 int n,c,x,y,ans,h=1,f[N],sum[N];
10 struct node{LL x,y;int id;}a[N];
11 multiset <node> s;
12 set <node>::iterator it;
13 int read()
14 {
15     int x=0,f=1;char c=getchar();
16     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
17     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
18     return x*f;
19 }
20 int find(int t){return f[t]==t?t:f[t]=find(f[t]);}
21 bool cmp(node a,node b){return a.x<b.x;}
22 bool operator < (node a,node b){return a.y<b.y;}
23 void change(int x,int y)
24 {
25     int xx=find(x),yy=find(y);
26     if(xx!=yy)ans--,f[xx]=yy;
27 }
28 int main()
29 {
30     n=read();c=read();ans=n;
31     for(int i=1;i<=n;i++)
32     {
33         x=read();y=read();f[i]=i;
34         a[i].x=x+y;a[i].y=x-y;a[i].id=i;
35     }
36     sort(a+1,a+n+1,cmp);
37     s.insert((node){0,-inf,0});
38     s.insert((node){0,inf,0});
39     s.insert(a[1]);
40     for(int i=2;i<=n;i++)
41     {
42         while(a[i].x-a[h].x>c)s.erase(s.find(a[h++]));
43         it=s.lower_bound(a[i]);
44         node xx=*it,yy=*--it;
45         if(xx.y-a[i].y<=c)change(xx.id,a[i].id);
46         if(a[i].y-yy.y<=c)change(yy.id,a[i].id);
47         s.insert(a[i]);
48     }
49     printf("%d ",ans);ans=0;
50     for(int i=1;i<=n;i++)sum[find(i)]++;
51     for(int i=1;i<=n;i++)ans=max(ans,sum[i]);
52     printf("%d",ans);
53     return 0;
54 }
并查集+平衡树

41.【bzoj1691】[Usaco2007 Dec]挑剔的美食家

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cstring>
 4 #include<set>
 5 #define LL long long
 6 using namespace std;
 7 const int N=1e5+10;
 8 int n,m,cnt=1;
 9 LL ans;
10 struct node{int x,y;}a[N],b[N];
11 multiset <int> s;
12 multiset <int> :: iterator t;
13 int read()
14 {
15     int x=0,f=1;char c=getchar();
16     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
17     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
18     return x*f;
19 }
20 bool cmp(node a,node b){return a.y>b.y;}
21 int main()
22 {
23     n=read();m=read();
24     for(int i=1;i<=n;i++)a[i].x=read(),a[i].y=read();
25     for(int i=1;i<=m;i++)b[i].x=read(),b[i].y=read();
26     sort(a+1,a+n+1,cmp);
27     sort(b+1,b+m+1,cmp);
28     for(int i=1;i<=n;i++)
29     {
30         while(b[cnt].y>=a[i].y&&cnt<=m)s.insert(b[cnt++].x);
31         t=s.lower_bound(a[i].x);
32         if(t!=s.end())
33         {
34             ans+=*t;
35             s.erase(t);
36         }
37         else
38         {
39             printf("-1");
40             return 0;
41         }
42     }
43     printf("%lld",ans);
44     return 0;
45 }
平衡树+贪心

42.【bzoj1697】[Usaco2007 Feb]Cow Sorting牛排序

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cstring>
 4 #define LL long long
 5 using namespace std;
 6 const int N=1e4+10;
 7 int n,t,mn=1e6;
 8 LL ans;
 9 bool f[N];
10 struct node{int num,pos,id;}a[N];
11 int read()
12 {
13     int x=0,f=1;char c=getchar();
14     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
15     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
16     return x*f;
17 }
18 bool cmp1(node a,node b){return a.num<b.num;}
19 bool cmp2(node a,node b){return a.pos<b.pos;}
20 int main()
21 {
22     n=read();
23     for(int i=1;i<=n;i++)
24         a[i].num=read(),a[i].pos=i,mn=min(mn,a[i].num);
25     sort(a+1,a+n+1,cmp1);
26     for(int i=1;i<=n;i++)a[i].id=i;
27     sort(a+1,a+n+1,cmp2);
28     for(int i=1;i<=n;i++)
29     {
30         if(f[i])continue;
31         int mnn=a[i].num,sum=a[i].num,cnt=1;
32         f[i]=true;t=a[i].id;
33         while(!f[t])
34         {
35             mnn=min(mnn,a[t].num);
36             sum+=a[t].num;
37             f[t]=true;cnt++;
38             t=a[t].id;
39         }
40         ans+=min(sum+1ll*(cnt-2)*mnn,sum+mnn+1ll*(cnt+1)*mn);
41 //      printf("%d %d %d\n",sum,mnn,cnt);
42     }
43     printf("%lld",ans);
44     return 0;
45 }
置换

43.【bzoj1589】[Usaco2008 Dec]Trick or Treat on the Farm 采集糖果

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cstring>
 4 #define LL long long
 5 using namespace std;
 6 const int N=1e5+10;
 7 int n,head=1,tail,cnt;
 8 int to[N],in[N],s[N],q[N],ans[N];
 9 bool f[N];
10 int read()
11 {
12     int x=0,f=1;char c=getchar();
13     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
14     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
15     return x*f;
16 }
17 int main()
18 {
19     n=read();
20     for(int i=1;i<=n;i++)
21         to[i]=read(),in[to[i]]++;
22     for(int i=1;i<=n;i++)
23         if(!in[i])s[++tail]=i,f[i]=true;
24     while(head<=tail)
25     {
26         int h=s[head++];in[to[h]]--;
27         if(!in[to[h]])s[++tail]=to[h],f[to[h]]=true;
28     }
29     for(int i=1;i<=n;i++)
30     {
31         if(f[i])continue;
32         int t=to[i];cnt=0;
33         f[i]=true;q[++cnt]=i;
34         while(!f[t])
35             f[t]=true,q[++cnt]=t,t=to[t];
36         for(int j=1;j<=cnt;j++)ans[q[j]]=cnt;
37     }
38     for(int i=tail;i>=1;i--)
39         ans[s[i]]=ans[to[s[i]]]+1;
40     for(int i=1;i<=n;i++)
41         printf("%d\n",ans[i]);
42     return 0;
43 }
拓扑排序

44.【bzoj1574】[Usaco2009 Jan]地震损坏Damage

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cstring>
 4 #define LL long long
 5 using namespace std;
 6 const int N=3e4+10;
 7 int n,m,c,u,v,w,ans,cnt;
 8 int first[N];
 9 bool del[N],f[N];
10 struct edge{int to,next;}e[N*7];
11 int read()
12 {
13     int x=0,f=1;char c=getchar();
14     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
15     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
16     return x*f;
17 }
18 void ins(int u,int v){e[++cnt]=(edge){v,first[u]};first[u]=cnt;}
19 void delet(int x)
20 {
21     for(int i=first[x];i;i=e[i].next)
22         del[e[i].to]=true;
23 }
24 void dfs(int x)
25 {
26     ans--;f[x]=true;
27     for(int i=first[x];i;i=e[i].next)
28         if(!f[e[i].to]&&!del[e[i].to])dfs(e[i].to);
29 }
30 int main()
31 {
32     ans=n=read();m=read();c=read();
33     for(int i=1;i<=m;i++)
34     {
35         u=read();v=read();
36         ins(u,v);ins(v,u);
37     }
38     for(int i=1;i<=c;i++)delet(read());
39     dfs(1);
40     printf("%d",ans);
41     return 0;
42 }
43 
dfs+贪心

45.【bzoj1828】[Usaco2010 Mar]balloc 农场分配

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cstring>
 4 #define LL long long
 5 #define l(x) x<<1
 6 #define r(x) x<<1|1
 7 using namespace std;
 8 const int N=1e5+10;
 9 int n,m,sum,ans,L,R,mn[N*4],add[N*4];
10 struct node{int l,r;}a[N];
11 int read()
12 {
13     int x=0,f=1;char c=getchar();
14     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
15     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
16     return x*f;
17 }
18 bool cmp(node a,node b){return a.r==b.r?a.l>b.l:a.r<b.r;}
19 void up(int x){mn[x]=min(mn[l(x)],mn[r(x)]);}
20 void dn(int x,int l,int r)
21 {
22     int mid=(l+r)>>1;
23     mn[l(x)]+=add[x];add[l(x)]+=add[x];
24     mn[r(x)]+=add[x];add[r(x)]+=add[x];
25     add[x]=0;
26 }
27 void build(int x,int l,int r)
28 {
29     if(l==r){mn[x]=read();return;}
30     int mid=(l+r)>>1;
31     build(l(x),l,mid);
32     build(r(x),mid+1,r);
33     if(l!=r)up(x);
34 }
35 void change(int x,int l,int r)
36 {
37     if(l!=r)dn(x,l,r);
38     if(L<=l&&R>=r){mn[x]--;add[x]--;return;}
39     int mid=(l+r)>>1;
40     if(L<=mid)change(l(x),l,mid);
41     if(R>mid)change(r(x),mid+1,r);
42     if(l!=r)up(x);
43 }
44 void ask(int x,int l,int r)
45 {
46     if(l!=r)dn(x,l,r);
47     if(L<=l&&R>=r){ans=min(ans,mn[x]);return;}
48     int mid=(l+r)>>1;
49     if(L<=mid)ask(l(x),l,mid);
50     if(R>mid)ask(r(x),mid+1,r);
51 }
52 int main()
53 {
54     n=read();m=read();
55     build(1,1,n);
56     for(int i=1;i<=m;i++)
57         a[i].l=read(),a[i].r=read();
58     sort(a+1,a+m+1,cmp);
59     for(int i=1;i<=m;i++)
60     {
61         ans=1e9;L=a[i].l;R=a[i].r;ask(1,1,n);
62         if(!ans)continue;
63         sum++;change(1,1,n);
64     }
65     printf("%d",sum);
66     return 0;
67 }
线段树+贪心

46.【bzoj1709】[Usaco2007 Oct]Super Paintball超级弹珠

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cstring>
 4 #define LL long long
 5 using namespace std;
 6 const int N=1e2+10;
 7 int n,k,ans;
 8 int row[N],line[N],left[N*2],right[N*2],map[N][N];
 9 int read()
10 {
11     int x=0,f=1;char c=getchar();
12     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
13     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
14     return x*f;
15 }
16 int main()
17 {
18     n=read();k=read();
19     for(int i=1;i<=k;i++)
20         map[read()][read()]++;
21     for(int i=1;i<=n;i++)
22         for(int j=1;j<=n;j++)
23         {
24             row[i]+=map[i][j];
25             line[j]+=map[i][j];
26         }
27     for(int i=n;i>=1;i--)
28     {
29         int x=i,y=1;
30         while(x<=n&&y<=n)left[n-i+1]+=map[x++][y++];
31     }
32     for(int i=2;i<=n;i++)
33     {
34         int x=1,y=i;
35         while(x<=n&&y<=n)left[n+i-1]+=map[x++][y++];
36     }
37     for(int i=n;i>=1;i--)
38     {
39         int x=i,y=n;
40         while(x<=n&&y>=1)right[n-i+1]+=map[x++][y--];
41     }
42     for(int i=n-1;i>=1;i--)
43     {
44         int x=1,y=i;
45         while(x<=n&&y>=1)right[n+n-i]+=map[x++][y--];
46     }
47     for(int i=1;i<=n;i++)
48         for(int j=1;j<=n;j++)
49         {
50             int tmp=0;
51             tmp+=row[i]-map[i][j];
52             tmp+=line[j]-map[i][j];
53             tmp+=left[n+j-i]-map[i][j];
54             tmp+=right[2*n-j-i+1]-map[i][j];
55 //          printf("[%d %d] [left]%d %d [right]%d %d\n",i,j,n-j+i,left[n+j-i],2*n-j-i+1,right[2*n-j-i+1]);
56             if(tmp+map[i][j]==k)ans++;
57         }
58     printf("%d",ans);
59 /*  for(int i=1;i<=n;i++)printf("[%d] %d\n",i,row[i]);
60     for(int i=1;i<=n;i++)printf("[%d] %d\n",i,line[i]);
61     for(int i=1;i<=2*n-1;i++)printf("[%d] %d\n",i,left[i]);
62     for(int i=1;i<=2*n-1;i++)printf("[%d] %d\n",i,right[i]);*/
63     return 0;
64 }
枚举

47.【bzoj1584】[Usaco2009 Mar]Cleaning Up 打扫卫生

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cstring>
 4 #include<cmath>
 5 #define LL long long
 6 using namespace std;
 7 const int N=4e4+10;
 8 int n,m,a[N],f[N],pre[N],pos[N],cnt[N];
 9 int read()
10 {
11     int x=0,f=1;char c=getchar();
12     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
13     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
14     return x*f;
15 }
16 int main()
17 {
18     n=read();m=read();
19     m=(int)(sqrt(n));
20     for(int i=1;i<=n;i++)a[i]=read(),f[i]=i;
21     for(int i=1;i<=n;i++)
22     {
23         for(int j=1;j<=m;j++)
24             if(pre[a[i]]<=pos[j])cnt[j]++;
25         pre[a[i]]=i;
26         for(int j=1;j<=m;j++)
27             if(cnt[j]>j)
28             {
29                 int t=pos[j]+1;
30                 while(pre[a[t]]>t)t++;
31                 pos[j]=t;cnt[j]--;
32             }
33         for(int j=1;j<=m;j++)
34             f[i]=min(f[i],f[pos[j]]+j*j);
35     }
36     printf("%d",f[n]);
37     return 0;
38 }
神奇のdp

48.【bzoj1707】[Usaco2007 Nov]tanning分配防晒霜

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cstring>
 4 #define LL long long
 5 using namespace std;
 6 const int N=2510;
 7 int n,m,ans,id;
 8 struct cow{int mx,mn;}c[N];
 9 struct spf{int spf,cov;}s[N];
10 int read()
11 {
12     int x=0,f=1;char c=getchar();
13     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
14     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
15     return x*f;
16 }
17 bool cmp1(cow a,cow b){return a.mx<b.mx;}
18 bool cmp2(spf a,spf b){return a.spf<b.spf;}
19 int main()
20 {
21     n=read();m=read();
22     for(int i=1;i<=n;i++)
23         c[i].mn=read(),c[i].mx=read();
24     for(int i=1;i<=m;i++)
25         s[i].spf=read(),s[i].cov=read();
26     sort(c+1,c+n+1,cmp1);
27     sort(s+1,s+m+1,cmp2);
28     for(int i=1;i<=n;i++)
29     {
30         id=-1;
31         for(int j=1;j<=m;j++)
32             if(s[j].spf>=c[i].mn&&s[j].spf<=c[i].mx&&s[j].cov>0){id=j;break;}
33         if(id==-1)continue;
34         ans++;s[id].cov--;
35     }
36     printf("%d",ans);
37     return 0;
38 }
39 
贪心

49.【bzoj1754】[Usaco2005 qua]Bull Math

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cstring>
 4 #define LL long long
 5 using namespace std;
 6 const int N=105;
 7 char s[N];
 8 int lena,lenb,len,a[N],b[N],ans[N];
 9 int main()
10 {
11     scanf("%s",s+1);
12     lena=strlen(s+1);
13     for(int i=lena;i>=1;i--)a[i]=s[lena-i+1]-'0';
14     scanf("%s",s+1);
15     lenb=strlen(s+1);
16     for(int i=lenb;i>=1;i--)b[i]=s[lenb-i+1]-'0';
17     for(int i=1;i<=lenb;i++)
18         for(int j=1;j<=lena;j++)
19             ans[i+j-1]+=b[i]*a[j];
20     len=lena+lenb+1;
21     for(int i=1;i<=len;i++)
22         if(ans[i]>9)ans[i+1]+=ans[i]/10,ans[i]%=10;
23     while(len>1&&!ans[len])len--;
24     for(int i=len;i>=1;i--)printf("%d",ans[i]);
25     return 0;
26 }
27 
高精度

50.【bzoj1706】[usaco2007 Nov]relays 奶牛接力跑

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cstring>
 4 #define LL long long
 5 using namespace std;
 6 const int N=105;
 7 int n,t,s,e,cnt,x,y,w,id[N*10];
 8 struct node
 9 {
10     int map[N][N];
11     node(){memset(map,0x3f,sizeof(map));}
12 }m,ans;
13 int read()
14 {
15     int x=0,f=1;char c=getchar();
16     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
17     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
18     return x*f;
19 }
20 int get_id(int x){return id[x]?id[x]:id[x]=++cnt;}
21 node operator * (node a,node b)
22 {
23     node c;
24     for(int k=1;k<=cnt;k++)
25         for(int i=1;i<=cnt;i++)
26             for(int j=1;j<=cnt;j++)
27                 c.map[i][j]=min(c.map[i][j],a.map[i][k]+b.map[k][j]);
28     return c;
29 }
30 int main()
31 {
32     n=read();t=read();s=read();e=read();
33     s=get_id(s);e=get_id(e);
34     for(int i=1;i<=t;i++)
35     {
36         w=read();x=read();y=read();
37         x=get_id(x);y=get_id(y);
38         m.map[x][y]=m.map[y][x]=min(m.map[x][y],w);
39     }
40     bool f=false;
41     for(int p=1;p<=n;p<<=1,m=m*m)
42         if(p&n)
43         {
44             if(!f)ans=m,f=true;
45             else ans=ans*m;
46         }
47     printf("%d",ans.map[s][e]);
48     return 0;
49 }
Floyd+矩阵乘法

51.【bzoj1753】[Usaco2005 qua]Who's in the Middle

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cstring>
 4 #define LL long long
 5 using namespace std;
 6 const int N=10005;
 7 int n,a[N];
 8 int read()
 9 {
10     int x=0,f=1;char c=getchar();
11     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
12     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
13     return x*f;
14 }
15 int main()
16 {
17     n=read();
18     for(int i=1;i<=n;i++)a[i]=read();
19     sort(a+1,a+n+1);
20     printf("%d",a[(n+1)>>1]);
21     return 0;
22 }
模拟

52.【bzoj1710】[Usaco2007 Open]Cheappal 廉价回文

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cstring>
 4 #define LL long long
 5 using namespace std;
 6 const int N=2005;
 7 const int inf=0x3f3f3f3f;
 8 int n,m,t,p[30],f[N][N],ans=inf;
 9 char s[N],ch[2];
10 int read()
11 {
12     int x=0,f=1;char c=getchar();
13     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
14     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
15     return x*f;
16 }
17 int main()
18 {
19     m=read();n=read();
20     scanf("%s",s+1);
21     for(int i=1;i<=m;i++)
22     {
23         scanf("%s",ch);
24         t=ch[0]-'a';
25         p[t]=read();
26         p[t]=min(p[t],read());
27     }
28     memset(f,0x3f,sizeof(f));
29     f[0][0]=0;
30     f[1][0]=p[s[1]-'a'];
31     f[0][1]=p[s[n]-'a'];
32     for(int i=0;i<=n;i++)
33     {
34         for(int j=0;j<=n-i;j++)
35         {
36             t=inf;
37             if(i&&j&&s[i]==s[n-j+1])t=f[i-1][j-1];
38             if(i)t=min(t,f[i-1][j]+p[s[i]-'a']);
39             if(j)t=min(t,f[i][j-1]+p[s[n-j+1]-'a']);
40             f[i][j]=min(f[i][j],t);
41 //          printf("%d %d %d \n",i,j,f[i][j]);
42         }
43     }
44     for(int i=0;i<=n;i++)
45     {
46         ans=min(ans,f[i][n-i]);
47         ans=min(ans,f[i][n-i-1]);
48     }
49     printf("%d",ans);
50     return 0;
51 }
52 
区间dp

53.【bzoj1598】[Usaco2008 Mar]牛跑步

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cstring>
 4 #define LL long long
 5 using namespace std;
 6 const int N=1005;
 7 const int inf=0x3f3f3f3f;
 8 int n,m,k,x,y,d,cnt,aa,bb;
 9 int c[105],first[N],f[N][105];
10 struct edge{int to,next,w;}e[N*10];
11 int read()
12 {
13     int x=0,f=1;char c=getchar();
14     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
15     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
16     return x*f;
17 }
18 void ins(int u,int v,int w){e[++cnt]=(edge){v,first[u],w};first[u]=cnt;}
19 void check(int a,int b,int w)
20 {
21     memset(c,0x3f,sizeof(c));
22     cnt=0;aa=bb=1;
23     while(cnt<k)
24     {
25         if(f[a][aa]<f[b][bb]+w)c[++cnt]=f[a][aa++];
26         else c[++cnt]=f[b][bb++]+w;
27     }
28     for(int i=1;i<=k;i++)f[a][i]=c[i];
29 }
30 int main()
31 {
32     n=read();m=read();k=read();
33     for(int i=1;i<=m;i++)
34     {
35         x=read();y=read();d=read();
36         ins(y,x,d);
37     }
38     memset(f,0x3f,sizeof(f));
39     f[n][1]=0;
40     for(int i=n-1;i>=1;i--)
41         for(int j=first[i];j;j=e[j].next)
42             check(i,e[j].to,e[j].w);
43     for(int i=1;i<=k;i++)
44         if(f[1][i]>=inf)printf("-1\n");
45         else printf("%d\n",f[1][i]);
46     return 0;
47 }
k短路

54.【bzoj2060】[Usaco2010 Nov]Visiting Cows 拜访奶牛

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cstring>
 4 using namespace std;
 5 const int N=5e4+10;
 6 int n,cnt,x,y,first[N],f[N][2];
 7 bool vis[N];
 8 struct edge{int to,next;}e[N*2];
 9 int read()
10 {
11     int x=0,f=1;char c=getchar();
12     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
13     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
14     return x*f;
15 }
16 void ins(int u,int v){e[++cnt]=(edge){v,first[u]};first[u]=cnt;}
17 void dfs(int x)
18 {
19     vis[x]=true;f[x][1]=1;
20     for(int i=first[x];i;i=e[i].next)
21     {
22         int to=e[i].to;
23         if(vis[to])continue;
24         dfs(to);
25         f[x][1]+=f[to][0];
26         f[x][0]+=max(f[to][0],f[to][1]);
27     }
28 }
29 int main()
30 {
31     n=read();
32     for(int i=1;i<n;i++)
33     {
34         x=read();y=read();
35         ins(x,y);ins(y,x);
36     }
37     dfs(1);
38     printf("%d",max(f[1][0],f[1][1]));
39     return 0;
40 }
树形dp

55.【bzoj1741】[Usaco2005 nov]Asteroids 穿越小行星群

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cstring>
 4 using namespace std;
 5 const int N=1e3+10;
 6 const int inf=0x3f3f3f3f;
 7 int n,k,cnt=1,x,y,S,T,ans;
 8 int first[N],dis[N],q[N],cur[N];
 9 struct edge{int to,next,flow;}e[50000];
10 int read()
11 {
12     int x=0,f=1;char c=getchar();
13     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
14     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
15     return x*f;
16 }
17 void ins(int u,int v,int flow){e[++cnt]=(edge){v,first[u],flow};first[u]=cnt;}
18 bool bfs()
19 {
20     memset(dis,-1,sizeof(dis));
21     int head=0,tail=1;
22     q[head]=S;dis[S]=0;
23     while(head!=tail)
24     {
25         int u=q[head++];if(head>5000)head=0;
26         for(int i=first[u];i;i=e[i].next)
27         {
28             int v=e[i].to;
29             if(dis[v]!=-1||!e[i].flow)continue;
30             dis[v]=dis[u]+1;
31             q[tail++]=v;if(tail>5000)tail=0;
32         }
33     }
34     return dis[T]>-1;
35 }
36 int dfs(int u,int a)
37 {
38     if(u==T||a==0)return a;
39     int f,flow=0;
40     for(int& i=cur[u];i;i=e[i].next)
41     {
42         int v=e[i].to;
43         if(dis[v]==dis[u]+1&&(f=dfs(v,min(e[i].flow,a)))>0)
44         {
45             e[i].flow-=f;e[i^1].flow+=f;
46             flow+=f;a-=f;if(a==0)break;
47         }
48     }
49     return flow;
50 }
51 int main()
52 {
53     n=read();k=read();
54     S=0;T=2*n+1;
55     for(int i=1;i<=k;i++)
56     {
57         x=read();y=read();
58         ins(x,y+n,inf);ins(y+n,x,0);
59     }
60     for(int i=1;i<=n;i++)ins(S,i,1),ins(i,S,0);
61     for(int i=1;i<=n;i++)ins(i+n,T,1),ins(T,i+n,0);
62     while(bfs())
63     {
64         for(int i=S;i<=T;i++)cur[i]=first[i];
65         ans+=dfs(S,inf);
66     }
67     printf("%d",ans);
68     return 0;
69 }
70 
网络流

56.【bzoj1578】[Usaco2009 Feb]Stock Market 股票市场

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cstring>
 4 using namespace std;
 5 int s,d,m,map[55][15],f[500005];
 6 int read()
 7 {
 8     int x=0,f=1;char c=getchar();
 9     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
10     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
11     return x*f;
12 }
13 int main()
14 {
15     s=read();d=read();m=read();
16     for(int i=1;i<=s;i++)
17         for(int j=1;j<=d;j++)
18             map[i][j]=read();
19     for(int i=1;i<=d-1;i++)
20     {
21         memset(f,0,sizeof(f));
22         for(int j=1;j<=s;j++)
23             for(int k=map[j][i];k<=m;k++)
24                 f[k]=max(f[k],f[k-map[j][i]]+map[j][i+1]-map[j][i]);
25         m+=f[m];
26     }
27     printf("%d",m);
28     return 0;
29 }
背包dp

57.【bzoj1703】[Usaco2007 Mar]Ranking the Cows 奶牛排名

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cstring>
 4 #include<bitset>
 5 using namespace std;
 6 bitset<1005> f[1005];
 7 int n,m,x,y,ans;
 8 int read()
 9 {
10     int x=0,f=1;char c=getchar();
11     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
12     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
13     return x*f;
14 }
15 int main()
16 {
17     n=read();m=read();
18     for(int i=1;i<=m;i++)
19         x=read(),y=read(),f[x][y]=true;
20     for(int i=1;i<=n;i++)
21         for(int j=1;j<=n;j++)
22             if(f[j][i])f[j]|=f[i];
23     for(int i=1;i<=n;i++)
24         for(int j=i+1;j<=n;j++)
25             if(!f[i][j]&&!f[j][i])ans++;
26     printf("%d",ans);
27     return 0;
28 }
Floyd传递闭包

58.【bzoj2199】[Usaco2011 Jan]奶牛议会

 1 #include<cstdio>
 2 #include<algorithm> 
 3 #include<cstring>
 4 #define LL long long
 5 using namespace std;
 6 const int inf=0x3f3f3f3f;
 7 const int N=2005; 
 8 const char s[3]={'?','Y','N'};
 9 struct edge{int to,next;}e[N*4];
10 int n,m,a,b,c,d,cnt,first[N],ans[N];
11 bool vis[N],p,q;
12 char ch;
13 int read()
14 {
15     int x=0,f=1;char c=getchar();
16     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
17     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
18     return x*f;
19 }
20 char get()
21 {
22     char ch=getchar();
23     while(ch!='Y'&&ch!='N')ch=getchar();
24     return ch;
25 }
26 void insert(int u,int v){e[++cnt]=(edge){v,first[u]};first[u]=cnt;}
27 void dfs(int x)
28 {
29     vis[x]=true;
30     for(int i=first[x];i;i=e[i].next)
31         if(!vis[e[i].to])dfs(e[i].to);
32 }
33 bool check(int x)
34 {
35     memset(vis,0,sizeof(vis));
36     dfs(x);
37     for(int i=1;i<=n;i++)
38         if(vis[i]&&vis[i+n])return false;
39     return true;
40 }
41 int main()
42 {
43     n=read();m=read();
44     for(int i=1;i<=m;i++)
45     {
46         a=read();ch=get();
47         if(ch=='Y')b=a+n;
48         else b=a,a=b+n;
49         c=read();ch=get();
50         if(ch=='Y')d=c+n;
51         else d=c,c=d+n;
52         insert(b,c);insert(d,a);
53     }
54     for(int i=1;i<=n;i++)
55     {
56         p=check(i);q=check(i+n);
57         if(!p&&!q){printf("IMPOSSIBLE");return 0;}
58         if(p&&q)ans[i]=0;
59         else if(p)ans[i]=1;
60         else ans[i]=2;
61     }
62     for(int i=1;i<=n;i++)putchar(s[ans[i]]);
63     return 0;
64 }
2-SAT

59.【bzoj1702】[Usaco2007 Mar]Gold Balanced Lineup 平衡的队列

 1 #include<cstdio>
 2 #include<algorithm> 
 3 #include<cstring>
 4 #include<map>
 5 #define LL long long
 6 #define UL unsigned long long
 7 using namespace std;
 8 const int N=1e5+10;
 9 const int hash=233;
10 int n,k,cnt,x,ans,f[N][35];
11 int read()
12 {
13     int x=0,f=1;char c=getchar();
14     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
15     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
16     return x*f;
17 }
18 map<UL,int>m;
19 int search(int t)
20 {
21     UL sum=0;
22     for(int i=2;i<=k;i++)sum=sum*hash+1LL*f[t][i];
23     if(!m[sum]&&sum)m[sum]=t;
24     return m[sum];
25 }
26 int main()
27 {
28     n=read();k=read();
29     for(int i=1;i<=n;i++)
30     {
31         cnt=0;x=read();
32         for(;x;x>>=1)f[i][++cnt]=x&1;
33         for(int j=1;j<=k;j++)f[i][j]+=f[i-1][j];
34     }
35     for(int i=1;i<=n;i++)
36     {
37         for(int j=2;j<=k;j++)f[i][j]-=f[i][1];
38         ans=max(ans,i-search(i));
39     }
40     printf("%d",ans);
41     return 0;
42 }
哈希

60.【bzoj1734】[Usaco2005 feb]Aggressive cows 愤怒的牛

 1 #include<cstdio>
 2 #include<algorithm> 
 3 #include<cstring>
 4 #define LL long long
 5 using namespace std;
 6 const int inf=0x3f3f3f3f;
 7 const int N=1e5+10;
 8 int n,c,ans,a[N]; 
 9 int read()
10 {
11     int x=0,f=1;char c=getchar();
12     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
13     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
14     return x*f;
15 }
16 bool check(int x)
17 {
18     int sum=1,last=a[1];
19     for(int i=2;i<=n;i++)
20         if(a[i]-last>=x)last=a[i],sum++;
21     return sum>=c;
22 }
23 void work()
24 {
25     int l=0,r=a[n]-a[1],mid;
26     while(l<=r)
27     {
28         mid=(l+r)>>1;
29         if(check(mid))ans=mid,l=mid+1;
30         else r=mid-1;
31     }
32 } 
33 int main()
34 {
35     n=read();c=read();
36     for(int i=1;i<=n;i++)a[i]=read();
37     sort(a+1,a+n+1);work();
38     printf("%d",ans);
39     return 0;
40 }
二分

61.【bzoj1585】[Usaco2009 Mar]Earthquake Damage 2 地震伤害

 1 #include<cstdio>
 2 #include<algorithm> 
 3 #include<cstring>
 4 #define LL long long
 5 using namespace std;
 6 const int N=6005;
 7 const int inf=0x3f3f3f3f;
 8 int n,m,k,x,y,cnt=1,ans,S,T;
 9 int dis[N],first[N],cur[N],q[N];
10 bool f[N];
11 struct edge{int to,next,flow;}e[N*20];
12 int read()
13 {
14     int x=0,f=1;char c=getchar();
15     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
16     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
17     return x*f;
18 }
19 void ins(int u,int v,int flow){e[++cnt]=(edge){v,first[u],flow};first[u]=cnt;}
20 bool bfs()
21 {
22     memset(dis,-1,sizeof(dis));
23     int head=0,tail=1;
24     q[head]=S;dis[S]=0;
25     while(head!=tail)
26     {
27         int u=q[head++];if(head>6000)head=0;
28         for(int i=first[u];i;i=e[i].next)
29         {
30             int v=e[i].to;
31             if(dis[v]!=-1||!e[i].flow)continue;
32             dis[v]=dis[u]+1;
33             q[tail++]=v;if(tail>6000)tail=0;
34         }
35     }
36     return dis[T]>-1;
37 }
38 int dfs(int u,int a)
39 {
40     if(u==T||a==0)return a;
41     int f,flow=0;
42     for(int& i=cur[u];i;i=e[i].next)
43     {
44         int v=e[i].to;
45         if(dis[v]==dis[u]+1&&(f=dfs(v,min(e[i].flow,a)))>0)
46         {
47             e[i].flow-=f;e[i^1].flow+=f;
48             flow+=f;a-=f;if(a==0)break;
49         }
50     }
51     return flow;
52 }
53 int main()
54 {
55     n=read();m=read();k=read();
56     S=0;T=2*n+1;
57     ins(1,T,inf);ins(T,1,0);
58     for(int i=1;i<=m;i++)
59     {
60         x=read();y=read();
61         ins(x+n,y,inf);ins(y,x+n,0);
62         ins(y+n,x,inf);ins(x,y+n,0);
63     }
64     for(int i=1;i<=k;i++)
65     {
66         x=read();f[x]=true;
67         ins(S,x,inf);ins(x,S,inf);
68     }
69     ins(1,1+n,inf);ins(1+n,1,0);
70     for(int i=1;i<=n;i++)
71     {
72         if(f[i])ins(i,i+n,inf),ins(i+n,i,0);
73         else ins(i,i+n,1),ins(i+n,i,0);
74     }
75     while(bfs())
76     {
77         for(int i=S;i<=T;i++)cur[i]=first[i];
78         ans+=dfs(S,inf);
79     }
80     printf("%d",ans);
81     return 0;
82 }
网络流

62.【bzoj1704】[Usaco2007 Mar]Face The Right Way 自动转身机

 1 #include<cstdio>
 2 #include<algorithm> 
 3 #include<cstring>
 4 #define LL long long
 5 using namespace std;
 6 const int N=5005;
 7 int n,cnt,K,M=5005,d[N],c[N];
 8 void read()
 9 {
10     char ch=getchar();
11     for(int i=1;i<=n;i++)
12     {
13         while(ch!='F'&&ch!='B')ch=getchar();
14         if(ch=='F')d[i]=0;
15         else d[i]=1;
16         ch=getchar();
17     }
18     for(int i=n;i;i--)d[i]-=d[i-1];
19 }
20 int main()
21 {
22     scanf("%d",&n);read();
23 //  for(int i=1;i<=n;i++)printf("%d ",d[i]);
24     for(int k=1;k<=n;k++)
25     {
26         int m=0,sum=0;bool f=true;
27         for(int i=1;i<=n;i++)c[i]=d[i];
28         for(int i=1;i<=n;i++)
29         {
30             sum+=c[i];
31             if(c[i]%2==0)continue;
32             if(i+k-1>n){f=false;break;}
33             c[i]++;c[i+k]--;sum++;m++;
34         }
35         if(f&&m<M)M=m,K=k;
36     }
37     printf("%d %d",K,M);
38     return 0;
39 }
枚举+贪心

63.【bzoj1718】[Usaco2006 Jan] Redundant Paths 分离的路径

 1 #include<cstdio>
 2 #include<algorithm> 
 3 #include<cstring>
 4 #define LL long long
 5 using namespace std;
 6 const int N=5005;
 7 const int M=10005;
 8 int n,m,x,y,cnt=1,T,top,C,ans;
 9 int ind[N],first[N],low[N],dfn[N],col[N],st[N],pos[N];
10 bool f[M*2];
11 struct edge{int from,to,next;}e[M*2];
12 int read()
13 {
14     int x=0,f=1;char c=getchar();
15     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
16     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
17     return x*f;
18 }
19 void ins(int u,int v){e[++cnt]=(edge){u,v,first[u]};first[u]=cnt;}
20 int min(int a,int b){return a<b?a:b;}
21 void tarjan(int x)
22 {
23     low[x]=dfn[x]=++T;
24     st[++top]=x;pos[x]=top;
25     for(int i=first[x];i;i=e[i].next)
26     {
27         int to=e[i].to;
28         if(f[i^1])continue;
29         f[i]=true;
30         if(!dfn[to])tarjan(to),low[x]=min(low[x],low[to]);
31         else low[x]=min(low[x],dfn[to]);
32     }
33     if(low[x]==dfn[x])
34         for(C++;top>=pos[x];top--)col[st[top]]=C;
35 }
36 int main()
37 {
38     n=read();m=read();
39     for(int i=1;i<=m;i++)
40     {
41         x=read();y=read();
42         ins(x,y);ins(y,x);
43     }
44     for(int i=1;i<=n;i++)
45         if(!dfn[i])tarjan(i);
46     for(int i=2;i<=cnt;i+=2)
47         if(col[e[i].from]!=col[e[i].to])
48             ind[col[e[i].from]]++,ind[col[e[i].to]]++;
49     for(int i=1;i<=C;i++)
50         if(ind[i]==1)ans++;
51     printf("%d",(ans+1)>>1);
52     return 0;
53 }
tarjan

64.【bzoj1700】[Usaco2007 Jan]Problem Solving 解题

 1 #include<cstdio>
 2 #include<algorithm> 
 3 #include<cstring>
 4 #define LL long long
 5 using namespace std;
 6 const int N=305;
 7 int n,m,ans,sum1[N],sum2[N],f[N][N];
 8 int read()
 9 {
10     int x=0,f=1;char c=getchar();
11     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
12     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
13     return x*f;
14 }
15 int main()
16 {
17     memset(f,0x3f,sizeof(f));
18     f[0][0]=0;
19     m=read();n=read();
20     for(int i=1;i<=n;i++)
21         sum1[i]=sum1[i-1]+read(),sum2[i]=sum2[i-1]+read();
22     for(int k=1;k<=n;k++)
23     {
24         for(int i=1;i<=k;i++)
25         {
26             if(sum1[k]-sum1[k-i]>m)continue;
27             for(int j=0;j<=k-i;j++)
28             {
29                 if(sum1[k]-sum1[k-i]+sum2[k-i]-sum2[k-i-j]>m)continue;
30                 f[k][i]=min(f[k][i],f[k-i][j]+1);
31 //              printf("%d %d %d   %d %d\n",k,i,f[k][i],k-i,j);
32             }
33         }
34         for(int i=1;i<=n;i++)
35             if(sum2[k]-sum2[k-i]<=m)f[k][0]=min(f[k][0],f[k][i]+1);
36     }
37 //  printf("%d %d\n",f[1][1],f[1][0]);
38     ans=f[n][0]+1;
39     for(int i=1;i<=n;i++)
40         if(sum2[n]-sum2[n-i]<=m)ans=min(ans,f[n][i]+2);
41     printf("%d",ans);
42     return 0;
43 }
普通dp

65.【bzoj1778】[Usaco2010 Hol]Dotp 驱逐猪猡

 1 #include<cstdio>
 2 #include<algorithm> 
 3 #include<cstring>
 4 #define LL long long
 5 using namespace std;
 6 const int N=305;
 7 const int M=1e5+10;
 8 int n,m,p,q,cnt,x,y;
 9 int first[N],ind[N];
10 double f[N][N];
11 struct edge{int to,next;}e[M];
12 int read()
13 {
14     int x=0,f=1;char c=getchar();
15     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
16     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
17     return x*f;
18 }
19 void ins(int u,int v){e[++cnt]=(edge){v,first[u]};first[u]=cnt;}
20 int main()
21 {
22     n=read();m=read();p=read();q=read();
23     for(int i=1;i<=m;i++)
24         x=read(),y=read(),ins(x,y),ins(y,x),ind[x]++,ind[y]++;
25     f[1][n+1]=1;
26     for(int i=1;i<=n;i++)
27     {
28         f[i][i]=1;
29         for(int j=first[i];j;j=e[j].next)
30             f[i][e[j].to]-=(1-1.0*p/q)/ind[e[j].to];
31     }
32     for(int i=1;i<=n;i++)
33     {
34         int now=i;
35         for(int j=i+1;j<=n;j++)if(f[j][i]>f[now][i])now=j;
36         if(now!=i)for(int j=i+1;j<=n;j++)swap(f[i][j],f[now][j]);
37         for(int j=i+1;j<=n;j++)
38             for(int k=n+1;k>=i;k--)
39                 f[j][k]-=f[j][i]/f[i][i]*f[i][k];
40     } 
41     for(int i=n;i>=1;i--)
42     {
43         for(int j=i+1;j<=n;j++)f[i][n+1]-=f[i][j]*f[j][n+1];
44         f[i][n+1]/=f[i][i];
45     }
46     for(int i=1;i<=n;i++)printf("%.9lf\n",f[i][n+1]*p/q);
47     return 0;
48 }
高斯消元+概率dp

66.【bzoj1776】[Usaco2010 Hol]cowpol 奶牛政坛

 1 #include<cstdio>
 2 #include<algorithm> 
 3 #include<cstring>
 4 #define LL long long
 5 using namespace std;
 6 const int N=2e5+10;
 7 int n,k,rt,cnt,p;
 8 int a[N],first[N],deep[N],id[N],depth[N],ans[N],x[N][18];
 9 struct edge{int to,next;}e[N];
10 int read()
11 {
12     int x=0,f=1;char c=getchar();
13     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
14     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
15     return x*f;
16 }
17 void ins(int u,int v){e[++cnt]=(edge){v,first[u]};first[u]=cnt;}
18 void dfs(int k)
19 {
20     for(int i=1;(1<<i)<=deep[k];i++)
21         x[k][i]=x[x[k][i-1]][i-1];
22     for(int i=first[k];i;i=e[i].next) 
23     {
24         x[e[i].to][0]=k;
25         deep[e[i].to]=deep[k]+1;
26         dfs(e[i].to);
27     }
28 }
29 int lca(int ri,int rj)
30 {
31     if(deep[ri]<deep[rj])swap(ri,rj);
32     int d=deep[ri]-deep[rj]; 
33     for(int i=0;(1<<i)<=d;i++)
34         if((1<<i)&d)ri=x[ri][i];
35     if(ri==rj)return ri;
36     for(int i=16;i>=0;i--)
37         if((1<<i)<=deep[rj]&&x[ri][i]!=x[rj][i])
38             ri=x[ri][i],rj=x[rj][i];
39     return x[ri][0];
40 }
41 int main()
42 {
43     n=read();k=read();
44     for(int i=1;i<=n;i++)
45     {
46         a[i]=read();p=read();
47         if(!p)rt=i;else ins(p,i);
48     }
49     deep[rt]=1;dfs(rt);
50     for(int i=1;i<=n;i++)
51         if(deep[i]>depth[a[i]])id[a[i]]=i,depth[a[i]]=deep[i];
52     for(int i=1;i<=n;i++)
53     {
54         if(i==id[a[i]])continue;
55         int lc=lca(i,id[a[i]]);
56         ans[a[i]]=max(ans[a[i]],deep[i]+deep[id[a[i]]]-2*deep[lc]);
57     }
58     for(int i=1;i<=k;i++)printf("%d\n",ans[i]);
59     return 0;
60 }
LCA

67.【bzoj1731】[Usaco2005 dec]Layout 排队布局

 1 #include<cstdio>
 2 #include<algorithm> 
 3 #include<cstring>
 4 #define LL long long
 5 using namespace std;
 6 const int N=1005;
 7 const int M=3e4+10; 
 8 const int inf=-1111638595;
 9 int n,ml,md,a,b,w,cnt;
10 int first[N],dis[N];
11 bool f,vis[N];
12 struct edge{int to,next,w;}e[M];
13 int read()
14 {
15     int x=0,f=1;char c=getchar();
16     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
17     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
18     return x*f;
19 }
20 void ins(int u,int v,int w){e[++cnt]=(edge){v,first[u],w};first[u]=cnt;}
21 void dfs(int x)
22 {
23     vis[x]=true;
24     for(int i=first[x];i&&!f;i=e[i].next)
25     {
26         int to=e[i].to;
27         if(dis[to]<dis[x]+e[i].w)
28         {
29             dis[to]=dis[x]+e[i].w;
30             if(vis[to]){f=1;return;}
31             else dfs(to);
32         }
33     }
34     vis[x]=false;
35 }
36 int main()
37 {
38     n=read();ml=read();md=read();
39     for(int i=1;i<n;i++)ins(i,i+1,0);
40     for(int i=1;i<=ml;i++)
41     {
42         a=read();b=read();w=read();
43         if(a>b)swap(a,b);ins(b,a,-w);
44     }
45     for(int i=1;i<=md;i++)
46     {
47         a=read();b=read();w=read();
48         if(a>b)swap(a,b);ins(a,b,w);
49     }
50     memset(dis,189,sizeof(dis));
51     dis[n]=0;dfs(n);
52     if(f)printf("-1");
53     else if(dis[1]==inf)printf("-2");
54     else printf("%d",-dis[1]);
55     return 0;
56 }
差分约束

68.【bzoj1755】[Usaco2005 qua]Bank Interest

 1 #include<cstdio>
 2 #include<algorithm> 
 3 #include<cstring>
 4 #define LL long long
 5 using namespace std;
 6 int r,m,y; 
 7 double p,ans;
 8 int read()
 9 {
10     int x=0,f=1;char c=getchar();
11     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
12     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
13     return x*f;
14 }
15 int main()
16 {
17     r=read();m=read();y=read();
18     p=1+1.0*r/100;ans=m;
19     for(int i=1;i<=y;i++)ans=ans*p;
20     printf("%lld",(LL)ans);
21     return 0;
22 }
模拟

69.【bzoj1705】[Usaco2007 Nov]Telephone Wire 架设电话线

 1 #include<cstdio>
 2 #include<algorithm> 
 3 #include<cstring>
 4 #define LL long long
 5 using namespace std;
 6 const int inf=0x3f3f3f3f;
 7 const int N=1e5+10;
 8 const int M=105;
 9 int n,c,a[N],f[M],up[M],dn[M];
10 int read()
11 {
12     int x=0,f=1;char c=getchar();
13     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
14     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
15     return x*f;
16 }
17 int main()
18 {
19     n=read();c=read();
20     for(int i=1;i<=n;i++)a[i]=read();
21     for(int i=1;i<=100;i++)
22         if(i<a[1])f[i]=inf;
23         else f[i]=(i-a[1])*(i-a[1]);
24     for(int i=2;i<=n;i++)
25     {
26         int t=inf;
27         for(int j=100;j;j--)up[j]=t=min(t,f[j]+j*c);
28         t=inf;
29         for(int j=1;j<=100;j++)
30         {
31             dn[j]=t=min(t,f[j]-j*c);
32             f[j]=inf;
33         }
34         for(int j=a[i];j<=100;j++)
35             f[j]=min(dn[j]+j*c,up[j]-j*c)+(j-a[i])*(j-a[i]);
36     }
37     int ans=inf;
38     for(int i=1;i<=100;i++)ans=min(ans,f[i]);
39     printf("%d",ans);
40     return 0;
41 }
奇怪のdp

70.【bzoj2200】[Usaco2011 Jan]道路和航线

 1 #include<cstdio>
 2 #include<algorithm> 
 3 #include<cstring>
 4 #include<queue> 
 5 #define LL long long
 6 using namespace std;
 7 const int inf=0x3f3f3f3f;
 8 const int N=25005;
 9 const int M=150010;
10 int n,mr,mp,S,cnt,tot,u,v,w,C,head,tail;
11 int first[N],FIRST[N],id[N],color[N],dis[N],ind[N],Q[N];
12 bool vis[N],f[N],mark[M];
13 struct edge{int to,next,w;}e[M];
14 struct path{int from,to,next,w;}E[M];
15 struct node{int d,id;bool operator < (const node& a)const{return a.d<d;}};
16 priority_queue<node>q[N];
17 int read()
18 {
19     int x=0,f=1;char c=getchar();
20     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
21     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
22     return x*f;
23 }
24 void ins(int u,int v,int w){e[++cnt]=(edge){v,first[u],w};first[u]=cnt;}
25 void INS(int u,int v,int w){E[++tot]=(path){u,v,FIRST[u],w};FIRST[u]=tot;}
26 void dfs(int x)
27 {
28     color[x]=C;
29     for(int i=first[x];i;i=e[i].next)
30         if(!color[e[i].to])dfs(e[i].to);
31 }
32 void search(int x)
33 {
34     vis[x]=f[color[x]]=true;
35     for(int i=first[x];i;i=e[i].next)
36         if(!vis[e[i].to])search(e[i].to);
37 }
38 void find(int x)
39 {
40     vis[x]=true;
41     for(int i=FIRST[x];i;i=E[i].next)
42     {
43         int to=E[i].to,now=color[to];
44         dis[to]=min(dis[to],dis[x]+E[i].w);
45 //      printf("to:%d dis:%d\n",to,dis[to]);
46         q[now].push((node){dis[to],to});
47         ind[now]--;if(!ind[now])Q[tail++]=now;
48     }
49     for(int i=first[x];i;i=e[i].next)
50         if(!mark[i]&&!vis[e[i].to])find(e[i].to);
51 }
52 int main()
53 {
54     n=read();mr=read();mp=read();S=read();
55     for(int i=1;i<=mr;i++)
56         u=read(),v=read(),w=read(),ins(u,v,w),ins(v,u,w);
57     for(int i=1;i<=n;i++)
58         if(!color[i])id[++C]=i,dfs(i);
59     for(int i=1;i<=mp;i++)
60     {
61         u=read(),v=read(),w=read();
62         ins(u,v,w),mark[cnt]=true;INS(u,v,w);
63     }
64     search(S);
65     for(int i=1;i<=mp;i++)
66         if(f[color[E[i].from]]&&f[color[E[i].to]])ind[color[E[i].to]]++;
67     memset(dis,0x3f,sizeof(dis));
68     dis[S]=0;memset(vis,0,sizeof(vis));
69     Q[tail++]=color[S];q[color[S]].push((node){0,S});
70     while(head!=tail)
71     {
72         int x=Q[head++];
73         while(!q[x].empty())
74         {
75             node t=q[x].top();u=t.id;q[x].pop();
76             if(dis[t.id]!=t.d)continue;
77 //          printf("u:%d\n",u);
78             for(int i=first[u];i;i=e[i].next)
79                 if(!mark[i]&&dis[e[i].to]>dis[u]+e[i].w)
80                     dis[e[i].to]=dis[u]+e[i].w,q[x].push((node){dis[e[i].to],e[i].to});
81         }
82         find(id[x]);
83     }
84     for(int i=1;i<=n;i++)
85         if(dis[i]>=inf)printf("NO PATH\n");
86         else printf("%d\n",dis[i]);
87     return 0;
88 }
dijkstra+拓扑排序

71.【bzoj1575】[Usaco2009 Jan]气象牛Baric

 1 #include<cstdio>
 2 #include<algorithm> 
 3 #include<cstring>
 4 #define LL long long
 5 using namespace std;
 6 const int N=105;
 7 const int inf=0x3f3f3f3f;
 8 int n,e,a[N],w[N][N],f[N][N]; 
 9 int read()
10 {
11     int x=0,f=1;char c=getchar();
12     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
13     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
14     return x*f;
15 }
16 int abs(int a){return a>0?a:-a;}
17 int main()
18 {
19     n=read();e=read();
20     for(int i=1;i<=n;i++)a[i]=read();
21     for(int i=0;i<=n-1;i++)
22         for(int j=i+2;j<=n+1;j++)
23             for(int k=i+1;k<j;k++)
24             {
25                 if(i==0)w[i][j]+=2*abs(a[j]-a[k]);
26                 else if(j==n+1)w[i][j]+=2*abs(a[k]-a[i]);
27                 else w[i][j]+=abs(2*a[k]-a[i]-a[j]);
28             }
29     memset(f,0x3f,sizeof(f));
30     f[0][0]=0;w[0][n+1]=inf;
31     for(int i=1;i<=n+1;i++)
32         for(int j=1;j<=i;j++)
33             for(int k=0;k<i;k++)
34                 if(k>=j-1)f[i][j]=min(f[i][j],f[k][j-1]+w[k][i]);
35     for(int i=1;i<=n+1;i++)
36         if(f[n+1][i]<=e)
37         {printf("%d %d\n",i-1,f[n+1][i]);break;}
38     return 0;
39 }
普通dp

72.【bzoj1774】[Usaco2009 Dec]Toll 过路费

 1 #include<cstdio>
 2 #include<algorithm> 
 3 #include<cstring>
 4 #define LL long long
 5 using namespace std;
 6 const int N=255;
 7 const int inf=0x3f3f3f3f;
 8 int n,m,q,x,y,w,s,t,c[N],f[N][N],g[N][N];
 9 struct node{int c,id;}a[N];
10 int read()
11 {
12     int x=0,f=1;char c=getchar();
13     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
14     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
15     return x*f;
16 }
17 bool cmp(node a,node b){return a.c<b.c;}
18 int main()
19 {
20     n=read();m=read();q=read();
21     for(int i=1;i<=n;i++)
22         c[i]=a[i].c=read(),a[i].id=i;
23     sort(a+1,a+n+1,cmp);
24     for(int i=1;i<=n;i++)
25         for(int j=i;j<=n;j++)
26             if(i!=j)f[i][j]=f[j][i]=g[i][j]=g[j][i]=inf;
27             else g[i][j]=c[i];
28     for(int i=1;i<=m;i++)
29         x=read(),y=read(),w=read(),f[x][y]=f[y][x]=min(f[x][y],w);
30     for(int now=1;now<=n;now++)
31     {
32         int k=a[now].id;
33         for(int i=1;i<=n;i++)
34             for(int j=1;j<=n;j++)
35             {
36                 f[i][j]=f[j][i]=min(f[i][j],f[i][k]+f[k][j]);
37                 g[i][j]=g[j][i]=min(g[i][j],f[i][j]+max(a[now].c,max(c[i],c[j])));
38             }
39     }
40     while(q--)
41     {
42         s=read();t=read();
43         printf("%d\n",g[s][t]);
44     }
45     return 0;
46 }
奇怪のFloyd

73.【bzoj1914】[Usaco2010 OPen]Triangle Counting 数三角形

 1 #include<cstdio>
 2 #include<algorithm> 
 3 #include<cstring>
 4 #define LL long long
 5 using namespace std;
 6 const int N=1e5+10;
 7 const int inf=0x3f3f3f3f;
 8 int n,x,y;
 9 LL ans,now,to;
10 struct node{int x,y,id;double t;}a[N];
11 int read()
12 {
13     int x=0,f=1;char c=getchar();
14     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
15     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
16     return x*f;
17 }
18 bool cmp(node a,node b){return a.id==b.id?a.t>b.t:a.id<b.id;}
19 int main()
20 {
21     n=read();
22     for(int i=1;i<=n;i++)
23     {
24         a[i].x=x=read();a[i].y=y=read();
25         if(x)a[i].t=1.0*y/x;
26         else a[i].t=-inf;
27         if(x>0&&y>=0)a[i].id=1;
28         else if(x>=0&&y<0)a[i].id=2;
29         else if(x<0&&y<=0)a[i].id=3;
30         else a[i].id=4;
31     }
32     sort(a+1,a+n+1,cmp);
33     ans=1ll*n*(n-1)*(n-2)/6;now=1;to=2;
34     for(int i=1;i<=n;i++)
35     {
36         now--;
37         while(1ll*a[i].x*a[to].y<1ll*a[to].x*a[i].y)
38         {
39             now++;to++;
40             if(to>n)to-=n;
41         }
42         ans-=now*(now-1)/2;
43 //      printf("%.3lf %lld %lld %lld\n",a[i].t,now,to,ans);
44     }
45     printf("%lld",ans);
46     return 0;
47 }
计算几何

74.【bzoj1577】[Usaco2009 Feb]庙会捷运Fair Shuttle

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #include<queue>
 5 using namespace std;
 6 const int N=8e4+10;
 7 int n,m,st,ed,num,c,cnt,now,ans,first[N];
 8 bool f[N];
 9 struct edge{int ed,num,next;}e[N];
10 struct node1
11 {
12     int id,ed,num;
13     bool operator <(const node1&x)const{return x.ed<ed;}
14 };
15 priority_queue<node1>q1;
16 struct node2
17 {
18     int id,ed,num;
19     bool operator <(const node2&x)const{return x.ed>ed;}
20 };
21 priority_queue<node2>q2;
22 int read()
23 {
24     int x=0,f=1;char c=getchar();
25     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
26     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
27     return x*f;
28 }
29 void ins(int u,int v,int w){e[++cnt]=(edge){v,w,first[u]};first[u]=cnt;}
30 void push(int id,int ed,int num){q1.push((node1){id,ed,num});q2.push((node2){id,ed,num});}
31 int main()
32 {
33     m=read();n=read();c=read();
34     for(int i=1;i<=m;i++)
35         st=read(),ed=read(),num=read(),ins(st,ed,num);
36     for(int i=1;i<=n;i++)
37     {
38         while(!q1.empty())
39         {
40             node1 t=q1.top();
41             if(t.ed>i)break;
42             if(!f[t.id]){q1.pop();continue;}
43             now-=t.num;ans+=t.num;
44             f[t.id]=false;q1.pop();
45         }
46         for(int j=first[i];j;j=e[j].next)
47         {
48             f[j]=true;now+=e[j].num;
49             push(j,e[j].ed,e[j].num);
50         }
51         while(now>c)
52         {
53             node2 t=q2.top();
54             if(!f[t.id]){q2.pop();continue;}
55             if(t.num>now-c)
56             {
57                 q2.pop();f[t.id]=false;
58                 t.num-=now-c;now=c;
59                 push(++cnt,t.ed,t.num);
60                 f[cnt]=true;break;
61             }
62             q2.pop();f[t.id]=false;now-=t.num;
63         }
64     }
65     printf("%d",ans);
66     return 0;
67 }
堆+贪心

75.【bzoj3940】[Usaco2015 Feb]Censoring

不会AC自动机…… 留坑待填QAQ(已经懒得填了)

76.【bzoj1783】[Usaco2010 Jan]Taking Turns

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #include<queue>
 5 #define LL long long
 6 using namespace std;
 7 const int N=7e5+10;
 8 int n,a[N];
 9 LL f[3][N];
10 int read()
11 {
12     int x=0,f=1;char c=getchar();
13     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
14     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
15     return x*f;
16 }
17 int main()
18 {
19     n=read();
20     for(int i=1;i<=n;i++)a[i]=read();
21     for(int i=n;i>=0;i--)
22     {
23         f[1][i]=f[1][i+1];
24         f[2][i]=f[2][i+1];
25         if(f[2][i]+a[i]>=f[1][i])
26         {
27             f[1][i]=f[2][i]+a[i];
28             f[2][i]=f[1][i+1];
29         }
30     }
31     printf("%lld %lld",f[1][0],f[2][0]);
32     return 0;
33 }
博弈论

77.【bzoj1590】[Usaco2008 Dec]Secret Message 秘密信息

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #include<queue>
 5 #define LL long long
 6 using namespace std;
 7 const int N=5e5+10;
 8 int n,m,len,cnt,ans;
 9 int a[N],c[N][2],w[N],sum[N];
10 int read()
11 {
12     int x=0,f=1;char c=getchar();
13     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
14     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
15     return x*f;
16 }
17 void ins(int now,int p)
18 {
19     if(p>len){sum[now]++;w[now]++;return;}
20     if(!c[now][a[p]])c[now][a[p]]=++cnt;
21     ins(c[now][a[p]],p+1);
22     sum[now]=w[now];
23     if(c[now][0])sum[now]+=sum[c[now][0]];
24     if(c[now][1])sum[now]+=sum[c[now][1]];
25 }
26 void dfs(int now,int p)
27 {
28     if(p>len){ans+=sum[now];return;}
29     if(now)ans+=w[now];
30     if(!c[now][a[p]])return;
31     dfs(c[now][a[p]],p+1);
32 }
33 int main()
34 {
35     n=read();m=read();
36     for(int i=1;i<=n;i++)
37     {
38         len=read();
39         for(int j=1;j<=len;j++)a[j]=read();
40         ins(0,1);
41     }
42     for(int i=1;i<=m;i++)
43     {
44         len=read();ans=0;
45         for(int j=1;j<=len;j++)a[j]=read();
46         dfs(0,1);printf("%d\n",ans);
47     }
48     return 0;
49 }
trie

78.【bzoj2097】[Usaco2010 Dec]Exercise 奶牛健美操

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cstring>
 4 #define LL long long
 5 using namespace std;
 6 const int N=1e5+10;
 7 int n,m,u,v,cnt,l,r,mid,ans,sum,tot;
 8 int s[N],first[N],f[N];
 9 struct edge{int to,next;}e[N<<1];
10 int read()
11 {
12     int x=0,f=1;char c=getchar();
13     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
14     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
15     return x*f;
16 }
17 void ins(int u,int v){e[++cnt]=(edge){v,first[u]};first[u]=cnt;}
18 void dfs(int x,int fa)
19 {
20     for(int i=first[x];i;i=e[i].next)
21         if(e[i].to!=fa)dfs(e[i].to,x);
22     f[x]=1;tot=0;
23     for(int i=first[x];i;i=e[i].next)
24         if(e[i].to!=fa)s[++tot]=f[e[i].to];
25     sort(s+1,s+tot+1);
26     for(int i=tot;i;i--)
27         if(s[i]+s[i-1]>mid)sum++;
28         else{f[x]+=s[i];break;}
29 }
30 int main()
31 {
32     n=read();m=read();l=1;r=n;
33     for(int i=1;i<n;i++)
34         u=read(),v=read(),ins(u,v),ins(v,u);
35     while(l<=r)
36     {
37         mid=(l+r)>>1;
38         sum=0;dfs(1,0);
39         if(sum<=m)ans=mid,r=mid-1;
40         else l=mid+1;
41     }
42     printf("%d",ans);
43     return 0;
44 }
二分+贪心

79.【bzoj1663】[Usaco2006 Open]赶集

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cstring>
 4 #define LL long long
 5 using namespace std;
 6 const int N=4e2+10;
 7 int n,ans,f[N],map[N][N];
 8 struct node{int t,id;}a[N];
 9 int read()
10 {
11     int x=0,f=1;char c=getchar();
12     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
13     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
14     return x*f; 
15 }
16 bool cmp(node a,node b){return a.t<b.t;}
17 int main()
18 {
19     n=read();
20     for(int i=1;i<=n;i++)
21         a[i].t=read(),a[i].id=i;
22     sort(a+1,a+n+1,cmp);
23     for(int i=1;i<=n;i++)
24         for(int j=1;j<=n;j++)
25             map[i][j]=read();
26     for(int i=1;i<=n;i++)
27     {
28         if(map[1][a[i].id]<=a[i].t)f[i]=1;
29         for(int j=1;j<i;j++)
30             if(a[j].t+map[a[j].id][a[i].id]<=a[i].t&&f[j]+1>f[i])
31                 f[i]=f[j]+1;
32         ans=max(ans,f[i]);
33     }
34     printf("%d",ans);
35     return 0;
36 }
普通dp

80.【bzoj1696】[Usaco2007 Feb]Building A New Barn新牛舍

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cstring>
 4 #define LL long long
 5 using namespace std;
 6 const int N=1e4+10;
 7 const int inf=0x3f3f3f3f;
 8 const int xx[4]={0,0,-1,1};
 9 const int yy[4]={-1,1,0,0};
10 int n,ans=inf,tmp,num,x[N],y[N];
11 struct node{int x,y;}a[N];
12 int read()
13 {
14     int x=0,f=1;char c=getchar();
15     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
16     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
17     return x*f; 
18 }
19 int ab(int a){return a>0?a:-a;}
20 bool check(int x,int y)
21 {
22     for(int i=1;i<=n;i++)
23         if(a[i].x==x&&a[i].y==y)return false;
24     return true;
25 }
26 int work(int x,int y)
27 {
28     int sum=0;
29     for(int i=1;i<=n;i++)
30         sum+=ab(x-a[i].x)+ab(y-a[i].y);
31     return sum;
32 }
33 int main()
34 {
35     n=read();
36     for(int i=1;i<=n;i++)
37     {
38         a[i].x=x[i]=read();
39         a[i].y=y[i]=read();
40     }
41     sort(x+1,x+n+1);sort(y+1,y+n+1);
42     if(n&1)
43     {
44         int X=x[(n>>1)+1],Y=y[(n>>1)+1];
45         if(check(X,Y))printf("%d 1",work(X,Y));
46         else
47         {
48             for(int i=0;i<4;i++)
49             {
50                 tmp=work(X+xx[i],Y+yy[i]);
51                 if(tmp<ans)ans=tmp,num=1;
52                 else if(tmp==ans)num++;
53             }
54             printf("%d %d\n",ans,num);
55         }
56     }
57     else
58     {
59         int X1=x[n>>1],Y1=y[n>>1],X2=x[(n>>1)+1],Y2=y[(n>>1)+1];
60         num=(X2-X1+1)*(Y2-Y1+1);
61         for(int i=1;i<=n;i++)
62             if(a[i].x>=X1&&a[i].x<=X2&&a[i].y>=Y1&&a[i].y<=Y2)num--;
63         printf("%d %d",work(X1,Y1),num);
64     }
65     return 0;
66 }
67 
中位数+乱搞

81.【bzoj1594】[Usaco2008 Jan]猜数游戏

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #define LL long long
 5 using namespace std;
 6 const int N=1e6+10;
 7 int n,m,l,r,ans=1,mid,f[N];
 8 struct node{int l,r,w;}a[N],b[N];
 9 int read()
10 {
11     int x=0,f=1;char c=getchar();
12     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
13     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
14     return x*f;
15 }
16 bool cmp(node a,node b){return a.w>b.w;}
17 int find(int t){return f[t]==t?t:f[t]=find(f[t]);}
18 bool check(int x)
19 {
20     for(int i=1;i<=n+1;i++)f[i]=i;
21     for(int i=1;i<=x;i++)b[i]=a[i];
22     sort(b+1,b+x+1,cmp);
23     int l1=b[1].l,l2=b[1].l,r1=b[1].r,r2=b[1].r;
24     for(int i=2;i<=x;i++)
25     {
26         if(b[i].w<b[i-1].w)
27         {
28             if(find(l1)>r1)return false;
29             for(int j=f[l2];j<=r2;j=f[j])
30                 find(j),f[j]=find(j+1);
31             l1=l2=b[i].l;r1=r2=b[i].r;
32         }
33         else
34         {
35             l1=max(l1,b[i].l);l2=min(l2,b[i].l);
36             r1=min(r1,b[i].r);r2=max(r2,b[i].r);
37             if(r1<l1)return false;
38         }
39     }
40     if(find(l1)>r1)return false;
41     return true;
42 }
43 int main()
44 {
45     n=read();m=read();
46     for(int i=1;i<=m;i++)
47         a[i].l=read(),a[i].r=read(),a[i].w=read();
48     l=1;r=m+1;
49     while(l<=r)
50     {
51         mid=(l+r)>>1;
52         if(check(mid))ans=mid,l=mid+1;
53         else r=mid-1;
54     }
55     printf("%d",ans>=m?0:ans+1);
56     return 0;
57 }
二分+并查集

82.【bzoj1583】[Usaco2009 Mar]Moon Mooing 哞哞叫

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #define LL long long
 5 using namespace std;
 6 const int N=4e6+10;
 7 LL c,n,a1,b1,c1,a2,b2,c2,n1,n2,cnt,l,r,q[N];
 8 int main()
 9 {
10     scanf("%lld%lld",&c,&n);
11     scanf("%lld%lld%lld",&a1,&b1,&c1);
12     scanf("%lld%lld%lld",&a2,&b2,&c2);
13     q[++cnt]=c;l=r=1;n1=a1*c/c1+b1;n2=a2*c/c2+b2;
14     while(cnt<n)
15     {
16         if(n1<n2)
17         {
18             if(n1!=q[cnt])q[++cnt]=n1;
19             n1=q[++l]*a1/c1+b1;
20         }
21         else
22         {
23             if(n2!=q[cnt])q[++cnt]=n2;
24             n2=q[++r]*a2/c2+b2;
25         }
26     }
27     printf("%lld",q[n]);
28     return 0;
29 }
模拟

83.【bzoj1742】[Usaco2005 nov]Grazing on the Run 边跑边吃草

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #define LL long long
 5 using namespace std;
 6 const int N=1e3+10;
 7 int n,p,pos[N],fl[N][N],fr[N][N];
 8 int read()
 9 {
10     int x=0,f=1;char c=getchar();
11     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
12     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
13     return x*f;
14 }
15 int ab(int a){return a>0?a:-a;}
16 int main()
17 {
18     n=read();p=read();
19     for(int i=1;i<=n;i++)pos[i]=read();
20     sort(pos+1,pos+n+1);
21     for(int i=1;i<=n;i++)fl[i][i]=fr[i][i]=n*ab(pos[i]-p);
22     for(int num=2;num<=n;num++)
23         for(int i=1;i<=n-num+1;i++)
24         {
25             int j=i+num-1,now=n-j+i;
26             fl[i][j]=min(fl[i+1][j]+now*(pos[i+1]-pos[i]),fr[i+1][j]+now*(pos[j]-pos[i]));
27             fr[i][j]=min(fl[i][j-1]+now*(pos[j]-pos[i]),fr[i][j-1]+now*(pos[j]-pos[j-1]));
28         }
29     printf("%d",min(fl[1][n],fr[1][n]));
30     return 0;
31 }
未来dp

84.【bzoj1751】[Usaco2005 qua]Lake Counting

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #define LL long long
 5 using namespace std;
 6 const int N=1e2+10;
 7 const int xx[8]={-1,0,1,-1,1,-1,0,1};
 8 const int yy[8]={-1,-1,-1,0,0,1,1,1};
 9 int n,m,ans,X,Y;
10 bool map[N][N],f[N][N];
11 char s[N];
12 int read()
13 {
14     int x=0,f=1;char c=getchar();
15     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
16     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
17     return x*f;
18 }
19 void dfs(int x,int y)
20 {
21     f[x][y]=true;
22     for(int i=0;i<8;i++)
23     {
24         X=x+xx[i];Y=y+yy[i];
25         if(X<1||X>n||Y<1||Y>m)continue;
26         if(map[X][Y]&&!f[X][Y])dfs(X,Y);
27     }
28 }
29 int main()
30 {
31     n=read();m=read();
32     for(int i=1;i<=n;i++)
33     {
34         scanf("%s",s+1);
35         for(int j=1;j<=m;j++)if(s[j]=='W')map[i][j]=true;
36     }
37     for(int i=1;i<=n;i++)
38         for(int j=1;j<=m;j++)
39         {
40             if(!map[i][j]||f[i][j])continue;
41             ans++;dfs(i,j);
42         }
43     printf("%d",ans);
44     return 0;
45 }
dfs

85.【bzoj1775】[Usaco2009 Dec]Vidgame 电视游戏问题

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #define LL long long
 5 using namespace std;
 6 const int N=55;
 7 const int M=1e5+10;
 8 int n,m,P,G,w,v,f[N][M],g[N][M];
 9 int read()
10 {
11     int x=0,f=1;char c=getchar();
12     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
13     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
14     return x*f;
15 }
16 int main()
17 {
18     memset(f,189,sizeof(f));
19     memset(g,189,sizeof(g));
20     n=read();m=read();
21     for(int i=0;i<=m;i++)f[0][i]=g[0][i]=0;
22     for(int i=1;i<=n;i++)
23     {
24         P=read();G=read();
25         for(int j=0;j<=m;j++)f[i][j]=max(f[i-1][j],g[i-1][j]);
26         for(int j=P;j<=m;j++)g[i][j]=max(f[i-1][j-P],g[i-1][j-P]);
27         while(G--)
28         {
29             w=read();v=read();
30             for(int j=m;j>=w;j--)g[i][j]=max(g[i][j],g[i][j-w]+v);
31         }
32     }
33     printf("%d\n",max(f[n][m],g[n][m]));
34     return 0;
35 }
普通dp

 

posted @ 2017-07-30 00:16  Zsnuo  阅读(777)  评论(0编辑  收藏  举报