题目链接:

  https://codeforces.com/contest/1118

A题:

题意:

  q次查询,给你一个n,要你用1和2来凑出n,1的花费为a,2的花费为b,求花费的最小值。

思路:

  我们知道当2*a<=b时全用1来肯定是最优的,当2*a>b时,若n为奇数就是1个1其他全是2,若n为偶数就全都是2这样是最优的。

代码实现如下:

  1. #include <set>
  2. #include <map>
  3. #include <deque>
  4. #include <queue>
  5. #include <stack>
  6. #include <cmath>
  7. #include <ctime>
  8. #include <bitset>
  9. #include <cstdio>
  10. #include <string>
  11. #include <vector>
  12. #include <cstdlib>
  13. #include <cstring>
  14. #include <iostream>
  15. #include <algorithm>
  16. using namespace std;
  17.  
  18. typedef long long LL;
  19. typedef pair<LL, LL> pLL;
  20. typedef pair<LL, int> pLi;
  21. typedef pair<int, LL> pil;;
  22. typedef pair<int, int> pii;
  23. typedef unsigned long long uLL;
  24.  
  25. #define lson rt<<1
  26. #define rson rt<<1|1
  27. #define lowbit(x) x&(-x)
  28. #define name2str(name) (#name)
  29. #define bug printf("*********\n")
  30. #define debug(x) cout<<#x"=["<<x<<"]" <<endl
  31. #define FIN freopen("D://code//in.txt","r",stdin)
  32. #define IO ios::sync_with_stdio(false),cin.tie(0)
  33.  
  34. const double eps = 1e-;
  35. const int mod = ;
  36. const int maxn = 2e5 + ;
  37. const double pi = acos(-);
  38. const int inf = 0x3f3f3f3f;
  39. const LL INF = 0x3f3f3f3f3f3f3f3fLL;
  40.  
  41. int q;
  42. LL n, a, b;
  43.  
  44. int main(){
  45. scanf("%d", &q);
  46. while(q--) {
  47. scanf("%lld%lld%lld", &n, &a, &b);
  48. LL ans = ;
  49. if( * a <= b) {
  50. ans = n * a;
  51. } else {
  52. ans = n / * b + (n % ) * a;
  53. }
  54. printf("%lld\n", ans);
  55. }
  56. return ;
  57. }

B题:

题意:

  给你n个数,要你去掉一个数,然后剩余的n-1个数保持相对顺序不变,问有多少种方案使得剩下的数中奇数项的和等于偶数项的和。

思路:

  我们知道去掉一个数后对前面的数的位置的奇偶性不会产生影响,后面的数的位置的奇偶性变为对立的,因此我们只需要求下奇数项和偶数项的前缀和即可。

代码实现如下:

  1. #include <set>
  2. #include <map>
  3. #include <deque>
  4. #include <queue>
  5. #include <stack>
  6. #include <cmath>
  7. #include <ctime>
  8. #include <bitset>
  9. #include <cstdio>
  10. #include <string>
  11. #include <vector>
  12. #include <cstdlib>
  13. #include <cstring>
  14. #include <iostream>
  15. #include <algorithm>
  16. using namespace std;
  17.  
  18. typedef long long LL;
  19. typedef pair<LL, LL> pLL;
  20. typedef pair<LL, int> pLi;
  21. typedef pair<int, LL> pil;;
  22. typedef pair<int, int> pii;
  23. typedef unsigned long long uLL;
  24.  
  25. #define lson rt<<1
  26. #define rson rt<<1|1
  27. #define lowbit(x) x&(-x)
  28. #define name2str(name) (#name)
  29. #define bug printf("*********\n")
  30. #define debug(x) cout<<#x"=["<<x<<"]" <<endl
  31. #define FIN freopen("D://code//in.txt","r",stdin)
  32. #define IO ios::sync_with_stdio(false),cin.tie(0)
  33.  
  34. const double eps = 1e-;
  35. const int mod = ;
  36. const int maxn = 2e5 + ;
  37. const double pi = acos(-);
  38. const int inf = 0x3f3f3f3f;
  39. const LL INF = 0x3f3f3f3f3f3f3f3fLL;
  40.  
  41. int n;
  42. LL a[maxn], sum1[maxn], sum2[maxn];
  43.  
  44. int main(){
  45. scanf("%d", &n);
  46. for(int i = ; i <= n; i++) {
  47. scanf("%lld", &a[i]);
  48. sum1[i] = sum1[i-];
  49. sum2[i] = sum2[i-];
  50. if(i & ) sum1[i] += a[i];
  51. else sum2[i] += a[i];
  52. }
  53. int ans = ;
  54. for(int i = ; i <= n; i++) {
  55. if(sum1[i-] + sum2[n] - sum2[i] == sum2[i-] + sum1[n] - sum1[i]) {
  56. ans++;
  57. }
  58. }
  59. printf("%d\n", ans);
  60. return ;
  61. }

