A - Sum in the tree

就是贪心选尽量让上面的点权尽量大,那么对于偶数层的点,其到根节点的和即为所有儿子中的最大值。

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. char gc() {
  4. // static char buf[100000],*p1,*p2;
  5. // return (p1==p2)&&(p2=(p1=buf)+fread(buf,1,100000,stdin))?EOF:*p1++;
  6. return getchar();
  7. }
  8. template<class T>
  9. int read(T &ans) {
  10. T f=1;ans=0;
  11. char ch=gc();
  12. while(!isdigit(ch)) {
  13. if(ch==EOF) return EOF;
  14. if(ch=='-') f=-1;
  15. ch=gc();
  16. }
  17. while(isdigit(ch))
  18. ans=ans*10+ch-'0',ch=gc();
  19. ans*=f;return 1;
  20. }
  21. template<class T1,class T2>
  22. int read(T1 &a,T2 &b) {
  23. return read(a)==EOF?EOF:read(b);
  24. }
  25. template<class T1,class T2,class T3>
  26. int read(T1 &a,T2 &b,T3 &c) {
  27. return read(a,b)==EOF?EOF:read(c);
  28. }
  29. typedef long long ll;
  30. const int Maxn=1100000;
  31. const int inf=0x3f3f3f3f;
  32. const ll mod=1000000007;
  33. int s[Maxn],to[Maxn],nxt[Maxn],first[Maxn],tot=1;
  34. int n,x,flag;
  35. ll ans;
  36. inline void add(int u,int v) {
  37. to[tot]=v;
  38. nxt[tot]=first[u];
  39. first[u]=tot++;
  40. }
  41. void dfs(int root) {
  42. if(s[root]==-1) {
  43. int temp=inf;
  44. for(int i=first[root];i;i=nxt[i]) {
  45. dfs(to[i]);
  46. temp=min(temp,s[to[i]]);
  47. }
  48. s[root]=temp;
  49. for(int i=first[root];i;i=nxt[i]) {
  50. ans+=s[to[i]]-s[root];
  51. }
  52. }
  53. else {
  54. for(int i=first[root];i;i=nxt[i]) {
  55. dfs(to[i]);
  56. if(s[to[i]]==inf) s[to[i]]=s[root];
  57. if(s[to[i]]<s[root]) flag=1;
  58. ans+=s[to[i]]-s[root];
  59. }
  60. }
  61. }
  62. int main() {
  63. read(n);
  64. for(int i=2;i<=n;i++) {
  65. read(x);
  66. add(x,i);
  67. }
  68. for(int i=1;i<=n;i++) read(s[i]);
  69. dfs(1);ans+=s[1];
  70. if(flag) return 0*puts("-1");
  71. printf("%I64d",ans);
  72. return 0;
  73. }

B - Nice table

