# 屯题大法好

CF 535C Tavas and Karafs

 1 #include<iostream>
2 #include<cstdio>
3 #define ll long long
4 using namespace std;
5 ll a,b,n,l,t,m,rr,lr;
6 int check(ll k)
7 {
8     ll f = a + (l - 1) * b,rrr = a + (k - 1) * b,su=((f+rrr)*(k-l+1))>>1;
9     if(t*m<su || rrr > t)return 0;
10     else return 1;
11 }
12 int main()
13 {
14     cin>>a>>b>>n;
15     for(int i=1;i<=n;i++)
16     {
17         cin>>l>>t>>m;
18         if(a+b*(l-1) > t)
19         {
20             puts("-1");
21             continue;
22         }
23         lr = l;
24         rr = l + t+10;
25         while(lr<rr)
26         {
27             int mid = (lr + rr+ 1) >> 1;
28             if(check(mid))lr=mid;else rr=mid-1;
29         }
30         cout<<lr<<endl;
31     }
32     return 0;
33 }
View Code

CF 533B. Work Group

 1 #include<cstdio>
2 #include<iostream>
3 #include<cstring>
4 #define maxn 400000
5 using namespace std;
7 long long dp[400000][3],root;
9 {
12     point[now]=y;
13 }
14 long long dfs(long long k,long long par)
15 {
16     if(dp[k][par]!=-1)return dp[k][par];
17     long long even = 0 ,odd = -1;
19     {
20         long long pp = point[i],u=dfs(pp,0) , v = dfs(pp,1);
21         long long tempe = even , tempo = odd;
22         even += u;odd += u;
23         if(v != -1)
24         {
25             if(tempo != -1)even = max(even , tempo + v);
26             odd = max(odd , tempe +v);
27         }
28     }
29     dp[k][0]=even;
30     dp[k][1] = max(even + p[k], odd);
31     return dp[k][par];
32 }
33 int main()
34 {
35     long long n,x;
36     memset(dp,-1,sizeof(dp));
37     cin>>n;
38     for(int i=1;i<=n;i++)
39     {
40         cin>>x>>p[i];
41         if(x==-1)root=i;
42         else
43         {
45         }
46     }
47     cout<<max(dfs(root,0),dfs(root,1))<<endl;
48     return 0;
49 }
View Code

GCJ 2015 A. Counter Culture

 1 #include<iostream>
2 #include<fstream>
3 #include<cstring>
4 #include<algorithm>
5 #define maxn 500
6 using namespace std;
7 ifstream fin("a.in");
8 ofstream fout("a.out");
9 //#define fin cin
10 //#define fout cout
11 int deg[100]={0,1,10,29,138,337,1436,3435,14434,34433,144432,344431,1444430,3444429,14444428};
12 int main()
13 {
14     long long t,n,cas=0;
15     fin>>t;
16     while(t--)
17     {
18         long long ans=0;
19         fin>>n;
20         int flag=0;
21         if(n<=20)
22         {
23             fout<<"Case #"<<++cas<<": "<<n<<endl;
24         }
25         else
26         {
27             long long temp=n;
28             if(n%10==0)
29             {
30                 n--;
31                 flag=1;
32             }
33             int a[100],h=0;
34             while(n)
35             {
36                 a[++h]=n%10;
37                 n/=10;
38             }
39             int u=h>>1;
40             ans += deg[h];
41             long long v=0;
42             for(int i=h-u+1;i<=h;i++)
43             {
44                 v = v * 10 + a[i];
45             }
46             ans  += v;
47             v=0;
48             for(int i=h-u;i>=1;i--)
49             {
50                 v= v*10 +a[i];
51             }
52             ans+=v;
53             v=1;
54             for(int i=1;i<h;i++)v*=10;
55             if(flag)ans++;
56             fout<<"Case #"<<++cas<<": "<<min(ans,temp-v+deg[h])<<endl;
57         }
58     }
59     return 0;
60 }
View Code

Codeforces Round #300 B. Quasi Binary

 1 #include<iostream>