C题:

题意:

  给你n*n个数要你构建一个每行每列都是回文的矩阵。

思路:

  模拟即可。

代码实现如下:

  1. #include <set>
  2. #include <map>
  3. #include <deque>
  4. #include <queue>
  5. #include <stack>
  6. #include <cmath>
  7. #include <ctime>
  8. #include <bitset>
  9. #include <cstdio>
  10. #include <string>
  11. #include <vector>
  12. #include <cstdlib>
  13. #include <cstring>
  14. #include <iostream>
  15. #include <algorithm>
  16. using namespace std;
  17.  
  18. typedef long long LL;
  19. typedef pair<LL, LL> pLL;
  20. typedef pair<LL, int> pLi;
  21. typedef pair<int, LL> pil;;
  22. typedef pair<int, int> pii;
  23. typedef unsigned long long uLL;
  24.  
  25. #define lson rt<<1
  26. #define rson rt<<1|1
  27. #define lowbit(x) x&(-x)
  28. #define name2str(name) (#name)
  29. #define bug printf("*********\n")
  30. #define debug(x) cout<<#x"=["<<x<<"]" <<endl
  31. #define FIN freopen("D://code//in.txt","r",stdin)
  32. #define IO ios::sync_with_stdio(false),cin.tie(0)
  33.  
  34. const double eps = 1e-;
  35. const int mod = ;
  36. const int maxn = 2e5 + ;
  37. const double pi = acos(-);
  38. const int inf = 0x3f3f3f3f;
  39. const LL INF = 0x3f3f3f3f3f3f3f3fLL;
  40.  
  41. int n, x;
  42. int mp[][], cnt[];
  43.  
  44. int main(){
  45. scanf("%d", &n);
  46. for(int i = ; i <= n * n; i++) {
  47. scanf("%d", &x);
  48. cnt[x]++;
  49. }
  50. if(n % == ) {
  51. for(int i = ; i <= ; i++) {
  52. if(cnt[i] % != ) {
  53. return printf("NO\n") * ;
  54. }
  55. }
  56. int pp = ;
  57. for(int i = ; i <= n / ; i++) {
  58. for(int j = ; j <= n / ; j++) {
  59. while(cnt[pp] == ) pp++;
  60. mp[i][j] = mp[i][n-j+] = mp[n-i+][j] = mp[n-i+][n-j+] = pp;
  61. cnt[pp] -= ;
  62. }
  63. }
  64. } else {
  65. int num1 = , num2 = , num3 = ;
  66. for(int i = ; i <= ; i++) {
  67. if(cnt[i] % == ) num1++;
  68. else if(cnt[i] % == ) num2++;
  69. else if(cnt[i] % == ) num3++;
  70. }
  71. if(num2 > (n-) || (num1 > && num3 > ) || (num1 == && num3 != ) || (num3 == && num1 != )) return printf("NO\n") * ;
  72. for(int i = ; i <= ; i++) {
  73. if(cnt[i] % == || cnt[i] % == ) {
  74. mp[n/+][n/+] = i;
  75. cnt[i]--;
  76. break;
  77. }
  78. }
  79. int pp = ;
  80. for(int i = ; i <= n / ; i++) {
  81. for(int j = ; j <= n / ; j++) {
  82. while(cnt[pp] < ) pp++;
  83. mp[i][j] = mp[i][n-j+] = mp[n-i+][j] = mp[n-i+][n-j+] = pp;
  84. cnt[pp] -= ;
  85. }
  86. }
  87. pp = ;
  88. for(int i = ; i <= n / ; i++) {
  89. while(cnt[pp] == ) {
  90. pp++;
  91. if(pp > ) return printf("NO\n") * ;
  92. }
  93. mp[i][n/+] = mp[n-i+][n/+] = pp;
  94. cnt[pp] -= ;
  95. }
  96. for(int i = ; i <= n / ; i++) {
  97. while(cnt[pp] < ) {
  98. pp++;
  99. if(pp > ) return printf("NO\n") * ;
  100. }
  101. if(pp > ) return printf("NO\n") * ;
  102. mp[n/+][i] = mp[n/+][n-i+] = pp;
  103. cnt[pp] -= ;
  104. }
  105. }
  106. printf("YES\n");
  107. for(int i = ; i <= n; i++) {
  108. for(int j = ; j <= n; j++) {
  109. printf("%d ", mp[i][j]);
  110. }
  111. printf("\n");
  112. }
  113. return ;
  114. }

