Codeforces Round #428 (Div. 2)

A

题意:给糖果每天最多给8个,给出每天给的个数,问k个糖果最少多少天给完,可以累计,比如我第一天给10个,但我最多可以给8个,另外2个可以累计到后面,如果第二天是6个,但是我可以给8个,就这吊毛意思

思路:暴力模拟

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int a[102];
 4 int main(){
 5     int n,k;
 6     cin>>n>>k;
 7     for(int i=1;i<=n;i++){
 8         scanf("%d",&a[i]);
 9     }
10     int ss=0;
11     for(int i=1;i<=n;i++){
12         if(a[i]<8) {
13                 k-=a[i];
14                 k-=min(ss,8-a[i]);
15                 ss=max(0,ss-(8-a[i]));
16         }
17         else {
18             k-=8;ss+=(a[i]-8);
19         }
20         if(k<=0){
21             cout<<i<<endl;return 0;
22         }
23     }
24     cout<<-1<<endl;
25 }

B

题意:每排有 {1, 2}, {3, 4}, {4, 5}, {5, 6} or {7, 8}.的相邻位置,相邻位置不能坐不同组的军人,问是否可行

思路:肯定是先把中间的四个位置能坐就坐,然后中间最多坐3个,2边还是2个,即能坐2个人的位置s2个,能坐一个人的位置s1个,注意:当我s1不够,我一个人可以消耗一个2个人的位置,s2--,当我s2不够的话,我可以消耗2个s1的位置

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 
 4 int a[103];
 5 
 6 int main(){
 7     int n,k;
 8     scanf("%d%d",&n,&k);
 9     int m=1;
10     for(int i=1;i<=k;i++){
11          scanf("%d",&a[i]);
12          while(a[i]>=4&&m<=n){
13              a[i]-=4;
14              m++;
15          }
16 
17     }
18     int s2=n*2+(n-m+1);
19     int s1=(n-m+1);
20    // cout<<s1<<" "<<s2<<endl;
21     for(int i=1;i<=k;i++){
22 
23         if(a[i]==0) continue;
24         else {
25             int x=a[i]/2;
26             if(s2!=0){
27                 if(s2>x) s2-=x;
28                 else {
29                     x-=s2;s2=0;
30                 }
31             }
32             if(s2==0){
33                 s1-=2*x;
34             }
35             if(a[i]%2==1) {
36 
37                 if(s1==0) s2--;
38                 else s1--;
39            // cout<<a[i]<<" "<<s1<<" "<<s2<<endl;
40             }
41         }
42         if(s1<0||s2<0){
43             cout<<"NO"<<endl;return 0;
44         }
45     }
46     cout<<"YES"<<endl;
47 }

C

题意:一棵树,从1号节点出发,等概率到他的子节点,只能走没到过的地方,会一直走,到不能走为止,问距离的期望是多少

思路:跑一遍就行了,记得不要看见函数不加return 心里就不舒服

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 
 4 vector<int > a[100005];
 5 int n;
 6 double sum;
 7 
 8 void dfs(int u,int fa,double y,int d){
 9     if(a[u].size()==1){
10        // printf("%d %.4lf\n",d,y);
11         sum+=y*d;
12        // return ; //就是这个return 啊,WA到死啊
13     }
14     for(int i=0;i<a[u].size();i++){
15         int v=a[u][i];
16         if(v==fa) continue;
17         if(u==1) dfs(v,u,y/a[u].size(),d+1);
18         else
19         dfs(v,u,y/(a[u].size()-1),d+1);
20     }
21 }
22 
23 int main(){
24     scanf("%d",&n);
25     int x,y;
26         for(int i=1;i<n;i++){
27         scanf("%d%d",&x,&y);
28         a[x].push_back(y);
29         a[y].push_back(x);
30     }
31     dfs(1,0,1.0,0);
32     printf("%.6lf\n",sum);
33 }

 

posted on 2017-08-13 10:00  hhhhx  阅读(123)  评论(0编辑  收藏  举报

导航