2 #include<cstdio>
3 #include<cstring>
4 #include<algorithm>
5 #define maxn 2000000
6 using namespace std;
7 int a[maxn],h=0,ini[maxn],dp[maxn],last[maxn],ans[maxn];
9 {
10     int sum=0;
11     for(int i=1;i<=7;i++)sum = sum*10 + temp[i];
12     ini[++h]=sum;
13 }
14 void dfs(int k)
15 {
17     a[k]=1;dfs(k+1);
18     a[k]=0;dfs(k+1);
19 }
20 int dfs2(int k)
21 {
22     if(dp[k]!=-1)return dp[k];
23     int ret=0x3f3f3f3f;
24     for(int i=1;i<=h;i++)
25     {
26         if(k-ini[i]>=0)
27         {
28             int u=dfs2(k-ini[i]);
29             if(u < ret)
30             {
31                 ret = u;
32                 last[k] = i;
33             }
34         }
35         else break;
36     }
37     return dp[k]=ret+1;
38 }
39 int main()
40 {
41     int n;
42     scanf("%d",&n);
43     dfs(1);
44     memset(dp,-1,sizeof(dp));
45     dp[0]=0;
46     h--;
47     sort(ini+1,ini+1+h);
48     printf("%d\n",dfs2(n));
49     int v=0;
50     while(last[n]!=0)
51     {
52         ans[++v]=ini[last[n]];
53         n-=ini[last[n]];
54     }
55     sort(ans+1,ans+1+v);
56     for(int i=1;i<=v;i++)printf("%d ",ans[i]);
57     return 0;
58 }
View Code

Codeforces Round #298 (Div. 2)  F. Simplified Nonogram

 1 #include<iostream>
2 #include<cstdio>
3 #include<cstring>
4 #include<vector>
5 #define maxn 35
6 using namespace std;
7 int a[6],b[maxn],c[maxn],n,m,g[11][50];
8 int ans[maxn][maxn],next[maxn];
9 bool visit[21][11][11][11][11][11][33];
10 bool dfs(int k,int last)
11 {
12     if(k>m)
13     {
14         for(int i=1;i<=n;i++)if(a[i]!=0)return false;
15         return true;
16     }
17     int temp[6];
18     if(visit[k][a[1]][a[2]][a[3]][a[4]][a[5]][last])return false;
19     for(int i=1;i<=g[b[k]][0];i++)
20     {
21         int u=g[b[k]][i],flag2=0;
22         memcpy(temp,a,sizeof(a));
23         for(int j=0;j<n;j++)
24         {
25             if( (u&(1<<j))!=0 && (last& (1<<j))==0 )
26             {
27                 a[j+1]--;
28                 if(a[j+1]<0){flag2=1;break;}
29             }
30         }
31         if(flag2)
32         {
33             memcpy(a,temp,sizeof(a));
34             continue;
35         }
36         if(dfs(k+1,u))
37         {
38             next[k]=u;
39             return true;
40         }
41         memcpy(a,temp,sizeof(a));
42     }
43     visit[k][a[1]][a[2]][a[3]][a[4]][a[5]][last]=1;
44     return false;
45 }
46 int main()
47 {
48     scanf("%d%d",&n,&m);
49     for(int i=1;i<=n;i++)scanf("%d",&a[i]);
50     for(int i=1;i<=m;i++)scanf("%d",&b[i]);
51     for(int s=0;s<=((1<<n)-1);s++)
52     {
53         int v=0;
54         for(int i=1;i<=n;i++)
55         {
56             if(((s>>(i-1)) & 3) == 1)v++;
57         }
58         g[v][++g[v][0]]=s;
59     }
60     dfs(1,0);
61     for(int i=1;i<=m;i++)
62         for(int j=1;j<=n;j++)
63         {
64             if(next[i]&(1<<(j-1)))ans[j][i]=1;else ans[j][i]=0;
65         }
66     for(int i=1;i<=n;i++)
67         for(int j=1;j<=m;j++)
68         {
69             if(ans[i][j])printf("*");else printf(".");
70         }
71         puts("");
72     return 0;
73 }
View Code

Codeforces Round #290 (Div. 2) D. Fox And Jumping

 1 #include<iostream>