D题:

题意:

  有n杯咖啡,m页作业,每杯咖啡的权值为ai,假如你某一天喝了k杯咖啡那么你写的作业页数为a1,a2-1,a3-2……这里的1,2,3指当天喝的顺序而非原序列的下标,要你用最少的天数写完作业。

思路:

  先将ai从大到小排序,二分天数,然后把所有会产生正贡献的咖啡求和,与m比较即可。

代码实现如下:

  1. #include <set>
  2. #include <map>
  3. #include <deque>
  4. #include <queue>
  5. #include <stack>
  6. #include <cmath>
  7. #include <ctime>
  8. #include <bitset>
  9. #include <cstdio>
  10. #include <string>
  11. #include <vector>
  12. #include <cstdlib>
  13. #include <cstring>
  14. #include <iostream>
  15. #include <algorithm>
  16. using namespace std;
  17.  
  18. typedef long long LL;
  19. typedef pair<LL, LL> pLL;
  20. typedef pair<LL, int> pLi;
  21. typedef pair<int, LL> pil;;
  22. typedef pair<int, int> pii;
  23. typedef unsigned long long uLL;
  24.  
  25. #define lson rt<<1
  26. #define rson rt<<1|1
  27. #define lowbit(x) x&(-x)
  28. #define name2str(name) (#name)
  29. #define bug printf("*********\n")
  30. #define debug(x) cout<<#x"=["<<x<<"]" <<endl
  31. #define FIN freopen("D://code//in.txt","r",stdin)
  32. #define IO ios::sync_with_stdio(false),cin.tie(0)
  33.  
  34. const double eps = 1e-;
  35. const int mod = ;
  36. const int maxn = 2e5 + ;
  37. const double pi = acos(-);
  38. const int inf = 0x3f3f3f3f;
  39. const LL INF = 0x3f3f3f3f3f3f3f3fLL;
  40.  
  41. int n, m;
  42. int a[maxn];
  43.  
  44. bool check(int x) {
  45. int tmp = ;
  46. LL sum = ;
  47. for(int i = ; i <= min(n, x); i++) {
  48. sum += a[i];
  49. }
  50. for(int i = x + ; i <= n; i++) {
  51. if(i % x == || x == ) tmp++;
  52. if(a[i] - tmp > ) {
  53. sum += a[i] - tmp;
  54. } else {
  55. break;
  56. }
  57. }
  58. return sum >= m;
  59. }
  60.  
  61. bool cmp(int a, int b) {
  62. return a > b;
  63. }
  64.  
  65. int main(){
  66. scanf("%d%d", &n, &m);
  67. LL sum = ;
  68. for(int i = ; i <= n; i++) {
  69. scanf("%d", &a[i]);
  70. sum += a[i];
  71. }
  72. sort(a + , a + n + , cmp);
  73. if(sum < m) {
  74. return printf("-1\n") * ;
  75. }
  76. int ub = inf, lb = , mid, ans = ;
  77. while(ub >= lb) {
  78. mid = (ub + lb) >> ;
  79. if(check(mid)) {
  80. ub = mid - ;
  81. ans = mid;
  82. } else {
  83. lb = mid + ;
  84. }
  85. }
  86. printf("%d\n", ans);
  87. return ;
  88. }

E题:

题意:

  给你n和k,要你构建n对数,假设第i对为ai,bi,第j对为aj,bj,需满足一下条件:

  1. ai!=bi;
  2. ai==aj与bi==bj不能同时成立;
  3. 若j==i+1时,ai!=aj且bi!=bj。

思路:

  第一个数一直是1~k的循环,第二个数则是2~k~1,3~k~2,4~k~2这样循环,我们知道满足题意的总排列对数只有k*(k-1)种(第一位放置方法有k种选择,第二位也有k种,故总的排列方法是k*k,其中只有1-1,2-2这种是不满足要求的),因此当n>k*(k-1)时输出NO。

