写在前边

链接:Codeforces Round #696 (Div. 2)

A. Puzzle From the Future

链接:A题链接

题目大意:

给定一个\(a\),\(b\),\(d = a + b\),例如:\(a = 0101\), \(b= 1111\), \(d = 1212\),但是d如果有连续重复的数字那么会省掉,假如\(a + b = 12211\),则\(d = 121\),现在已知\(a\),\(b\)丢失了,要求求出一个\(b\)使得\(d\)最大。

思路:

首先要使得\(d\)最大化,\(b\)的第一位必定是\(1\),剩下的直接从前到后模拟即可,若\(a_i + b_i == d[i-1]\)那么\(d\)会减小,因此“反其道而行之即可”。

代码:

#include <iostream>
#include <string>
#include <algorithm>
#include <cstdio> using namespace std; #define Inf 0x3f3f3f3f
#define PII pair<int, int>
#define PLL pair<long, long> typedef long long LL;
typedef unsigned long long ULL; void solve() {
string s, res = "1";
int n;
cin >> n;
cin >> s;
for (int i = 1; i < s.size(); i++) {
if (((s[i]-'0') + 1) == (s[i - 1] - '0' + res[i - 1] - '0')) {
res += '0';
} else {
res += '1';
}
} cout << res << endl;
} int main()
{
int t;
cin >> t;
while (t--) {
solve();
} return 0;
}

B. Different Divisors

链接:B题链接

题目大意:

给定一个\(d\),求\(a\),要求\(a\)至少有\(4\)个约数,并且约数两两之间的至少差\(d\),并且要求\(a\)是可行方案中中的最小值。

思路:

\(\quad\) 题目要求四个约数,那么让其只有四个约数即可,即\(1 \quad p \quad q \quad pq\),\(p >= 1 + d\), \(q >= p + d\),因此\(p\)就是大于\(d + 1\)的最小质数,\(q\)就是大于\(p + d\)的最小质数,那么\(p * q\)就是满足条件\(a\)。

证明:

\(\quad\) 若\(p\)是一个\(>=1+d\)合数,那么\(1 -- p\)之间必定还会会有一个\(p\)的约数\(p′\),因为\(p′\)是\(p\)的约数,那么它必然也是\(a\)的约数这就使得\(p′-1 < d\),不满足约数之间至少差\(d\)的条件,因此矛盾。

\(\quad\) 若\(p\)不是最小质数,设其为\(p′\),另一个为\(q′\),那么\(p′ * q′ > p*q\),因此\(p′\)、\(q′\)并不是最优解。

综上:\(p\)为\(p >= 1 + d\)的最小质数与\(q\)为\(q >= p + d\)最小质数为最优解。

代码:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <vector> using namespace std; #define Inf 0x3f3f3f3f
#define PII pair<int, int>
#define PLL pair<long, long> typedef long long LL;
typedef unsigned long long ULL;
typedef vector<long long> VLL;
typedef vector<int> VI; const int N = 25e3 + 10;
int primes[N], cnt;
bool st[N]; void get_primes() {
for (int i = 2; i <= N; i++) {
if (!st[i]) {
primes[cnt++] = i;
}
for (int j = 0; primes[j] <= N / i; j++) {
st[primes[j] * i] = true;
if (i % primes[j] == 0) {
break;
}
}
}
} void solve() {
int d;
cin >> d;
int p, q, pcnt = 0;
for (pcnt; pcnt <= cnt; pcnt++) {
if (primes[pcnt] - 1 >= d) {
p = primes[pcnt];
break;
}
}
for (int i = pcnt; i <= cnt; i++) {
if (primes[i] - p >= d) {
q = primes[i];
break;
}
}
cout << p * q << endl;
} int main()
{
int t;
cin >> t;
get_primes();
while (t--) {
solve();
} return 0;
}

C. Array Destruction

链接:C题链接

题目大意:

例如,如果最初\(a=[3,5,1,2]\),则可以选择\(x=6\)。然后你可以选择第二个和第三个元素的总和\(5+1=6\),并抛出它们。在这个运算之后,\(x\)等于\(5\),数组中有两个元素:\(3\)和\(2\)。你可以在下次操作时把它们扔了,那么输出

