Codeforces Round #702 (Div. 3) 全部题解

读错题意,写了半天真是心态爆炸,总的来看这次题目不难的。

A. Dense Array

http://codeforces.com/contest/1490/problem/A

解题思路
相邻的数字必然是倘若不满足的话是需要插入数据的,那么我们模拟插入数据即可.
x=min(ai,ai+1),y=max(ai,ai+1)x = min(a_i, a_{i+1}), y = max(a_i, a_{i+1})x=min(ai​,ai+1​),y=max(ai​,ai+1​)

  • 倘若2∗x≤y2 * x \leq y2∗x≤y不需要操作
  • 否则,需要插入数据。不断的贪心插入 2⋅x2\cdot x2⋅x,直到满足条件。 其实这有一个公式的,推导与公式如下所示。

    模拟乘以2
#include <bits/stdc++.h>
using namespace std;typedef long long LL;
typedef pair<int, int> PII;
const int N = 1010, INF = 0x3f3f3f3f;
int n, a[N];
PII b[N];
bool st[N];int cal(int t1, int t2)
{int ret = 0;while (2 * t1 < t2){ret ++;t1 *= 2;}return ret;
}int main()
{int t;  cin >> t;while (t -- ){cin >> n;LL x = 0;for (int i = 1; i <= n; i ++ ){scanf("%d", &a[i]);}double cnt;for (int i = 1, t1, t2; i < n; i ++ ){t1 = min(a[i], a[i + 1]), t2 = max(a[i], a[i + 1]);if (t1 * 2 >= t2)   continue;else{x += cal(t1, t2);}}cout << x << endl;}return 0;
}

直接上公式,没有过,应该是小数精度会被卡

#include <bits/stdc++.h>
using namespace std;typedef long long LL;
typedef pair<int, int> PII;
const int N = 1010, INF = 0x3f3f3f3f;
int n, a[N];int main()
{int t;  cin >> t;while (t -- ){cin >> n;LL x = 0;for (int i = 1; i <= n; i ++ ){scanf("%d", &a[i]);}double cnt;for (int i = 1, t1, t2; i < n; i ++ ){t1 = min(a[i], a[i + 1]), t2 = max(a[i], a[i + 1]);if (t1 * 2 >= t2)   continue;else{x += ceil(log(t2 * 1.0 / t1) / log(2)) - 1;}}cout << x << endl;}return 0;
}

B. Balanced Remainders

http://codeforces.com/contest/1490/problem/B

先统计出来%3\%3%3余数为0,1,20,1, 20,1,2数字的数量c0,c1,c2c_0, c_1, c_2c0​,c1​,c2​,根据倘若多,那么一定会送走,倘若少,一定会挪进行这个性质进行模拟,而且对于特定的cic_ici​他给别人,或者是别人给他的交易对象是一样的,即操作是固定的。

#include <bits/stdc++.h>
using namespace std;typedef long long LL;
typedef pair<int, int> PII;
const int N = 100010, INF = 0x3f3f3f3f;
int n, a[N];
int c[5] = {0, 0, 0};int opt(int idx, int ave)
{int idx2 = (idx + 1) % 3, idx0 = (idx - 1 + 3) % 3;int need = 0;if (c[idx] < ave)  // 过少,需要向前一个索要{need = ave - c[idx];c[idx0] -= need;c[idx] += need;}else      // 太多,需要给下一个{need = c[idx] - ave;c[idx2] += need;c[idx] -= need;}return need;
}int main()
{int t;  cin >> t;while (t -- ){cin >> n;int ave = n / 3;memset(c, 0, sizeof c);for (int i = 1; i <= n; i ++ ){scanf("%d", &a[i]);c[a[i] % 3] ++;}int res = 0;for (int i = 0; i < 3; i ++ ){res += opt(i, ave);}printf("%d\n", res);}return 0;
}

C. Sum of Cubes

http://codeforces.com/contest/1490/problem/C

直接就是 将 i3i^3i3在map上进行打表就可以了。
很关键的是预处理多处理几个,不然很容易被卡

#include <bits/stdc++.h>
using namespace std;typedef long long LL;
const int N = 100010;
LL n;
LL a[N];
map<LL, bool> m;int main()
{int t;  cin >> t;for (LL i = 1; i <= 10010; i ++ )    // 这里仅仅写到 10000就会 WA,主要是怕他多往后走了一步,查询到了0{a[i] = i * i * i;m[a[i]] = true;}// cout << m[0] << endl;while (t -- ){scanf("%lld", &n);bool flag = false;LL surp;for (int i = 1; !flag && a[i] <= n; i ++ ){surp = n - a[i];if (m[surp] == true){flag = true;}}if (flag)   puts("YES");else    puts("NO");}return 0;
}