代码实现如下:

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int n,k;
  4. pair<int,int> ans[];
  5. int main(){
  6. scanf("%d%d",&n,&k);
  7. if(n>1LL*k*(k-)) return puts("NO")*;
  8. int a=,x=,y=;
  9. for(int i = ; i<=n;i++){
  10. ans[i]={x,y};
  11. x++,y++;
  12. if(x>k) x = ;
  13. if(y>k) y =;
  14. if(y==a) {
  15. a++;
  16. if(a>k) a=;
  17. y = a;
  18. }
  19. }
  20. puts("YES");
  21. for(int i =;i<=n;i++){
  22. printf("%d %d\n",ans[i].first,ans[i].second);
  23. }
  24. return ;
  25. }

F1题:

题意:

  给你一棵树,每个结点的颜色可以是0,1,2,要你删除一条边使得得到的两个联通块内不能同时出现1和2。

思路:

  dfs记一下子树中的1和2的个数与总的1和2的个数比较即可。

代码实现如下:

  1. #include <set>
  2. #include <map>
  3. #include <deque>
  4. #include <queue>
  5. #include <stack>
  6. #include <cmath>
  7. #include <ctime>
  8. #include <bitset>
  9. #include <cstdio>
  10. #include <string>
  11. #include <vector>
  12. #include <cstdlib>
  13. #include <cstring>
  14. #include <iostream>
  15. #include <algorithm>
  16. using namespace std;
  17.  
  18. typedef long long LL;
  19. typedef pair<LL, LL> pLL;
  20. typedef pair<LL, int> pLi;
  21. typedef pair<int, LL> pil;;
  22. typedef pair<int, int> pii;
  23. typedef unsigned long long uLL;
  24.  
  25. #define lson rt<<1
  26. #define rson rt<<1|1
  27. #define lowbit(x) x&(-x)
  28. #define name2str(name) (#name)
  29. #define bug printf("*********\n")
  30. #define debug(x) cout<<#x"=["<<x<<"]" <<endl
  31. #define FIN freopen("D://code//in.txt","r",stdin)
  32. #define IO ios::sync_with_stdio(false),cin.tie(0)
  33.  
  34. const double eps = 1e-;
  35. const int mod = ;
  36. const int maxn = 3e5 + ;
  37. const double pi = acos(-);
  38. const int inf = 0x3f3f3f3f;
  39. const LL INF = 0x3f3f3f3f3f3f3f3fLL;
  40.  
  41. int n, u, v, num1, num2;
  42. LL ans = ;
  43. int a[maxn], sum1[maxn], sum2[maxn];
  44. vector<int> G[maxn];
  45.  
  46. void dfs(int u, int p) {
  47. if(a[u] == ) sum1[u]++;
  48. else if(a[u] == ) sum2[u]++;
  49. for(int i = ; i < (int)G[u].size(); i++) {
  50. int v = G[u][i];
  51. if(v == p) continue;
  52. dfs(v, u);
  53. if((sum1[v] == num1 && sum2[v] == ) || (sum2[v] == num2 && sum1[v] == )) ans++;
  54. sum1[u] += sum1[v], sum2[u] += sum2[v];
  55. }
  56. }
  57.  
  58. int main(){
  59. scanf("%d", &n);
  60. for(int i = ; i <= n; i++) {
  61. scanf("%d", &a[i]);
  62. if(a[i] == ) num1++;
  63. else if(a[i] == ) num2++;
  64. }
  65. for(int i = ; i < n; i++) {
  66. scanf("%d%d", &u, &v);
  67. G[u].push_back(v);
  68. G[v].push_back(u);
  69. }
  70. dfs(, );
  71. printf("%lld\n", ans);
  72. return ;
  73. }