这个结论就是要么是对于每一行都只有两种字符或每一列都只有两种字符。至于为什么。。自己感性理解一下就好了。

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<algorithm>
  4. #include<queue>
  5. #include<cctype>
  6. #include<string>
  7. #include<iostream>
  8. #define qmin(x,y) (x=min(x,y))
  9. #define qmax(x,y) (x=max(x,y))
  10. using namespace std;
  11. inline char gc() {
  12. // static char buf[100000],*p1,*p2;
  13. // return (p1==p2)&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
  14. return getchar();
  15. }
  16. template<class T>
  17. int read(T &ans) {
  18. ans=0;char ch=gc();T f=1;
  19. while(!isdigit(ch)) {
  20. if(ch==EOF) return -1;
  21. if(ch=='-') f=-1;
  22. ch=gc();
  23. }
  24. while(isdigit(ch))
  25. ans=ans*10+ch-'0',ch=gc();
  26. ans*=f;return 1;
  27. }
  28. template<class T1,class T2>
  29. int read(T1 &a,T2 &b) {
  30. return read(a)!=EOF&&read(b)!=EOF?2:EOF;
  31. }
  32. template<class T1,class T2,class T3>
  33. int read(T1 &a,T2 &b,T3 &c) {
  34. return read(a,b)!=EOF&&read(c)!=EOF?3:EOF;
  35. }
  36. typedef long long ll;
  37. const int Maxn=310000;
  38. const int inf=0x3f3f3f3f;
  39. char t[4]={'A','T','C','G'};
  40. string a[Maxn],b[Maxn];
  41. int w[Maxn][4][4],w1[Maxn][4][4];
  42. int p[4],p1[4],p2[4],n,m;
  43. inline bool check() {
  44. return p[1]!=1||p[2]!=2||p[3]!=3||p[0]!=0;
  45. }
  46. int calc() {
  47. int ans=0;
  48. for(int i=0,t=2;i<m;i++,t^=2)
  49. ans+=min(w[i][p[0+t]][p[1+t]],w[i][p[1+t]][p[0+t]]);
  50. return ans;
  51. }
  52. signed main() {
  53. // freopen("test.in","r",stdin);
  54. read(n,m);
  55. for(int i=0;i<n;i++) cin>>a[i];
  56. for(int i=0;i<m;i++)
  57. for(int x=0;x<4;x++)
  58. for(int y=0;y<4;y++)
  59. for(int j=0;j<n;j++)
  60. if(j&1) w[i][x][y]+=(t[y]!=a[j][i]);
  61. else w[i][x][y]+=(t[x]!=a[j][i]);
  62. int ans1=inf,ans2=inf;
  63. for(int i=0;i<4;i++) p[i]=i;
  64. do {
  65. int temp=calc();
  66. if(temp<ans1) {
  67. ans1=temp;
  68. memcpy(p1,p,sizeof(p));
  69. }
  70. }
  71. while(next_permutation(p,p+4),check());
  72. for(int i=0;i<m;i++) b[i].resize(n);
  73. for(int i=0;i<n;i++)
  74. for(int j=0;j<m;j++) b[j][i]=a[i][j];
  75. memcpy(w1,w,sizeof(w1));
  76. memset(w,0,sizeof(w));
  77. swap(n,m);
  78. for(int i=0;i<m;i++)
  79. for(int x=0;x<4;x++)
  80. for(int y=0;y<4;y++)
  81. for(int j=0;j<n;j++)
  82. if(j&1) w[i][x][y]+=(t[y]!=b[j][i]);
  83. else w[i][x][y]+=(t[x]!=b[j][i]);
  84. do {
  85. int temp=calc();
  86. if(temp<ans2) {
  87. ans2=temp;
  88. memcpy(p2,p,sizeof(p));
  89. }
  90. }
  91. while(next_permutation(p,p+4),check());
  92. swap(n,m);
  93. if(ans1<ans2)
  94. for(int i=0;i<n;i++,puts(""))
  95. for(int j=0;j<m;j++)
  96. if(j&1) putchar(t[p1[(w1[j][p1[0]][p1[1]]>w1[j][p1[1]][p1[0]])^(i&1)]]);
  97. else putchar(t[p1[(w1[j][p1[2]][p1[3]]>w1[j][p1[3]][p1[2]])^(i&1)+2]]);
  98. else
  99. for(int i=0;i<n;i++,puts(""))
  100. for(int j=0;j<m;j++)
  101. if(i&1) putchar(t[p2[(w[i][p2[0]][p2[1]]>w[i][p2[1]][p2[0]])^(j&1)]]);
  102. else putchar(t[p2[(w[i][p2[2]][p2[3]]>w[i][p2[3]][p2[2]])^(j&1)+2]]);
  103. return 0;
  104. }

C - Construct a tree

