Codeforces Round #579 (Div. 3) 题解
比赛链接:https://codeforc.es/contest/1203/
A. Circle of Students
题意:\(T\)组询问,每组询问给出\(n\)个数字,问这\(n\)个数字能否围成圆环。(围成圆环指,从某一位开始顺时针或逆时针遍历,数组为\(1, 2, 3, ..., n\))
分析:把数组复制一份,两个数组首尾相接,正反判定两次即可。
AC代码:
#include<bits/stdc++.h>
#define SIZE 200010
#define rep(i, a, b) for(int i = a; i <= b; ++i)
using namespace std;
typedef long long ll;
void io() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
}
ll n, m, k, t, a[SIZE];
int main() {
io(); cin >> t;
while (t--) {
cin >> n;
bool flag = true;
rep(i, 1, n) cin >> a[i];
rep(i, n + 1, n + n) a[i] = a[i - n];
rep(i, 1, n) {
if (a[i] == 1) {
ll cnt = 1;
rep(j, i + 1, i + n - 1) {
if (++cnt != a[j]) {
flag = false;
break;
}
}
}
}
if (flag) cout << "YES\n";
else {
flag = true;
rep(i, 1, n / 2) swap(a[i], a[n - i + 1]);
rep(i, n + 1, n + n) a[i] = a[i - n];
rep(i, 1, n) {
if (a[i] == 1) {
ll cnt = 1;
rep(j, i + 1, i + n - 1) {
if (++cnt != a[j]) {
flag = false;
break;
}
}
}
}
if (flag) cout << "YES\n";
else cout << "NO\n";
}
}
}
B. Equal Rectangles
题意:\(q\)组输入,每组输入给出一个整数\(n\),然后输入\(4n\)个整数。判断这些数字能否构成\(n\)个面积相同的矩形。
分析:先确定矩形的面积,显然应该是这些整数中的最小值乘最大值。然后我们对这些数排序,每次从首尾分别取两个数字判断是否构成矩形,面积是否相等即可。
AC代码:
#include<bits/stdc++.h>
#define SIZE 200010
#define rep(i, a, b) for(int i = a; i <= b; ++i)
using namespace std;
void io() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
}
int a[SIZE], n, t;
int main(){
io(); cin >> t;
while(t--) {
cin >> n;
rep (i, 1, 4 * n) cin >> a[i];
sort(a + 1, a + 4 * n + 1);
bool flag = true;
int L = 1, R = 4 * n;
int s = a[L] * a[R];
while(L < R){
if(a[L] != a[L + 1] || a[R] != a[R - 1]) { flag = false; break; }
else if(s != a[L] * a[R]) { flag = false; break; }
L += 2, R -= 2;
}
if (flag) cout << "YES\n";
else cout << "NO\n";
}
}
C. Common Divisors
题意:给出\(n\)个正整数,找出能整除这\(n\)个数的正整数个数。
分析:即求最大公因数的因子个数。
AC代码:
// luogu-judger-enable-o2
#include<bits/stdc++.h>
#define SIZE 500010
#define rep(i, a, b) for(long long i = a; i <= b; ++i)
using namespace std;
typedef long long ll;
void io() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
}
ll n, m, k, t, cnt = 0, a[SIZE];
int main() {
io(); cin >> n;
ll minx = 1e15;
rep(i, 1, n) {
cin >> a[i];
minx = min(minx, a[i]);
if (a[i] == 1) { cout << 1; return 0; }
}
rep(i, 1, n) {
if (a[i] % minx == 0) continue;
else minx = __gcd(minx, a[i]);
}
rep(i, 1, sqrt(minx)) {
if (minx % i == 0) {
if (i * i != minx)cnt += 2;
else cnt++;
}
}
cout << cnt;
}
D2. Remove the Substring (hard version)
题意:给定两个字符串\(s\)和\(t\),求一个最大的长度\(n\),使得在字符串\(s\)中删除连续\(n\)个字符后\(t\)仍然可以为\(s\)的子串。
分析:我们考虑下面这个样例:
\(asxxxxasdxxxd\)
\(asd\)
可以发现我们删除的最大连续\(n\)个字符有两种情况,一种是从头或尾开始删(像CF给出的样例),另一种就是上面这样删除中间的字符。因此,我们考虑用\(t\)串去和\(s\)串匹配,记录前缀和和后缀和,在所有前缀和\(+\)后缀和\(=\) \(t\)串长度的情况中找到最大值。
AC代码:
#include<bits/stdc++.h>
#define SIZE 500010
#define rep(i, a, b) for(long long i = a; i <= b; ++i)
using namespace std;
typedef long long ll;
void io() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
}
char s1[SIZE], s2[SIZE];
int pre[SIZE], post[SIZE], cnt, len1, len2, ans = 0;
int main() {
io(); cin >> (s1 + 1) >> (s2 + 1);
len1 = strlen(s1 + 1), len2 = strlen(s2 + 1);
for (int i = 1, j = 1; i <= len2; ++i) {
while (s1[j] != s2[i]) ++j;
pre[i] = j++;
}
for (int i = len2, j = len1; i; --i) {
while (s1[j] != s2[i]) --j;
post[i] = j--;
}
ans = max(len1 - pre[len2], post[1] - 1);
rep(i, 1, len2 - 1) ans = max(ans, post[i + 1] - pre[i] - 1);
cout << ans;
}
E. Boxers
题意:有\(n\)个拳击手,给出他们的体重\(a_i\),每个拳击手的体重可以加\(1\)或减\(1\)(体重必须是正整数),询问最多能选出几个体重不同的拳击手。
分析:由于数据很小,用一个布尔数组保存所有可能的体重取值,然后贪心。
AC代码:
#include<bits/stdc++.h>
#define SIZE 500010
#define rep(i, a, b) for(long long i = a; i <= b; ++i)
using namespace std;
typedef long long ll;
void io() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
}
ll n, m, k, t, x, num[SIZE];
bool a[SIZE];
int main() {
io(); cin >> n;
rep(i, 1, n) {
cin >> x;
a[x] = true;
num[x]++;
}
if (a[1]) a[2] = true;
rep(i, 2, 150001)
if (a[i])
a[i - 1] = a[i + 1] = true;
int cnt = 0;
rep(i, 1, 150000 + 1) {
if (a[i]) {
if (num[i - 1]) { num[i - 1]--; cnt++; }
else if(num[i]) { num[i]--; cnt++; }
else if (num[i + 1]) { num[i + 1]--; cnt++; }
}
}
cout << cnt;
}
F1. Complete the Projects (easy version)
题意:给定\(n\)个项目,和一个初始权值\(r\)。每个项目有两个数值构成,\(a\)表示要进行这个项目的最低权值需求,\(b\)表示进行完这个项目后权值将会变化的数值。询问能否完成所有的项目。
分析:首先我们肯定先做权值变化为正的项目,然后再做变化为负的项目。对于变化为正的项目,我们以\(a\)为关键字排序,对于变化为负的项目,我们以\(a+b\)为关键字排序。下证为什么以\(a+b\)为关键字排序:
我们考虑两项工作\(A_1\)和\(A_2\),如果排序时\(A_1\)在前,则有$$a_1 \leq r$$ $$a_2 \leq r+b_1$$
同理,对于\(A_2\)在前时有$$a_2 \leq r$$ $$a_1 \leq r+b_2$$
转化为$$max(a_1, a_2-b_1) \leq r$$ $$max(a_2, a_1-b_2) \leq r$$
由于我们需要让\(A_1\)在前时更优,于是有\(max(a_1, a_2-b_1) \leq max(a_2, a_1-b_2)\)。分析后得到这个式子等价于\(a_2+b_2 \leq a_1+b_1\)
AC代码:
#include <bits/stdc++.h>
#define SIZE 200010
#define rep(i, a, b) for(int i = a; i <= b; ++i)
using namespace std;
typedef long long ll;
ll n, r, x, y;
void io() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
}
struct node {
ll a, b;
}p1[SIZE], p2[SIZE], tp;
bool cmp1(node a, node b) {
return a.a < b.a;
}
bool cmp2(node a, node b) {
return a.a + a.b > b.a + b.b;
}
int main(){
io(); cin >> n >> r;
int j = 1, k = 1;
rep (i, 1, n) {
cin >> tp.a >> tp.b;
if (tp.b >= 0) p1[j++] = tp;
else p2[k++] = tp;
}
sort(p1 + 1, p1 + j, cmp1);
rep (i, 1, j - 1) {
if (r < p1[i].a) { cout << "NO"; return 0; }
else r += p1[i].b;
}
sort(p2 + 1, p2 + k, cmp2);
rep (i, 1, k - 1) {
if (r < p2[i].a) { cout << "NO"; return 0; }
else r += p2[i].b;
}
if (r >= 0) cout << "YES";
else cout << "NO";
}
F2. Complete the Projects (hard version):
题意:和easy version基本一样,就是询问变成了最多能做几个项目。
分析:先排序贪心,然后取负数项目时dp。
AC代码:
#include <bits/stdc++.h>
#define SIZE 200010
#define rep(i, a, b) for(long long i = a; i <= b; ++i)
using namespace std;
typedef long long ll;
void io() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
}
int n, r;
struct node {
ll a, b;
}p1[SIZE], p2[SIZE], tp;
bool cmp1(node a, node b) {
return a.a < b.a;
}
bool cmp2(node a, node b) {
return a.a + a.b < b.a + b.b;
}
int main() {
io(); cin >> n >> r;
int j = 1, k = 1, cnt = 0;
rep(i, 1, n) {
cin >> tp.a >> tp.b;
if (tp.b >= 0) p1[j++] = tp;
else p2[k++] = tp;
}
sort(p1 + 1, p1 + j, cmp1);
rep(i, 1, j - 1) {
if (r < p1[i].a) { continue; }
else r += p1[i].b;
cnt++;
}
sort(p2 + 1, p2 + k, cmp2);
vector<int> dp(r + 1);
rep(i, 1, k - 1) {
for (int j = r; j >= max(p2[i].a, abs(p2[i].b)); --j) {
dp[j] = max(dp[j], dp[j + p2[i].b] + 1);
}
}
cout << cnt + dp[r];
}
Codeforces Round #579 (Div. 3) 题解的更多相关文章
- Codeforces Round #182 (Div. 1)题解【ABCD】
Codeforces Round #182 (Div. 1)题解 A题:Yaroslav and Sequence1 题意: 给你\(2*n+1\)个元素,你每次可以进行无数种操作,每次操作必须选择其 ...
- Codeforces Round #608 (Div. 2) 题解
目录 Codeforces Round #608 (Div. 2) 题解 前言 A. Suits 题意 做法 程序 B. Blocks 题意 做法 程序 C. Shawarma Tent 题意 做法 ...
- Codeforces Round #579 (Div. 3)
Codeforces Round #579 (Div. 3) 传送门 A. Circle of Students 这题我是直接把正序.逆序的两种放在数组里面直接判断. Code #include &l ...
- Codeforces Round #525 (Div. 2)题解
Codeforces Round #525 (Div. 2)题解 题解 CF1088A [Ehab and another construction problem] 依据题意枚举即可 # inclu ...
- Codeforces Round #528 (Div. 2)题解
Codeforces Round #528 (Div. 2)题解 A. Right-Left Cipher 很明显这道题按题意逆序解码即可 Code: # include <bits/stdc+ ...
- Codeforces Round #466 (Div. 2) 题解940A 940B 940C 940D 940E 940F
Codeforces Round #466 (Div. 2) 题解 A.Points on the line 题目大意: 给你一个数列,定义数列的权值为最大值减去最小值,问最少删除几个数,使得数列的权 ...
- Codeforces Round #677 (Div. 3) 题解
Codeforces Round #677 (Div. 3) 题解 A. Boring Apartments 题目 题解 简单签到题,直接数,小于这个数的\(+10\). 代码 #include &l ...
- Codeforces Round #665 (Div. 2) 题解
Codeforces Round #665 (Div. 2) 题解 写得有点晚了,估计都官方题解看完切掉了,没人看我的了qaq. 目录 Codeforces Round #665 (Div. 2) 题 ...
- Codeforces Round #160 (Div. 1) 题解【ABCD】
Codeforces Round #160 (Div. 1) A - Maxim and Discounts 题意 给你n个折扣,m个物品,每个折扣都可以使用无限次,每次你使用第i个折扣的时候,你必须 ...
随机推荐
- js中事件代理(委托)
var oul = document.getElementById(‘uli’); oul.onclick = function(e) { e = e || window.event; var tar ...
- Vue组件中的Data为什么是函数。
简单点说,组件是要复用的,在很多地方都会调用. 如果data不是函数,而是属性,就又可能会发生多个地方的相同组件操作同一个Data属性,导致数据混乱. 而如果是函数,因为组件data函数的返回值是 ...
- poj1321棋盘问题(dfs+摆放问题)
在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的摆放方案C. ...
- opencv:程序运行完保持dos窗口不关闭
(1)在main函数最后加上 system("pause"); 第一种不能加到含有imshow图片显示的结尾:否则会不能显示图片: (2)利用cvWaitKey()函数: 这种能加 ...
- JUC-多线程锁
多线程锁的练习题 1.标准访问,先打印短信还是邮件 class Phone { public synchronized void sendSMS() throws Exception { Thread ...
- Controller-->View传值的几种方法
Controller --- > View 传值的方法 ViewBag ViewData TempData 向普通View页面传一个Model对象 向强类型页面传传一个Model对象 用一个Vi ...
- [CF276B] Little Girl and Game
[CF276B] Description 给定字符串 \(S\) ,两人轮流,每次从字符串中任意取出一个字符并从原串中删去.如果某人某次操作前,串种剩余的字符集合经过排列可以得到回文串,那么这个人就胜 ...
- [NOI2010] 超级钢琴 - 贪心,堆,ST表
这也算是第K大问题的套路题了(虽然我一开始还想了个假算法),大体想法就是先弄出最优的一批解,然后每次从中提出一个最优解并转移到一个次优解,用优先队列维护这个过程即可. 类似的问题很多,放在序列上的,放 ...
- Myeclipse异常
打不开文件 问题描述:Myeclipse然打开什么东西都报错了:Could not open the editor: Invalid thread access 解决方法:1.cmd 2.cd 进入你 ...
- 自制yum源离线安装ansible
适应场景 在实际生产环境中,服务器往往是不能访问互联网,如果简单的下载ansible源码安装,会碰到缺少各种依赖包的问题,因此,推荐制作yum源,然后使用yum安装ansible. 实验环境 模拟可以 ...