Codeforces Round #821 Div. 2
没有打比赛,纯赛后刷题
A - Consecutive Sum (800)
简单思维,找到下标mod k的范围内最大的一组数即可
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 110;
int n, k;
int a[N];
void colve(){
memset(a, 0, sizeof a);
cin>>n>>k;
for(int i = 0; i < n; i ++ ){
int t;
cin>>t;
a[i % k] = max(t, a[i % k]);
}
int ans = 0;
for(int i = 0; i < k; i ++ ){
ans += a[i];
}
cout<<ans<<endl;
}
signed main()
{
int T;
cin>>T;
while(T -- ){
colve();
}
return 0;
}
B - Rule of League (900)
思维题,首先排除不合法方案,在x和y都不是0的情况下,一定是不合理方案,所以在合理方案的情况下,遍历i,保证每个数输出的次数不大于x/y就行
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e5 + 10;
int n;
int x, y;
int a[N]; //记录下标i出现的次数
int b[N]; //放数
void colve(){
cin>>n>>x>>y;
if(x == 0 && y == 0){
cout<<"-1"<<endl;
return ;
}
else if(x == 0 || y == 0){ //只有在这两种情况下才有合理地安排
if(x < y) swap(x, y); //保证x是哪个非0数
if((n - 1) % x != 0){
cout<<"-1"<<endl;
return ;
}
int res = 1;
memset(a, 0, sizeof a);
for(int i = 1; i <= n - 1; i ++ ){
cout<<res<<' ';
a[res] ++ ; //记录这个数出现的次数
if(a[res] >= x){
res = i + 2;
}
}
cout<<endl;
return ;
}
else{ //排不出合理的安排
cout<<"-1"<<endl;
return ;
}
return ;
}
signed main()
{
int T;
cin>>T;
while(T -- ){
colve();
}
return 0;
}
C - Parity Shuffle Sorting (1300)
注意细节
#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int>PII;
const int N = 1e6 + 10;
int T;
int n;
int a[N];
int c[N];
signed main()
{
cin>>T;
while(T -- ){
cin>>n;
int maxn = 0;
int minn = 1;
int res = 0;
vector<PII>b(n);
for(int i = 1; i <= n; i ++ ){
cin>>a[i];
c[i] = a[i] & 1;
maxn = max(c[i], maxn);
minn = min(minn, c[i]);
}
if(maxn == minn){
cout<<n - 1<<endl;
for(int i = n - 1; i >= 1; i -- ) cout<<i<<' '<<i + 1<<endl;
}
else{
if(a[1] != a[n]){
if((a[1] & 1) != (a[n] & 1)) a[n] = a[1];
else a[1] = a[n];
b[res ++ ] = {1, n};
}
if(a[1] & 1){
for(int i = 2; i <= n - 1; i ++ ){
if(a[i] & 1) b[res ++ ] = {i, n};
else b[res ++ ] = {1, i};
}
}
else{
for(int i = 2; i <= n - 1; i ++ ){
if(a[i] & 1) b[res ++ ] = {1, i};
else b[res ++ ] = {i, n};
}
}
cout<<res<<endl;
if(res > 0){
for(int i = 0; i < res; i ++ ){
cout<<b[i].first<<' '<<b[i].second<<endl;
}
}
}
}
return 0;
}
D1. Zero-One (Easy Version) (1400)
第一次自己写出来1400的题,有些激动,继续加油
思路:首先需要明确,两个字符串(分别以a、b代称)的不同的字符个数为奇数的情况下,只能输出-1,因为每次更改无论花费x还是y都只能更改两个字符。明确这种情况之后,还有区分a和b只有两个相邻的字符不同,这种情况下要比较x和2y谁的花费更大,2y就是让任一字符被替换两次,相当于没有替换。最后一种情况就是按照y花费计算输出即可
(一度有些怀疑这难度够不够1400)
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 6030;
int T;
int n, x, y;
char a[N], b[N];
int res;
int p[N];
int ioss; //记录相邻组数
bool ip[N];
signed main()
{
cin>>T;
while(T -- ){
//状态重置
res = 0;
ioss = 0;
memset(p, 0, sizeof p);
memset(ip, false, sizeof ip);
cin>>n>>x>>y;
scanf("%s%s", a + 1, b + 1);
for(int i = 1; i <= n; i ++ ){
if(a[i] != b[i]){
//p数组从下标1开始装数
p[ ++ res] = i;
}
}
if(res > 1){
for(int i = 1; i < res; i ++ ){
if(p[i] == p[i + 1] - 1 && !ip[i] && !ip[i + 1]){
ioss ++ ; //可相邻消的组数+1
ip[i] = true;
ip[i + 1] = true;
}
}
}
if(res & 1) cout<<"-1"<<endl;
else if(res == 0) cout<<0<<endl;
else if(res == 2){
int ans = y;
if(ioss == 1) ans = min(x, 2 * y); //如果可以相邻消
cout<<ans<<endl;
}
else{
int ans = (res / 2) * y;
cout<<ans<<endl;
}
}
return 0;
}
D2 - Zero-One (Hard Version) (2000)
对于x>=y的,参照D1,对于x<y的,无法用贪心,则用动态规划的思路:
大佬原题解
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 5010, INF = 0x3f3f3f3f3f3f3f;
int T;
int n, x, y;
char a[N], b[N];
int res;
int p[N];
int ioss; //记录相邻组数
bool ip[N];
int dp[N][N];
signed main()
{
cin>>T;
while(T -- ){
//状态重置
res = 0;
ioss = 0;
memset(p, 0, sizeof p);
memset(ip, false, sizeof ip);
int tt = 0;
cin>>n>>x>>y;
scanf("%s%s", a + 1, b + 1);
for(int i = 1; i <= n; i ++ ){
if(a[i] != b[i]){
//p数组从下标1开始装数
tt ++ ;
}
}
if(tt & 1){
cout<<"-1"<<endl;
continue;
}
if(x >= y){
for(int i = 1; i <= n; i ++ ){
if(a[i] != b[i]){
//p数组从下标1开始装数
p[ ++ res] = i;
}
}
if(res > 1){
for(int i = 1; i < res; i ++ ){
if(p[i] == p[i + 1] - 1 && !ip[i] && !ip[i + 1]){
ioss ++ ; //可相邻消的组数+1
ip[i] = true;
ip[i + 1] = true;
}
}
}
if(res & 1) cout<<"-1"<<endl;
else if(res == 0) cout<<0<<endl;
else if(res == 2){
int ans = y;
if(ioss == 1) ans = min(x, 2 * y); //如果可以相邻消
cout<<ans<<endl;
}
else{
int ans = (res / 2) * y;
cout<<ans<<endl;
}
}
else{
for(int i = 0; i <= n; i ++ ){
for(int j = 0; j <= n; j ++ ){
dp[i][j] = INF;
}
}
dp[0][0] = 0; //表示前i个字符,未匹配的字符数为i
int cur = 0;
for(int i = 1; i <= n; i ++ ){
p[i] = cur; //记录距离i最近的不相同的字符的下标
if(a[i] != b[i]) cur = i; //因为最近的是i-1,所以cur猴更新
}
for(int i = 1; i <= n; i ++ ){
for(int j = 0; j <= n; j ++ ){ //未匹配字符的数量从0~n
if(a[i] == b[i]) dp[i][j] = min(dp[i][j], dp[i - 1][j]); //如果字符相同,则只用继承无需状态转移
else{ //如果字符不相同,则会有状态转移
if(p[i] == 0) dp[i][j + 1] = min(dp[i][j + 1], dp[i - 1][j]);
else{
dp[i][j + 1] = min(dp[i][j + 1], dp[i - 1][j]);
dp[i][j] = min(dp[i][j], dp[p[i] - 1][j] + min(x * (i - p[i]), 2 * y));
if(i - p[i] != 1){
dp[i][j] = min(dp[i][j], dp[p[i] - 1][j] + y);
}
}
}
}
}
int ans = INF;
for(int i = 0; i <= n; i ++ ){
if(i & 1) continue;
ans = min(ans, dp[n][i] + (i / 2) * y);
}
cout<<ans<<endl;
}
}
return 0;
}
第一次纯补题补完一场比赛,有知识收获也有自信心收获(特指自己过了一道1400的题)
网络预选赛被打击了,省赛之前冲一波,好好刷刷题,争取一天一场cf,加油加油冲冲冲!!!!