首先就是分叉数越大,其对应的所有子树的大小和越小。那么依次枚举判断,如果合法构造即可。

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<algorithm>
  4. #include<queue>
  5. #include<cctype>
  6. #define qmin(x,y) (x=min(x,y))
  7. #define qmax(x,y) (x=max(x,y))
  8. using namespace std;
  9. inline char gc() {
  10. // static char buf[100000],*p1,*p2;
  11. // return (p1==p2)&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
  12. return getchar();
  13. }
  14. template<class T>
  15. int read(T &ans) {
  16. ans=0;char ch=gc();T f=1;
  17. while(!isdigit(ch)) {
  18. if(ch==EOF) return -1;
  19. if(ch=='-') f=-1;
  20. ch=gc();
  21. }
  22. while(isdigit(ch))
  23. ans=ans*10+ch-'0',ch=gc();
  24. ans*=f;return 1;
  25. }
  26. template<class T1,class T2>
  27. int read(T1 &a,T2 &b) {
  28. return read(a)!=EOF&&read(b)!=EOF?2:EOF;
  29. }
  30. template<class T1,class T2,class T3>
  31. int read(T1 &a,T2 &b,T3 &c) {
  32. return read(a,b)!=EOF&&read(c)!=EOF?3:EOF;
  33. }
  34. typedef long long ll;
  35. const int Maxn=110000;
  36. const int inf=0x3f3f3f3f;
  37. int n,a[Maxn],p[Maxn];
  38. ll s;
  39. ll check(int x) {
  40. ll res=n,temp=1,ans=0;
  41. for(int i=1;res;i++) {
  42. qmin(temp,res);
  43. ans+=temp*i;
  44. res-=temp;
  45. temp*=x;
  46. }
  47. return ans;
  48. }
  49. void work(int x) {
  50. a[1]=1;s--;
  51. ll res=n-1,temp=x,t=2;
  52. for(;res;t++) {
  53. qmin(temp,res);
  54. s-=temp*t;
  55. a[t]=temp;
  56. res-=temp;
  57. temp*=x;
  58. }
  59. for(int i=t-1;s;i--) {
  60. while(a[i]>1) {
  61. a[i]--;
  62. s+=i;
  63. if(s>t) {
  64. s-=t;
  65. a[t++]++;
  66. }
  67. else {
  68. a[s]++;
  69. s=0;
  70. break;
  71. }
  72. }
  73. }
  74. int sum=0,tempp=1;
  75. for(int i=1;i<t;i++) {
  76. int temp=sum+1;
  77. sum=tempp;
  78. for(int j=temp;j<=sum;j++)
  79. for(int k=min(a[i+1],x);k;k--) {
  80. p[++tempp]=j;
  81. a[i+1]--;
  82. }
  83. }
  84. for(int i=2;i<=n;i++) printf("%d ",p[i]);
  85. }
  86. signed main() {
  87. // freopen("test.in","r",stdin);
  88. read(n,s);
  89. if(1ll*n*(n+1)/2==s) {
  90. puts("Yes");
  91. for(int i=2;i<=n;i++) printf("%d ",i-1);
  92. return 0;
  93. }
  94. if(1ll*n*(n+1)/2<s||2*n-1>s) {
  95. puts("No");
  96. return 0;
  97. }
  98. for(int i=2;i<=n;i++) if(check(i)<=s) {
  99. puts("Yes");
  100. work(i);
  101. return 0;
  102. }
  103. return 0;
  104. }