Codeforces Round #540 (Div. 3)题解的更多相关文章

  1. Codeforces Round #540 (Div. 3) 部分题解

    Codeforces Round #540 (Div. 3) 题目链接:https://codeforces.com/contest/1118 题目太多啦,解释题意都花很多时间...还有事情要做,就选 ...

  2. Codeforces Round #182 (Div. 1)题解【ABCD】

    Codeforces Round #182 (Div. 1)题解 A题:Yaroslav and Sequence1 题意: 给你\(2*n+1\)个元素,你每次可以进行无数种操作,每次操作必须选择其 ...

  3. Codeforces Round #608 (Div. 2) 题解

    目录 Codeforces Round #608 (Div. 2) 题解 前言 A. Suits 题意 做法 程序 B. Blocks 题意 做法 程序 C. Shawarma Tent 题意 做法 ...

  4. Codeforces Round #525 (Div. 2)题解

    Codeforces Round #525 (Div. 2)题解 题解 CF1088A [Ehab and another construction problem] 依据题意枚举即可 # inclu ...

  5. Codeforces Round #528 (Div. 2)题解

    Codeforces Round #528 (Div. 2)题解 A. Right-Left Cipher 很明显这道题按题意逆序解码即可 Code: # include <bits/stdc+ ...

  6. Codeforces Round #466 (Div. 2) 题解940A 940B 940C 940D 940E 940F

    Codeforces Round #466 (Div. 2) 题解 A.Points on the line 题目大意: 给你一个数列,定义数列的权值为最大值减去最小值,问最少删除几个数,使得数列的权 ...

  7. Codeforces Round #677 (Div. 3) 题解

    Codeforces Round #677 (Div. 3) 题解 A. Boring Apartments 题目 题解 简单签到题,直接数,小于这个数的\(+10\). 代码 #include &l ...

  8. Codeforces Round #665 (Div. 2) 题解

    Codeforces Round #665 (Div. 2) 题解 写得有点晚了,估计都官方题解看完切掉了,没人看我的了qaq. 目录 Codeforces Round #665 (Div. 2) 题 ...

  9. Codeforces Round #160 (Div. 1) 题解【ABCD】

    Codeforces Round #160 (Div. 1) A - Maxim and Discounts 题意 给你n个折扣,m个物品,每个折扣都可以使用无限次,每次你使用第i个折扣的时候,你必须 ...

随机推荐

  1. Oracle中SYS_CONNECT_BY_PATH函数的妙用 ;

    Oracle 中SYS_CONNECT_BY_PATH函数是非常重要的函数,下面就为您介绍一个使用SYS_CONNECT_BY_PATH函数的例子,实例如下: 数据准备: ),b )); ', 'A' ...

  2. 2018最新Web前端经典面试试题及答案

    javascript: JavaScript中如何检测一个变量是一个String类型?请写出函数实现 typeof(obj) === "string" typeof obj === ...

  3. strtr、str_replace()、substr_replace、preg_replace之间的区别

    strtr(string, from, to): 逐个字符开始替换,以from跟to中长度较较短的一个为准,例如: strtr("aidengni","ai", ...

  4. vue-cli配置axios

    1. npm install axios --save 2. npm install @type/axios --save-dev(使用ts编写的需要此声明文件,升级的axios好像不需要了,已经自带 ...

  5. Memcache 服务管理脚本

    自定义脚本将memcached作为系统服务启动以及开机启动. 一.编写脚本 在/etc/init.d/目录下新建一个脚本,名称为:memcached.内容如下: vi /etc/init.d/memc ...

  6. 每日一问(如何在List中加入、设置、获取和删除其中的元素?)

    作为集合接口的一部分,对List接口所做的操作,最常见的就是增删查改了.这里总结下JAVA 中List接口及实现该接口的类实现这些操作的方法. 一.增加新的元素的方法 在Collection接口中定义 ...

  7. centos 7下ldap安装

    环境说明: 操作系统:CentOS Linux release 7.5.1804 (Core) LDAP:2.4.44 前提条件: 关闭防火墙.selinux,同时进行时钟同步. 其中XXX需要用域名 ...

  8. 关于SDWebImage加载高清图片导致app崩溃的问题

    链接是对于SDWebImage的使用方法 http://www.cnblogs.com/JimmyBright/p/4457258.html 使用SDWebImage加载高清图片的时候,往往会报内存溢 ...

  9. BZOJ 2742: [HEOI2012]Akai的数学作业

    2742: [HEOI2012]Akai的数学作业 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 535  Solved: 226[Submit][S ...

  10. 【BZOJ4559】成绩比较(动态规划,拉格朗日插值)

    [BZOJ4559]成绩比较(动态规划,拉格朗日插值) 题面 BZOJ 洛谷 题解 显然可以每门课顺次考虑, 设\(f[i][j]\)表示前\(i\)门课程\(zsy\)恰好碾压了\(j\)个\(yy ...