BYRBT

Codeforces 348

题解移步至:http://blog.csdn.net/huzecong/article/details/12174359 (斜眼笑)

 

A:

 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<cstring>
 4 
 5 using namespace std;
 6 
 7 int n;
 8 
 9 int main()
10 {
11     scanf("%d",&n);
12     int max=0;
13     long long sum=0;
14     for (int a=1;a<=n;a++)
15     {
16         int v;
17         scanf("%d",&v);
18         if (v>max) max=v;
19         sum+=v;
20     }
21     sum=(sum-1)/(n-1)+1;
22     if (sum>max) printf("%I64d\n",sum);
23     else printf("%d\n",max);
24 
25     return 0;
26 }
View Code

 

B:

 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<cstring>
 4 #include<algorithm>
 5 
 6 using namespace std;
 7 
 8 const int maxn=100010;
 9 const long long INF=100000000000000ll;
10 
11 int n,en,q[maxn],f[maxn];
12 
13 long long z[maxn],minv[maxn],least[maxn];
14 
15 struct edge
16 {
17     int e;
18     edge *next;
19 }*v[maxn],ed[maxn<<1];
20 
21 void add_edge(int s,int e)
22 {
23     en++;
24     ed[en].next=v[s];v[s]=ed+en;v[s]->e=e;
25 }
26 
27 long long gcd(long long a,long long b)
28 {
29     if (!b) return a;
30     else return gcd(b,a%b);
31 }
32 
33 int main()
34 {
35     scanf("%d",&n);
36     long long ans=0;
37     for (int a=1;a<=n;a++)
38         scanf("%I64d",&z[a]),ans+=z[a];
39     for (int a=1;a<n;a++)
40     {
41         int s,e;
42         scanf("%d%d",&s,&e);
43         add_edge(s,e);
44         add_edge(e,s);
45     }
46     int front=1,tail=1;
47     q[1]=1;
48     for (;front<=tail;)
49     {
50         int now=q[front++];
51         for (edge *e=v[now];e;e=e->next)
52             if (e->e!=1 && !f[e->e])
53             {
54                 f[e->e]=now;
55                 q[++tail]=e->e;
56             }
57     }
58     for (int a=n;a>=1;a--)
59     {
60         int now=q[a];
61         minv[now]=INF;
62         least[now]=1;
63         int tot=0;
64         for (edge *e=v[now];e;e=e->next)
65             if (e->e!=f[now])
66             {
67                 tot++;
68                 minv[now]=min(minv[now],minv[e->e]*least[e->e]);
69                 least[now]=least[now]/gcd(least[now],least[e->e])*least[e->e];
70             }
71         if (!tot) 
72         {
73             minv[now]=z[now];
74             continue;
75         }
76         if (least[now]) minv[now]=minv[now]/least[now];
77         else minv[now]=0;
78         least[now]*=tot;
79     }
80     printf("%I64d\n",ans-least[1]*minv[1]);
81 
82     return 0;
83 }
View Code

 

C:

 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<algorithm>
 6 #include<vector>
 7 
 8 using namespace std;
 9 
10 const int maxn=100010;
11 const int maxs=350;
12 
13 int n,m,q,delta[maxn][maxs],num[maxn],to[maxn];
14 
15 long long z[maxn],res[maxs],col[maxs];
16 
17 bool use[maxs][maxn];
18 
19 char s[4];
20 
21 vector<int> y[maxn];
22 
23 int main()
24 {
25     scanf("%d%d%d",&n,&m,&q);
26     for (int a=1;a<=n;a++)
27         scanf("%I64d",&z[a]);
28     int limit=0;
29     for (int a=1;a<=m;a++)
30     {
31         scanf("%d",&num[a]);
32         for (int b=1;b<=num[a];b++)
33         {
34             int v;
35             scanf("%d",&v);
36             y[a].push_back(v);
37         }
38         limit+=num[a];
39     }
40     limit=(int)(sqrt(limit));
41     int cnt=0;
42     for (int a=1;a<=m;a++)
43         if (num[a]>limit)
44         {
45             to[a]=++cnt;
46             for (int b=0;b<num[a];b++)
47                 use[cnt][y[a][b]]=true,res[cnt]+=z[y[a][b]];
48         }
49     for (int a=1;a<=m;a++)
50         for (int b=1;b<=cnt;b++)
51             for (int c=0;c<num[a];c++)
52                 delta[a][b]+=use[b][y[a][c]];
53     for (int a=1;a<=q;a++)
54     {
55         scanf("%s",s);
56         if (s[0]=='?')
57         {
58             int p;
59             scanf("%d",&p);
60             long long ans=0;
61             if (num[p]<=limit)
62             {
63                 for (int b=0;b<num[p];b++)
64                     ans+=z[y[p][b]];
65                 for (int b=1;b<=cnt;b++)
66                     ans+=col[b]*delta[p][b];
67             }
68             else
69             {
70                 ans=res[to[p]];
71                 for (int b=1;b<=cnt;b++)
72                     ans+=col[b]*delta[p][b];
73             }
74             printf("%I64d\n",ans);
75         }
76         else
77         {
78             int p,x;
79             scanf("%d%d",&p,&x);
80             if (num[p]<=limit)
81             {
82                 for (int b=0;b<num[p];b++)
83                     z[y[p][b]]+=x;
84                 for (int b=1;b<=cnt;b++)
85                     res[b]+=(long long)x*delta[p][b];
86             }
87             else col[to[p]]+=x;
88         }
89     }
90 
91     return 0;
92 }
View Code

 

D:

 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<cstring>
 4 
 5 using namespace std;
 6 
 7 #define inc(a,b) {a+=b;if (a>=mo) a-=mo;}
 8 
 9 const int maxn=3010;
10 const int mo=1000000007;
11 
12 int n,m,f[maxn][maxn];
13 
14 char s[maxn][maxn];
15 
16 int solve(int x1,int y1,int x2,int y2)
17 {
18     memset(f,0,sizeof(f));
19     f[x1][y1]=1;
20     for (int a=x1;a<=x2;a++)
21         for (int b=y1;b<=y2;b++)
22             if (s[a][b]=='.')
23             {
24                 inc(f[a+1][b],f[a][b]);
25                 inc(f[a][b+1],f[a][b]);
26             }
27             else f[a][b]=0;
28     return f[x2][y2];
29 }
30 
31 int main()
32 {
33     scanf("%d%d",&n,&m);
34     for (int a=1;a<=n;a++)
35         scanf("%s",s[a]+1);
36     printf("%d\n",(int)((((long long)solve(1,2,n-1,m)*solve(2,1,n,m-1)-(long long)solve(1,2,n,m-1)*solve(2,1,n-1,m))%mo+mo)%mo));
37 
38     return 0;
39 }
View Code

 

 

posted @ 2013-10-03 16:09 zhonghaoxi 阅读(...) 评论(...) 编辑 收藏
BYRBT