YES
6
1 5
2 3

若无论如何数组内元素都无法清空,那么输出\(NO\)即可。

思路:

每次选最大的数作为x。

证明:

假设\(a_i < a_j < a_k\),不选\(a_j\)来构造x,那么即\(a_i + a_j = x\),此后\(x = a_j\),之后无论如何都消除不掉\(a_k\),因此每次选最大数作为x即可。

那么第一次除了选最大的数作为下一轮的x,另一个数就需要枚举所有可能,直到枚举到一个可以消掉所有数的情况。明白了这一点那么剩下的只需要模拟即可,可以用\(multiset\),因为其查找删除插入操作都是\(O(logn)\),若不用\(multiset\)则手写二分查找即可,最时间复杂度:\(O(n^2logn)\)

自己的错误:写了一晚上,但最后还是\(TLE2\)告终,其实我已经知道了每次要选最大值了,但还是用双指针来搜哪两个数等于\(x\),由于答案唯一,因此搜的结果是对的,但是导致\(O(n^3)\)被卡,应该直接确定数组中还剩下的最大数,然后剩下的小数直接用二分搜就好了。

代码:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <set> using namespace std; #define Inf 0x3f3f3f3f
#define PII pair<int, int>
#define PLL pair<long, long> typedef long long LL;
typedef unsigned long long ULL;
typedef vector<long long> VLL;
typedef vector<int> VI; vector<PII> check(VI all, int n, int x) {
vector<PII> res;
multiset<int> s;
for (auto it : all) {
s.insert(it);
}
for (int i = 0; i < n; i++) { //2*n个数,n个答案
auto it1 = s.end();
it1--;
int y = x - *it1;
s.erase(it1);
auto it2 = s.find(y);
if (it2 == s.end()) {
return {};
}
s.erase(it2);
res.push_back({y, x - y});
x = max(x - y, y);
}
return res;
} void solve() {
int n;
cin >> n;
VI all;
for (int i = 0; i < n * 2; i++) {
int c;
cin >> c;
all.push_back(c);
}
sort(all.begin(), all.end());
for (int i = 0; i < all.size() - 1; i++) { //枚举第一次操作的第二个数
int x = all[i] + all[all.size() - 1]; //最大的一个
vector<PII> res = check(all, n, x); if (res.size()) {
cout << "YES" << endl;
cout << x << endl;
for (auto it : res) {
cout << it.first << " " << it.second << endl;
}
return;
}
}
cout << "NO" << endl;
} int main()
{
int t;
cin >> t;
while (t--) {
solve();
} return 0;
}

