Codeforces Round #359 (Div. 2) A ~D
来源:互联网 发布:淘宝店转让多少钱 编辑:程序博客网 时间:2024/04/27 15:08
官方题解:http://codeforces.com/blog/entry/45558
A
A. Free Ice Cream
根据题意模拟一下
#include<bits/stdc++.h>using namespace std;typedef long long ll;int main(){ int n;ll num; while(~scanf("%d %I64d",&n,&num)){ int ans=0; for(int i=0;i<n;i++){ char str[3];int s; scanf("%s %d",str,&s); if(str[0]=='-'){ if(num<s) ans++; else num-=s; } else num+=s; } printf("%I64d %d\n",num,ans); }}
B. Little Robber Girl’s Zoo
这题范围100,模拟的过程刚好冒泡排序满足。
而且冒泡解决最多100*99/2,也满足题目要求不超过20000。
#include<bits/stdc++.h>using namespace std;int n,a[110];int main(){ int n; while(~scanf("%d",&n)){ for(int i=1;i<=n;i++) scanf("%d",&a[i]); for(int i=1;i<=n;i++){ for(int j=1;j<=n-i;j++){ if(a[j]>a[j+1]){ printf("%d %d\n",j,j+1); swap(a[j],a[j+1]); } } } }}
C. Robbers’ watch
题意:有一个手表,他显示数字是七进制(0~6),给你小时,分钟n,m代表一天的周期,问一天有多少个时间所有数字不同。
思路:首先n,m的范围是10^9,但是这里七进制每一位不同最多七位数字,所以大于这个范围都是0.
只有七位,然后我的做法是全排列7!之后在保留满足小于n,和m的。
#include<bits/stdc++.h>using namespace std;set<long long>s;int cal(int x)//计算x转换成7进制有多少位{ int t=0; if(x==0) return 1;//带入x-1 while(x){t++;x/=7;} return t;}int main(){ int n,m; while(~scanf("%d%d",&n,&m)){ int num=cal(n-1)+cal(m-1); if(num>7){ printf("0\n"); continue; } int pre=cal(n-1),post=cal(m-1); int a[7]; s.clear(); for(int i=0;i<7;i++) a[i]=i; do{ int b[2]={0,0}; for(int i=0;i<pre;i++) b[0]=b[0]*7+a[i]; for(int i=pre;i<pre+post;i++) b[1]=b[1]*7+a[i]; if(b[0]<n&&b[1]<m) s.insert(b[0]*10000000+b[1]); }while(next_permutation(a,a+7)); printf("%d\n",s.size()); }}
D. Kay and Snowflake
题意:在一个有n个节点有向有根树上,有m个询问,问v节点以及他的子孙构成子树的中心是哪个节点,中心满足的条件是去掉这个节点后,这个子树分裂最大的一块大小不大于原来的1/2.
思路:首先确认每个子树的中心的那个节点下的子树一定是大于等于子树1/2.第二,他也必须是子树最小的的满足这个条件的节点。
我的代码就是根据这两点搜索。
#include<bits/stdc++.h>using namespace std;const int maxn=300000+10;vector<int>E[maxn];int ans[maxn],Size[maxn],father[maxn];//Size[i]i节点的子树大小void dfs(int x){ Size[x]=1; if(E[x].empty()){ ans[x]=x; return; } for(int i=0;i<E[x].size();i++){ int now=E[x][i]; dfs(now); Size[x]+=Size[now]; if(Size[now]>Size[ans[x]]) ans[x]=now; } ans[x]=ans[ans[x]]; while(Size[x]>Size[ans[x]]*2) ans[x]=father[ans[x]];}int main(){ int n,m; while(~scanf("%d%d",&n,&m)){ for(int i=0;i<=n;i++) E[i].clear(); memset(Size,0,sizeof(Size)); for(int i=2;i<=n;i++){ scanf("%d",&father[i]); E[father[i]].push_back(i); } dfs(1); while(m--){ int x;scanf("%d",&x); printf("%d\n",ans[x]); } }}
目标:这个暑假cf要上紫!
0 0
- Codeforces Round #359 (Div. 2) A ~D
- Codeforces Round #209 (Div. 2) <A-D>
- Codeforces Round #231 (Div. 2)A-D
- Codeforces Round #235 (Div. 2) A~D
- Codeforces Round #237 (Div. 2) A~D
- Codeforces Round #240 (Div. 2) A~D
- Codeforces Round #239 (Div. 2) A~D
- Codeforces Round #242 (Div. 2) <A-D>
- Codeforces Round #250 (Div. 2) A-D
- Codeforces Round #256 (Div. 2)A-D
- Codeforces Round #263 (Div. 2) A-D
- Codeforces Round #269 (Div. 2) A~D
- Codeforces Round #358 (Div. 2)A~D
- Codeforces Round #364 (Div. 2)(A ~ D)
- Codeforces Round #368 (Div. 2) A~D
- Codeforces Round #369 (Div. 2) A~D
- Codeforces Round #369 (Div. 2) A~D
- Codeforces Round #374 (Div. 2) A~D
- VIM 常用快捷键
- QT-创建具有复选框的树形控件
- android json解析
- 代码中的常见的问题总结
- [111] Minimum Depth of Binary Tree
- Codeforces Round #359 (Div. 2) A ~D
- Android studio如何导出.so库(NDK开发入门)
- 风格style与主题theme(一)-style、theme的关系与使用
- 圆圈中最后剩下的数
- POJ 3074&&3076 舞蹈链
- 小飞的电梯调度法
- spring配置文件加载FTP服务器上的属性文件
- Linux学习之用户管理命令
- assert()函数用法总结