D. Permutation Transformation

http://codeforces.com/contest/1490/problem/D


一个简单的模拟dfs,每次在区间找最大值作为 root 即可

#include <bits/stdc++.h>
using namespace std;const int N = 110;
int a[N], n, depth[N];void build(int fa, int l, int r)
{if (l >= r) return;int u = -1, v = -1;// 左子树for (int i = l; i < fa; i ++ ){if (u == -1 || a[u] < a[i])u = i;}depth[u] = depth[fa] + 1;build(u, l, fa - 1);// 右子树for (int i = fa + 1; i <= r; i ++ ){if (v == -1 || a[v] < a[i])v = i;}depth[v] = depth[fa] + 1;build(v, fa + 1, r);
}int main()
{int t;  cin >> t;while (t -- ){scanf("%d", &n);for (int i = 1; i <= n; i ++ )scanf("%d", &a[i]);memset(depth, -1, sizeof depth);int v;for (int i = 1; i <= n; i ++ ){if (a[i] == n){v = i;break;}}depth[v] = 0;build(v, 1, n);cout << depth[1];for (int i = 2; i <= n; i ++ )printf(" %d", depth[i]);cout << endl;}return 0;
}

E. Accidental Victory

http://codeforces.com/contest/1490/problem/E

说白了就是看谁可以赢。
首先我们先将他们按照各自的数值排序,得到数组a1,a2⋅⋅⋅,ana_1,a_2\cdot\cdot\cdot,a_na1​,a2​⋅⋅⋅,an​,对应编号为idx1,idx2,⋅⋅⋅,idxnidx_1, idx_2, \cdot\cdot\cdot,idx_nidx1​,idx2​,⋅⋅⋅,idxn​,我们对数组求取前缀和sum1,sum2,⋅⋅⋅,sumnsum_1, sum_2,\cdot\cdot\cdot,sum_nsum1​,sum2​,⋅⋅⋅,sumn​,
我们从后往前看,

  • 第nnn个是最大的,肯定有机会成为winner
  • 第n−1n-1n−1倘若可以成为winnerwinnerwinner,当且仅当他赢过1n−21~n-21 n−2个人,然后和ana_nan​比较,倘若他成不了winnerwinnerwinner那么直接算法结束,比他小的人更成为不了winnnerwinnnerwinnner
  • 不断这样算下去即可
#include <bits/stdc++.h>
using namespace std;typedef long long LL;
typedef pair<LL, int> PII;
const int N = 200010;
vector<int> res;
LL a[N];
LL b[N];
PII c[N];
int n;bool cmp(const PII &t1, const PII &t2)
{return t1.first < t2.first;
}
int main()
{int t;  cin >> t;while (t -- ){scanf("%d", &n);for (int i = 1; i <= n; i ++ ){scanf("%lld", &a[i]);c[i].first = a[i];c[i].second = i;}sort(c + 1, c + n + 1, cmp);for (int i = 1; i <= n; i ++ )b[i] = b[i - 1] + c[i].first;res.clear();res.push_back(c[n].second);for (int i = n - 1; i >= 1; i -- ){if (b[i] >= c[i + 1].first)res.push_back(c[i].second);elsebreak;}sort(res.begin(), res.end());cout << res.size() << endl;cout << res[0];for (int i = 1; i < res.size(); i ++ )printf(" %d", res[i]);cout << endl;}return 0;
}

F. Equalize the Array

http://codeforces.com/contest/1490/problem/F

具体思路:
先用map将出现次数存储起来,统计出出现次数为iii的数字一共有cnticnt_icnti​个,
那么我们首先模拟一下,想让所有数字出现的次数为iii或者是000,那么我们需要将出现次数小于等于iii的数字全部移除,需要的次数为cnt1⋅1+⋅⋅⋅cnti−1⋅(i−1)cnt_1\cdot1+\cdot\cdot\cdot cnt_{i-1}\cdot{(i-1)}cnt1​⋅1+⋅⋅⋅cnti−1​⋅(i−1)
需要将出现次数大于iii的数字次数缩减为iii,
需要的操作次数为$$
⟹(i+1)⋅cnti+1+(i+2)⋅cnti+2+⋅⋅⋅+(n)⋅cntn−i⋅(cnti+1+cnti+2+⋅⋅⋅+cntn)\Longrightarrow(i+1)\cdot cnt_{i+1} + (i+2)\cdot cnt_{i+2}+\cdot\cdot\cdot+(n)\cdot cnt_n -i\cdot(cnt_{i+1}+cnt_{i+2}+\cdot\cdot\cdot+cnt_n)⟹(i+1)⋅cnti+1​+(i+2)⋅cnti+2​+⋅⋅⋅+(n)⋅cntn​−i⋅(cnti+1​+cnti+2​+⋅⋅⋅+cntn​)
因此我们直接预处理出来 cnticnt_icnti​的前缀数组和他们的加权成前缀数组即可