Codeforces Round #696 (Div. 2) (A~C题解)的更多相关文章

  1. Codeforces Round #612 (Div. 2) 前四题题解

    这场比赛的出题人挺有意思,全部magic成了青色. 还有题目中的图片特别有趣. 晚上没打,开virtual contest打的,就会前三道,我太菜了. 最后看着题解补了第四道. 比赛传送门 A. An ...

  2. Codeforces Round #198 (Div. 2)A,B题解

    Codeforces Round #198 (Div. 2) 昨天看到奋斗群的群赛,好奇的去做了一下, 大概花了3个小时Ak,我大概可以退役了吧 那下面来稍微总结一下 A. The Wall Iahu ...

  3. Codeforces Round #672 (Div. 2) A - C1题解

    [Codeforces Round #672 (Div. 2) A - C1 ] 题目链接# A. Cubes Sorting 思路: " If Wheatley needs more th ...

  4. Codeforces Round #614 (Div. 2) A-E简要题解

    链接:https://codeforces.com/contest/1293 A. ConneR and the A.R.C. Markland-N 题意:略 思路:上下枚举1000次扫一遍,比较一下 ...

  5. Codeforces Round #610 (Div. 2) A-E简要题解

    contest链接: https://codeforces.com/contest/1282 A. Temporarily unavailable 题意: 给一个区间L,R通有网络,有个点x,在x+r ...

  6. Codeforces Round #611 (Div. 3) A-F简要题解

    contest链接:https://codeforces.com/contest/1283 A. Minutes Before the New Year 题意:给一个当前时间,输出离第二天差多少分钟 ...

  7. Codeforces Round #499 (Div. 2) D. Rocket题解

    题目: http://codeforces.com/contest/1011/problem/D This is an interactive problem. Natasha is going to ...

  8. Codeforces Round #499 (Div. 2) C Fly题解

    题目 http://codeforces.com/contest/1011/problem/C Natasha is going to fly on a rocket to Mars and retu ...

  9. Codeforces Round #198 (Div. 2)C,D题解

    接着是C,D的题解 C. Tourist Problem Iahub is a big fan of tourists. He wants to become a tourist himself, s ...

  10. Codeforces Round #579 (Div. 3) 套题 题解

    A. Circle of Students      题目:https://codeforces.com/contest/1203/problem/A 题意:一堆人坐成一个环,问能否按逆时针或者顺时针 ...

随机推荐

  1. 数据库是要拿来用的,不是用来PK先进性的

    周五参加了WAIC后又和一家上海本地的数据库厂商交流了一下午.等我要买高铁票回南京的时候已经买不到票了.好不容易刷到一张到苏州北的高铁票,我就上了车.上车后突然想起还不如就回苏州老家住一晚算了.到家后 ...

  2. 【工具推荐】github打不开or加载慢?不用配置hosts,教你一键加速!

    不说废话 下载watt toolkit(原名steam++) 官方地址: Watt Toolkit - 瓦特工具箱(Steam++官网) (steampp.net) 安装完后选中,点击一键加速即可. ...

  3. Bug_ku source (WEB) wp

    在源代码中注释中发现了flag,提交发现是假的. 我们打开kali用dirserch扫描一下网站后台目录吧 发现.git ,这题应该是一个git泄露的题,用wget吧文件下载到本地,-r 选项的作用是 ...

  4. 一些不错的VSCode设置和插件

    设置 同步设置 我们做的各项设置,不希望再到其他机器的时候还得再重新配置一次.VSCode中我们可以登陆微软账号或者GitHub账号,登陆后我们可以开启同步设置.开启设置同步,根据提示登陆即可. 允许 ...

  5. Nginx深入:nginx功能模块、目录结构及配置文件详解

    Nginx功能模块说明 1.Nginx 核心功能模块(Core functionality) Nginx核心功能模块负责Nginx的全局应用,主要对应主配置文件的核心层(Main层)和事件(Event ...

  6. STM32中SWD下载不进去的解决方法

    这是我第一次写自己的博客,希望以后写博客可以当做自己的个人习惯并坚持下去,作为技术分享,也欢迎各位大佬前来指正.本人本科学习的机械电子工程,了解机械制图.嵌入式编程.目前刚好学习了PCB制板,正在向着 ...

  7. KRPANO资源分析工具模板链接下载

    KRPano资源分析工具1.4.0加入了模板链接下载,可以批量下载有规律的链接. 模板链接基本规则 数字递增链接 pic[1-100]:会生成pic1,pic2,-pic100的链接 pic[a-z] ...

  8. IDEA 22.2.3 创建web项目及Tomcat部署与服务器初始界面修改(保姆版)

    开始前请确认自己的Tomcat.JDK已经安装配置完毕 不同版本的IDEA创建配置流程可能不同,演示中的IDEA版本号为22.2.3 本教程创作时间为2023/09/14 1.创建项目 通过下图路径进 ...

  9. SQL语句简单入门

    SQL语句速查 创建部门表 deptno dname location 1 技术部 23楼 create table dept --dept部门 ( deptno int primary key, - ...

  10. 在线问诊 Python、FastAPI、Neo4j — 生成 Cypher 语句

    目录 构建节点字典 构建Cypher CQL语句 Test 这边只是为了测试,演示效果和思路,实际应用中,可以通过NLP构建CQL 接上一篇的问题分类 question = "请问最近看东西 ...