0
点赞
收藏
分享

微信扫一扫

Codeforces Round #807 (Div. 2) A--D

海滨公园 2022-07-18 阅读 72

A

Codeforces Round #807 (Div. 2)  A--D_#define

思路:排序之后,就将数据分割成前n和后n就行,

12345678->

5678

1234

这样一一对应相差值最大

/**/
#include <bits/stdc++.h>
#define rep(i, a, b) for(int i = a; i <= b; i ++)
#define endl '\n'
using namespace std;
const int N = 500 + 5;

void s(){
int n ,x; cin >> n >> x;

int a[N] = {}, a1[N] = {}, b1[N] = {};
for(int i = 1; i <= 2 * n ;i ++) cin >> a[i];

sort(a+1, a+1+n*2);


for(int i = 1; i <= n ;i ++) a1[i] = a[i];
rep(i, n + 1, 2*n) b1[i - n] = a[i]; //升序 b1是高的

for(int i = 1; i <= n; i++){
if(b1[i] - a1[i] < x) {
cout << "NO\n";return ;
}
}

cout << "YES\n";


}

int main(){

cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);

#ifdef LOCAL
cout << "记得更改input.txt!!!\n";
FILE *p = fopen("input.txt", "a+");
fclose(p);
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
int t; cin >> t; while(t-- )s();


return 0;
}

//提交代码删掉#define LOCAL

B

Codeforces Round #807 (Div. 2)  A--D_#ifdef_02




思路:

将一堆沙子往后移动需要有铺垫。

前缀和+(去掉前导0,如果最后a[n]是0也要去掉)0的个数 = 答案

/**/
#include <bits/stdc++.h>
#define rep(i, a, b) for(int i = a; i <= b; i ++)
#define endl '\n'
using namespace std;
const int N = 2e5 + 5;
long long sum[N] = {};
void s(){
int n ; cin >> n;

int a[N] = {};
memset(sum, 0, sizeof sum);
rep(i,1,n) cin >> a[i];

bool tag = 0;
int cnt = 0;
int id = 1;
while(a[id] == 0) id++;
rep(i,id,n){
sum[i] = sum[i - 1] + a[i];
if(a[i] == 0) cnt++;
}
if(a[n] == 0 && cnt != 0) cnt --;


cout << sum[n - 1] + cnt << endl;


}

int main(){

cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);

#ifdef LOCAL
cout << "记得更改input.txt!!!\n";
FILE *p = fopen("input.txt", "a+");
fclose(p);
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
int t; cin >> t;while(t--) s();


return 0;
}

//提交代码删掉#define LOCAL





C

Codeforces Round #807 (Div. 2)  A--D_#ifdef_03




思路:先记录每一个区间。

每一次询问都倒着推导k所在的位置。最终递推出k在1--2e5之间的值(也就是在原数据s中的值)



/**/
#include <bits/stdc++.h>
#define rep(i, a, b) for(int i = a; i <= b; i ++)
#define endl '\n'
using namespace std;
const int N = 2e5 + 5;
char s[N];
long long l[50], r[50];//c=40
long long sum[50];

void ss(){
int n , c , q;
cin >> n >> c >> q >> s + 1;
sum[0] = n;
for(int i = 1;i <= c;i ++){
cin >> l[i] >> r[i];//记录复制的区间
sum[i] = sum[i - 1] + r[i] - l[i] + 1;//sum[i]是当前多长了
}

while(q--){
long long k;
cin >> k;

for(int i = c; i >= 1; i --){//每一次询问都倒着查询。
if(k > sum[i - 1] && k <= sum[i]){//如果k落在前缀和区间内
k = l[i] + k - sum[i - 1] - 1;//定位到k的位置,循环不断的缩小k的值,最终k会落在1--2e5的范围,答案就是s[k]
}
}
cout << s[k] << endl;
}
}

int main(){

cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);

#ifdef LOCAL
cout << "记得更改input.txt!!!\n";
FILE *p = fopen("input.txt", "a+");
fclose(p);
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif


int t; cin >> t; while(t--) ss();


return 0;
}

//提交代码删掉#define LOCAL




D

Codeforces Round #807 (Div. 2)  A--D_ios_04






/**/
#include <bits/stdc++.h>
#define rep(i, a, b) for(int i = a; i <= b; i ++)
#define endl '\n'
using namespace std;
const int N = 2e5 + 5;

char s1[N], s2[N];

void s(){
int n ; cin >> n >> s1 + 1 >> s2 + 1;
if(s1[1] != s2[1] || s1[n] != s2[n]){//判断两头是否相等,不相等是永远无法处理的
cout << -1 << endl;
return ;
}


vector<pair<int, int>> v1, v2;
for(int i = 1; i <= n ;i ++){
if(s1[i] == '0') continue;
int j = i ;
while(j + 1 <= n && s1[j + 1] == '1') j ++;
v1.push_back({i, j});
i = j;
}

for(int i = 1; i <= n ;i ++){
if(s2[i] == '0') continue;
int j = i;
while(j + 1 <= n && s2[j + 1] == '1') j++;
v2.push_back({i, j});
i = j;
}

if(v1.size() != v2.size()){
cout << -1 << endl; return;
}

long long ans = 0;
for(int i = 0 ; i < v1.size(); i++){
pair<int, int> tmp1, tmp2;
tmp1 = v1[i];
tmp2 = v2[i];
int l1 = tmp1.first, r1 = tmp1.second, l2 = tmp2.first, r2 = tmp2.second;

ans += abs(l2 - l1) + abs(r2 - r1);
}

cout << ans << endl;
}

int main(){

cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);

#ifdef LOCAL
cout << "记得更改input.txt!!!\n";
FILE *p = fopen("input.txt", "a+");
fclose(p);
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
int t; cin >> t;
while(t--) s();


return 0;
}

//提交代码删掉#define LOCAL


举报

相关推荐

0 条评论