#include <bits/stdc++.h>
using namespace std;typedef pair<int, int > PII;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const int N = 200010;
int a[N + 5];
int n;
map<int, int> m;
int cnt[N + 5];
int sum[N + 5];
LL addval[N + 5];int main()
{int t;  cin >> t;while (t -- ){m.clear();scanf("%d", &n);for (int i = 1; i <= n; i ++ ){scanf("%lld", &a[i]);m[a[i]] ++;}memset(cnt, 0, sizeof cnt);memset(sum, 0, sizeof sum);memset(addval, 0, sizeof addval);map<int, int>::iterator it;for (it = m.begin(); it != m.end(); it ++)  // 出现it->second 次数的数字进行统计{/// printf("IT: %d, %d\n",  it->first, it->second);cnt[it->second] ++;}// 计算前缀数组和累加和数组for (int i = 1; i <= n; i ++ )  // 最多出现 n 次{sum[i] = sum[i - 1] + cnt[i];addval[i] = addval[i - 1] + LL(cnt[i]) * i;}
/*printf("CNT:\n\t");for (int i = 1; i <= n; i ++ ){printf("%d ", cnt[i]);}cout << endl;
*/LL res = 1e16;LL up, down;for (int i = 1; i <= n; i ++ ){down = addval[i - 1];up = (addval[n] - addval[i - 1]) - (sum[n] - sum[i - 1]) * i;res = min(res, up + down);}cout << res << endl;}return 0;
}

G. Old Floppy Drive

http://codeforces.com/contest/1490/problem/G


本题是一个较为隐蔽的二分题目,需要我们贪心预处理出来可以二分的数组。
首先我们先将前缀数组给求出来,
并且贪心得到 cnt不断增加,而且前缀和也增加的数字,将其放入数组v, 具体见代码max_val部分。
那么数组 v 是一个数值(无论是val,还是opt_cnt)都不断增加的数据

  • 倘若 xix_ixi​在 数组 v 最大值的范围之内,那么直接二分找最小的cnt
  • 否则,需要判断一下整个原数组 a 的累加和是否大于零,倘若小于等于0,无解
  • 否则,是可以经过不断的循环找到这个数的,首先我们根据max(v)找到最小的循环次数,然后二分即可。
    本题最坑的点就是 long long 开的地方很多,几乎所有的数据都要开long long