2 #include<cstdio>
3 #include<algorithm>
4 #include<map>
5 #include<queue>
6 #define maxn 1000
7 using namespace std;
8 map<int,int>h;
9 queue<int>q;
10 int l[maxn],c[maxn];
11 int main()
12 {
13     int n;
14     scanf("%d",&n);
15     for(int i=1;i<=n;i++)scanf("%d",&l[i]);
16     for(int i=1;i<=n;i++)
17     {
18         scanf("%d",&c[i]);
19         if(h[l[i]]==0)
20         {
21             q.push(l[i]);
22             h[l[i]]=c[i];
23         }
24         else
25         {
26             h[l[i]]=min(h[l[i]],c[i]);
27         }
28     }
29     while(!q.empty())
30     {
31         int u=q.front();
32         q.pop();
33         for(int i=1;i<=n;i++)
34         {
35             int v=__gcd(l[i],u),w=h[u]+h[l[i]];
36             if(h[v]>w || h[v]==0)
37             {
38                 h[v]=w;
39                 q.push(v);
40             }
41         }
42     }
43     if(h[1]==0)
44     {
45         puts("-1");
46     }
47     else printf("%d\n",h[1]);
48     return 0;
49 }
View Code

Codeforces Round #300 E. Demiurges Play Again

 1 #include<iostream>
2 #include<cstdio>
3 #include<cstring>
4 #define maxn 400009
5 using namespace std;
7 bool visit[maxn];
9 {
12     point[now]=y;
13 }
14 int dfs(int k,bool f)
15 {
16     visit[k]=1;
17     int minx=0x3f3f3f3f,ret=0,flag=1;
19     {
20         flag=0;
21         int u=point[i];
22         if(f)
23         {
24             minx=min(minx,dfs(u,f^1));
25         }
26         else
27         {
28             ret+=dfs(u,f^1);
29         }
30     }
31     if(flag)
32     {
33         leaf++;
34         return 1;
35     }
36     if(f)return minx;else return ret;
37 }
38 int main()
39 {
40     int n,x,y;
41     scanf("%d",&n);
42     for(int i=1;i<n;i++)
43     {
44         scanf("%d%d",&x,&y);
47     }
48     int u=dfs(1,1);
49     leaf=0;
50     memset(visit,0,sizeof(visit));
51     int v = dfs(1,0);
52     printf("%d %d\n",leaf-u+1,v);
53     return 0;
54 }
View Code

Codeforces Round #301 (Div. 2)  D. Bad Luck Island

 1 #include<iostream>
2 #include<cstring>
3 #include<cstdio>
4 using namespace std;
5 double dp[109][109][109];
6 bool visit[109][109][109];
7 double dfs(int r,int s,int p)
8 {
9     if(r==0)return 0.0;
10     if(s==0 && p==0)return 1.0;
11     if(visit[r][s][p])return dp[r][s][p];
12     visit[r][s][p]=1;
13     int tot = r*s + r*p + s*p;
14     dp[r][s][p]=0;
15     if(s)dp[r][s][p]+=1.0*dfs(r,s-1,p) * (1.0 * r * s) / (1.0*tot);
16     if(r)dp[r][s][p]+=1.0*dfs(r-1,s,p) * (1.0 * r * p) / (1.0*tot);
17     if(p)dp[r][s][p]+=1.0*dfs(r,s,p-1) * (1.0 * s * p) / (1.0*tot);
18     return dp[r][s][p];
19 }
20 int main()
21 {
22     int r,s,p;
23     scanf("%d%d%d",&r,&s,&p);
24     printf("%.12f %.12f %.12f\n",dfs(r,s,p),dfs(s,p,r),dfs(p,r,s));
25     return 0;
26 }
View Code

Codeforces Round #296 (Div. 2)  D. Clique Problem

xi- xj <=  wi +ｗｊ ||　　xi- xj <=  wi +ｗｊ

 1 #include<iostream>
2 #include<cstdio>
3 #include<algorithm>
4 #define maxn 200000
5 #define inf 0x3f3f3f3f
6 using namespace std;
7 struct T
8 {
9     int x;
10     int y;
11 }a[maxn];
12 int cmp(T x,T y)
13 {
14     return x.x < y.x;
15 }
16 int x,w;
17 int
18 main()
19 {
20     int n;
21     scanf("%d",&n);
22     for(int i=1;i<=n;i++)
23     {
24         scanf("%d%d",&x,&w);
25         a[i].x = x + w;
26         a[i].y = x - w;
27     }
28     sort(a+1, a+1+n,cmp);
29     int maxx = -inf,ans = 0;
30     for(int i=1;i<=n;i++)
31     {
32         if(a[i].y >= maxx)
33         {
34             maxx = a[i].x;
35             ans++;
36         }
37     }
38     printf ("%d\n",ans);
39     return 0;
40 }
View Code

posted @ 2015-05-03 17:04  philippica  阅读(278)  评论(0编辑  收藏  举报