0
点赞
收藏
分享

微信扫一扫

Codeforces Round #784 (Div. 4) ,CF全新第一把div4体验&题解


文章目录

  • ​​A. Division​​
  • ​​B. Triple​​
  • ​​C. Odd/Even Increments​​
  • ​​D. Colorful Stamp​​
  • ​​E. 2-Letter Strings​​
  • ​​F. Eating Candies​​
  • ​​G. Fall Down​​
  • ​​H. Maximal AND​​

不愧是div4, 打完还有一个多小时()

A. Division

  • ifelse判断就行

#include<bits/stdc++.h>
using namespace std;

int main(){
int T; cin>>T;
while(T--){
int x; cin>>x;
int t;
if(x <= 1399)t = 4;
else if(x <= 1599)t = 3;
else if(x <= 1899)t = 2;
else t = 1;
cout<<"Division "<<t<<"\n";
}
return 0;
}

B. Triple

  • 找出数组中出现次数大于3的数字,直接map统计就行

#include<bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(0), cin.tie(0),cout.tie(0)

int main(){
IOS;
int T; cin>>T;
while(T--){
int n; cin>>n;
map<int,int>ma;
for(int i = 1; i <= n; i++){
int x; cin>>x;
ma[x]++;
}
int cc = -1;
for(auto t : ma){
if(t.second>=3){
cc = t.first;
break;
}
}
cout<<cc<<"\n";
}
return 0;
}

C. Odd/Even Increments

  • 每次操作可以给所有下标偶数或下标奇数的数+1,求能否让序列奇偶性都一样
  • 判断下标奇,偶的数字的奇偶性是否分别相同即可

#include<bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(0), cin.tie(0),cout.tie(0)

int a[110];

int main(){
IOS;
int T; cin>>T;
while(T--){
int n; cin>>n;
for(int i = 1; i <= n; i++){
cin>>a[i];
}
int ok = 1;
for(int i = 3; i <= n; i+=2){
if(a[i]%2 != a[i-2]%2){ok = 0; break;}
}
for(int i = 4; i <= n; i+=2){
if(a[i]%2 != a[i-2]%2){ok = 0; break;}
}
if(ok)cout<<"YES\n";
else cout<<"NO\n";
}
return 0;
}

D. Colorful Stamp

  • 初始全部W,每次可以RB或者BR,求最后能否构造出给出的串
  • 可以发现RBRBRB或BRBRBR重叠盖一个,可以构造出任何的串,除非整个串都是R或者都是B才出不来。所以拿W作分界判断中间的RB串是否出现两个字母即可
  • 开始特判了1是NO,然而W也可以,调了好久。

#include<bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(0), cin.tie(0),cout.tie(0)

int a[110];

int main(){
IOS;
int T; cin>>T;
while(T--){
int n; cin>>n;
string s; cin>>s;
if(s.size()==1){
if(s[0]=='W')cout<<"YES\n";
else cout<<"NO\n";
continue;
}
int ok = 1;
int x = 0, y = 0;
for(int i = 0; i < s.size(); i++){
if(s[i]=='W'){
if(x+y==1){
ok = 0;
}
x = 0, y = 0;
}else{
if(s[i]=='R')x=1;
else y = 1;
}
}
if(x+y==1)ok = 0;
if(ok)cout<<"YES\n";
else cout<<"NO\n";
}
return 0;
}

E. 2-Letter Strings

  • 给出n个长为2的字符串,判断能构造多少对,满足只有一个字母位置不同
  • 对于当前串,考虑前面只有一个位置不同的个数,累加即可。
  • 注意开ll,不然会wa

#include<bits/stdc++.h>
using namespace std;

#define IOS ios::sync_with_stdio(0), cin.tie(0),cout.tie(0)
typedef long long LL;

LL a[110];

int main(){
IOS;
LL T; cin>>T;
while(T--){
LL n; cin>>n;
map<string, LL>ma;
map<char,LL>m1, m2;
LL ans = 0;
for(LL i = 1; i <= n; i++){
string s; cin>>s;
LL t = m1[s[0]]+m2[s[1]]-2*ma[s];
// cout<<"ttt: "<<s[0]<<" "<<s[1]<<" "<<ma[s]<<" "<<t<<"\n";
ans += t;
ma[s]++;
m1[s[0]]++;
m2[s[1]]++;
}
cout<<ans<<"\n";
}
return 0;
}

F. Eating Candies

  • 两个人分别从左右往中间拿糖果,且他们糖果数必须一样,求最多可以拿多少糖果
  • 双指针模拟即可

#include<bits/stdc++.h>
using namespace std;

#define IOS ios::sync_with_stdio(0), cin.tie(0),cout.tie(0)
typedef long long LL;
const int maxn = 2e5+10;

int a[maxn];

int main(){
IOS;
int T; cin>>T;
while(T--){
int n; cin>>n;
for(int i = 1; i <= n; i++)cin>>a[i];
int l = 1, r = n;
int lsum = 0, rsum = 0, ans = 0;
while(l <= r){
if(lsum <= rsum)lsum += a[l++];
else rsum += a[r--];
if(lsum == rsum)ans = l+(n-r-1);
}
cout<<ans<<"\n";
}
return 0;
}

G. Fall Down

  • 给出一个由*,o,.构成的矩阵,*会往下掉,除非遇到o或者叠满了,求最后的效果
  • 暴力模拟即可,每次数有几个直到遇到o,然后o往上都暴力修改一下。

#include<bits/stdc++.h>
using namespace std;

#define IOS ios::sync_with_stdio(0), cin.tie(0),cout.tie(0)
typedef long long LL;
const int maxn = 2e5+10;

string a[70];

int main(){
IOS;
int T; cin>>T;
while(T--){
int n, m; cin>>n>>m;
for(int i = 0; i < n; i++)cin>>a[i];
for(int j = 0; j < m; j++){
int c = 0;
for(int i = 0; i < n; i++){
if(a[i][j]=='*'){
c++; a[i][j] = '.';
}else if(a[i][j]=='o'){
for(int k = i-c; k < i; k++){
a[k][j] = '*';
}
c = 0;
}
}
if(c){
for(int k = n-c; k < n; k++){
a[k][j] = '*';
}
c = 0;
}
}
for(int i = 0; i < n; i++)cout<<a[i]<<"\n";
cout<<"\n";
}
return 0;
}

H. Maximal AND

  • 给出n个数,可以操作k次,每次可以把一个数的一个二进制位改为1,求最后n个数AND起来最大是多少
  • 从最高位往下枚举,如果当前位0的个数<k,那么就把他们都变成1,用掉相应操作次数,直到不能操作即可。

#include<bits/stdc++.h>
using namespace std;

#define IOS ios::sync_with_stdio(0), cin.tie(0),cout.tie(0)
typedef long long LL;
const int maxn = 2e5+10;

int a[35];

int main(){
IOS;
int T; cin>>T;
while(T--){
int n, k; cin>>n>>k;
memset(a,0,sizeof(a));
for(int i = 1; i <= n; i++){
int x; cin>>x;
for(int j = 30; j >= 0; j--){
if((x>>j)&1 == 1)a[j]++;
}
}
int ans = 0;
for(int j = 30; j >= 0; j--){
// cout<<a[j]<<' ';
int t = n-a[j];
if(k >= t){
k -= t;
ans += (1<<j);
}
}
cout<<ans<<"\n";
}
return 0;
}


举报

相关推荐

0 条评论