#include <bits/stdc++.h>
using namespace std;typedef long long LL;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int N = 200010;
int n, m;
LL a[N], x[N];
LL sum[N];
class Node
{public:LL val, cnt;Node(LL _val = 0, LL _cnt = 0){val = _val, cnt = _cnt;}
}b[N];int main()
{int t;  cin >> t;while (t -- ){cin >> n >> m;LL cur_max = -INF;b[1] = Node(-INF, -1);int n2 = 1;sum[0] = 0LL;for (int i = 1; i <= n; i ++ ){scanf("%lld", &a[i]);sum[i] = sum[i - 1] + a[i];if (sum[i] > cur_max){cur_max = sum[i];b[++ n2] = Node(cur_max, i - 1);    // 注意在这里的 - 1}}for (int i = 1; i <= m; i ++ )scanf("%lld", &x[i]);// 当前的 b 数字 是 1~n val 增加, cnt增加的递增数组,下面我们的 x 仅仅只需要二分即可;/// printf("RES:\n\t");LL loop = sum[n];for (int i = 1; i <= m; i ++ ){if (b[n2].val >= x[i])  // 可以直接二分找 >= x[i] 的最小值{int l = 1, r = n2, mid;while (l < r){mid = l + r >> 1;if (b[mid].val >= x[i])r = mid;elsel = mid + 1;}printf("%lld ", b[l].cnt);}else    // 查看他的 loop{if (loop <= 0)  // 无法达到{printf("-1 ");}else{LL loopcnt = (x[i] - b[n2].val) / loop + ((x[i] - b[n2].val) % loop != 0);x[i] -= loopcnt * loop;int l = 1, r = n2, mid;while (l < r){mid = l + r >> 1;if (b[mid].val >= x[i])r = mid;elsel = mid + 1;}printf("%lld ", b[l].cnt + loopcnt * n);  // loopcnt 是乘以 n 的}}}puts("");}return 0;
}

Codeforces Round #702 (Div. 3)解题报告相关推荐

  1. CodeCraft-19 and Codeforces Round #537 (Div. 2)解题报告

    Codeforces Round #537 (Div. 2) 题解报告 A. Superhero Transformation 题意 问能否通过把辅音字母换成另一个辅音字母,元音字母换成另一个元音字母 ...

  2. Codeforces Round #264 (Div. 2) 解题报告

    Source:  http://codeforces.com/contest/463 打得比较差..第三题想写nlgn的,结果调了很久又wa,以为写挫,过了很久发现做法有问题..最后两题惨淡收场.第四 ...

  3. Codeforces Round #219 (Div. 2) 解题报告

    Problem A Collecting Beats is Fun 题意:就是音乐游戏在4*4的网上一些格子需要在固定的时间点,告诉你一只手同一时间能点几个.问你能不能通关(就是一个不丢) 思路:水题 ...

  4. Codeforces Round #535 (Div. 3) 解题报告

    CF1108A. Two distinct points 做法:模拟 如果两者左端点重合就第二条的左端点++就好,然后输出左端点 #include <bits/stdc++.h> usin ...

  5. Codeforces Round #702 (Div. 3)A-G题解

    Codeforces Round #702 (Div. 3)A-G题解 比赛链接:https://codeforces.ml/contest/1490 这场F读错题意白给一发,G二分的if(dp[mi ...

  6. Codeforces Round #578 (Div. 2) 题解报告

    A. Hotelier sb模拟,直接按题意模拟就可以了. B. Block Adventure Gildong is playing a video game called Block Advent ...

  7. Codeforces Round #702 (Div. 3)全部题解

    题目链接:https://codeforces.com/contest/1490 文章目录 A. Dense Array B. Balanced Remainders C. Sum of Cubes ...

  8. (复习次数:1)D - Permutation Transformation——Codeforces Round #702 (Div. 3)

    https://codeforces.com/contest/1490/problem/D 一手递归 终结:没有子树 继续:把最大的找到,为记录,为界,加层数,左范围,右范围 #include< ...

  9. Codeforces Round #702 (Div. 3)D. Permutation Transformation

    D. Permutation Transformation 题目 输入样例 3 5 3 5 2 1 4 1 1 4 4 3 1 2 输出样例 1 0 2 3 1 0 0 1 3 2 解题思路 递归,当 ...

最新文章

  1. MySql错误1045 Access denied for user 'root'@'localhost' (using password:YES)
  2. 在python中操作excel
  3. 软件开发包(SDK)安全与合规报告(2020)
  4. 基础知识 | 对目标检测认识及理解
  5. 怎么能让宝贝快点入睡?
  6. 【Tricks】半监督深度学习训练和实现小Tricks
  7. Bioconductor学习_基因组坐标体系-Granges和IRanges
  8. 软考—软件设计师(软件工程基础知识)
  9. 解读住房公积金提取买房、租房、贷款新政
  10. 一张图慢慢转换成下一张图_给一张照片做一个视频 如何把一张图片制作成几分钟的视频|图片做成视频软件...
  11. TensorFlow-similarity 学习笔记13
  12. 研发质量管理的“红与黑”
  13. 解决win10启动夜神模拟器就蓝屏重启的问题
  14. mysql练习(1)
  15. 基于web_socket_channel 实现弹幕通信
  16. Java代理模式及动态代理详解
  17. 【Linux】Linux进程控制(学习复习兼顾)
  18. Android实现学生信息管理系统之学生端功能实现
  19. 依托EcoStruxure架构 施耐德电气发力数据中心全面创新
  20. 《android开发艺术探索》笔记之Bitmap的加载和Cache

热门文章

  1. CCCC L1-002. 打印沙漏【图形打印】
  2. 实现一个无法被继承的C++类
  3. Lucene3.5自学4--建索引相关知识总结
  4. 2011年值得注意的5个设计趋势
  5. CPU上电后加载程序的流程 | 基于RK3399
  6. Linux 30岁了~我们也老了
  7. 进程和线程的本质和区别
  8. android fragment 底部菜单栏,一句话搞定Android底部导航栏,一键绑定Fragment、ViewPager...
  9. 51单片机——硬件基础
  10. linux cocos环境变量,Linux开发cocos2dx程序环境搭建