Codeforces Round #530 (Div. 1)的更多相关文章

  1. Codeforces Round #530 (Div. 2) F (树形dp+线段树)

    F. Cookies 链接:http://codeforces.com/contest/1099/problem/F 题意: 给你一棵树,树上有n个节点,每个节点上有ai块饼干,在这个节点上的每块饼干 ...

  2. Codeforces Round #530 (Div. 2) A,B,C,D

    A. Snowball 链接:http://codeforces.com/contest/1099/problem/A 思路:模拟 代码: #include<bits/stdc++.h> ...

  3. Codeforces Round #530 (Div. 2):D. Sum in the tree (题解)

    D. Sum in the tree 题目链接:https://codeforces.com/contest/1099/problem/D 题意: 给出一棵树,以及每个点的si,这里的si代表从i号结 ...

  4. Codeforces Round #530 (Div. 2) F 线段树 + 树形dp(自下往上)

    https://codeforces.com/contest/1099/problem/F 题意 一颗n个节点的树上,每个点都有\(x[i]\)个饼干,然后在i节点上吃一个饼干的时间是\(t[i]\) ...

  5. Codeforces Round #530 (Div. 2)F Cookies (树形dp+线段树)

    题:https://codeforces.com/contest/1099/problem/F 题意:给定一个树,每个节点有俩个信息x和t,分别表示这个节点上的饼干个数和先手吃掉这个节点上一个饼干的的 ...

  6. Codeforces Round #530 (Div. 2) C D

    C: *可以保留删除或者增加 ? 保留或者删除 #include<bits/stdc++.h> using namespace std; int main(){ string s; int ...

  7. Codeforces Round #530 (Div. 2)

    RANK :2252 题数 :3 补题: D - Sum in the tree 思路:贪心 把权值放在祖先节点上 ,预处理 每个节点保存 他与他儿子中 权值最小值即可. 最后会有一些叶子节点依旧为 ...

  8. Codeforces Round #530 Div. 1 自闭记

    A:显然应该让未确定的大小尽量大.不知道写了啥就wa了一发. #include<iostream> #include<cstdio> #include<cmath> ...

  9. Codeforces Round #530 (Div. 2) F - Cookies

    F - Cookies 思路:我们先考虑如何算出在每个节点结束最多能吃多少饼干, 这个dfs的时候用线段树维护一下就好了, 然后有个这个信息之后树上小dp一下就好啦. #include<bits ...

随机推荐

  1. 64位windows 7下配置TortoiseGit(转)

    原文:http://our2848884.blog.163.com/blog/static/146854834201152325233854/ 最近感觉自己电脑上的代码太乱了,东一块.西一块……于是决 ...

  2. flask-WTForms组件

    WTForms是一个支持多个web框架的form组件 主要能够帮助我们生成html标签 对数据进行验证 安装 pip install wtforms Wtforms的使用 这里借助一个用户登录注册的示 ...

  3. js与jQuery差别

    jQuery能大大简化Javascript程序的编写,我近期花时间了解了一下jQuery.把我上手过程中的笔记和大家分享出来.希望对大家有所帮助. 要使用jQuery.首先要在HTML代码最前面加上对 ...

  4. 【剑指offer】斐波那契数列

    一.题目: 大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项.n<=39 二.思路: 式子: n=0时,f=0:n=1或者n=2时f=1:否则f=f(n-1)+f(n ...

  5. ABP项目创建

    第一种:1.在MyAbp.Migrator下面的appsettings.json里面的sql连接语句 MyAbp.Web.Host 下面的appsettings.json 里面的连接语句2.把MyAb ...

  6. 在ios上时间无法parse返回 "Invalid Date"

    百度搜索:" ios 上面 new date" 关键字 BUG 描述直接上原码 var psrseDate = Date.parse("2010-03-15 10:30: ...

  7. AngularJS2.0起步

    ES6工具链 要让Angular2应用跑起来不是件轻松的事,因为它用了太多还不被当前主流浏览器支持的技术.所以,我们需要一个工具链:

  8. Locust性能测试5-参数化批量注册

    前言 实现场景:所有并发虚拟用户共享同一份测试数据,并且保证虚拟用户使用的数据不重复. 例如,模拟10用户并发注册账号,总共有100个手机号,要求注册账号不重复,注册完毕后结束测试 准备数据 虚拟用户 ...

  9. 软件包管理:rpm命令管理-包命名与依赖性

    rpm包的管理主要有两种方法:一种是rpm命令管理另一种是yum在线管理 注意软件包没有扩展名,写上只是为了好看,便于识别而已. 注意区别包名,包全名.之所以要区分,就是因为有些命令十分挑剔,需要跟正 ...

  10. linux字符处理命令 sort(部分转载)

    [root@LocalWeb01 ~]# sort /etc/passwd |less   (升序 ) [root@LocalWeb01 ~]# sort -r /etc/passwd |less ( ...