cf round428

cf round428

A

题目要求

ai表示每天可以有ai个糖果 要去A需要给Bk个糖果
每天最多只能给8个 问多少天可以给B k个糖果 若不能输出-1

参考AC代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include<bits/stdc++.h>
#define maxn 105
#define mm(a,b) memset(a,b,sizeof(a))
using namespace std;
int a[maxn];
int main(){
// freopen("input.txt","r",stdin);
int n,k;
cin>>n>>k;
for(int i=1;i<=n;i++) cin>>a[i];
int sum=0,left=0,day,flag=0;
for(int i=1;i<=n;i++){
left+=a[i];
if(left>8){
sum+=8;
left-=8;
}
else{
sum+=left;
left=0;
}
if(sum>=k){
day=i;
flag=1;
break;
}
}
if(flag==0) cout<<"-1"<<endl;
else cout<<day<<endl;
return 0;
}

思路

模拟
注意处理好剩下的糖果

B

题目要求

{1, 2}, {3, 4}, {4, 5}, {5, 6} or {7, 8}算是相邻的位置 给出n行
给出k个军队 每个军队有ai个人 相邻的位置不能做不同军队的人 问是否能满足题意

参考AC代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e2 + 17;
int n, k, have[5], cnt[5];
int main(){
ios::sync_with_stdio(0), cin.tie(0);
cin >> n >> k;
have[2] = 2 * n, have[4] = n;
for(int i = 0; i < k; i++){
int x;
cin >> x;
while(x >= 3)
if(have[4] > 0)
x -= 4, have[4]--;
else if(have[2] > 0)
x -= 2, have[2]--;
else
return cout << "NO" << '\n', 0;
if(x > 0)
cnt[x]++;
}
while(cnt[2])
if(have[2] > 0)
cnt[2]--, have[2]--;
else if(have[4] > 0)
cnt[2]--, have[4]--, have[1]++;
else
cnt[2]--, cnt[1] += 2;
if(cnt[1] > have[1] + have[2] + have[4] * 2)
return cout << "NO" << '\n', 0;
cout << "YES" << '\n';
return 0;
}

思路

模拟
把位置转换为4 2 和剩下的即可

C

题目要求

给出一棵树 求树上所有叶子结点深度的期望

参考AC代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include<bits/stdc++.h>
#define maxn 100050
#define mm(a,b) memset(a,b,sizeof(a))
using namespace std;
vector<int>e[maxn];
int du[maxn];
double ans;
void dfs(int x,int fa,double epx,int deep){
int len=e[x].size();
for(int i=0;i<len;i++){
int next=e[x][i];
if(next==fa) continue;
if(du[next]==1) ans+=epx*(deep+1);
dfs(next,x,epx*1.0/(du[next]-1),deep+1);
}
}
int main(){
// freopen("input.txt","r",stdin);
int n; cin>>n;
for(int i=0;i<n;i++) e[i].clear();
mm(du,0);
for(int i=1;i<=n-1;i++){
int u,v;
cin>>u>>v;
e[u].push_back(v);
e[v].push_back(u);
du[u]++,du[v]++;
}
dfs(1,0,1.0/du[1],0);
printf("%.6lf\n",ans);
return 0;
}

思路

dfs里维护一个概率即可 du判断是否是叶子结点

E

转跳链接

文章目录
  1. 1. cf round428
    1. 1.1. A
      1. 1.1.1. 题目要求
      2. 1.1.2. 参考AC代码
      3. 1.1.3. 思路
    2. 1.2. B
      1. 1.2.1. 题目要求
      2. 1.2.2. 参考AC代码
      3. 1.2.3. 思路
    3. 1.3. C
      1. 1.3.1. 题目要求
      2. 1.3.2. 参考AC代码
      3. 1.3.3. 思路
    4. 1.4. E
|