Codeforces Round 928 (Div. 4)(A~E)

2024-02-20 07:12
文章标签 codeforces round div 928

本文主要是介绍Codeforces Round 928 (Div. 4)(A~E),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

A. Vlad and the Best of Five

输出A和B那个出现的更多。

#include <bits/stdc++.h>
//#define int long long
#define per(i,j,k) for(int (i)=(j);(i)<=(k);++(i))
#define rep(i,j,k) for(int (i)=(j);(i)>=(k);--(i))
#define fr first
#define se second
#define endl '\n'
using namespace std;void solve(){string s;cin>>s;int a=0,b=0;per(i,0,s.length()-1){if(s[i]=='A')a++;else b++;}if(a>=b)cout<<"A"<<endl;else cout<<"B"<<endl;
}void init(){}
signed main(){ios::sync_with_stdio(false),cin.tie(nullptr);int t;cin>>t;while(t--)solve(),init();return 0;
}

B. Vlad and Shapes

问给定的01矩阵中,1组成的三角形还是正方形。

取最早的1和最晚的1,然后暴力循环一下,看看里面是不是都是1。

#include <bits/stdc++.h>
//#define int long long
#define per(i,j,k) for(int (i)=(j);(i)<=(k);++(i))
#define rep(i,j,k) for(int (i)=(j);(i)>=(k);--(i))
#define fr first
#define se second
#define endl '\n'
using namespace std;int n;
pair<int,int>x,y;
string s[15];void solve(){cin>>n;per(i,1,n)cin>>s[i];x.fr=-1;per(i,1,n){per(j,0,n-1){if(x.fr==-1 and s[i][j]=='1'){x={i,j};}if(s[i][j]=='1')y={i,j};}}if(y.se-x.se==y.fr-x.fr){per(i,x.fr,y.fr){per(j,x.se,y.se){if(s[i][j]=='0'){cout<<"TRIANGLE"<<endl;return;}}}}cout<<"SQUARE"<<endl;
}void init(){per(i,1,n)s[i]="";
}
signed main(){ios::sync_with_stdio(false),cin.tie(nullptr);int t;cin>>t;while(t--)solve(),init();return 0;
}

C. Vlad and a Sum of Sum of Digits

每个问题的测试只给0.5s的时间,要求输出1~n所有数按位加在一起,如521=5+2+1。

可以预处理一下每个数的按位和,然后再进行前缀和。

#include <bits/stdc++.h>
//#define int long long
#define per(i,j,k) for(int (i)=(j);(i)<=(k);++(i))
#define rep(i,j,k) for(int (i)=(j);(i)>=(k);--(i))
#define fr first
#define se second
#define endl '\n'
using namespace std;
const int N=2e5+5;int n,pre[N];
map<int,int>f;void solve(){cin>>n;cout<<pre[n]<<endl;
}void init(){}
signed main(){ios::sync_with_stdio(false),cin.tie(nullptr);int t;cin>>t;per(i,1,2e5){int tmp=i;int sum=0;while(tmp){sum+=tmp%10;tmp/=10;}f[i]=sum;}per(i,1,2e5){pre[i]=pre[i-1]+f[i];}while(t--)solve(),init();return 0;
}

D. Vlad and Division

如果数A和数B在二进制的每一位(32位以内,int上限大小)上都不同,那么可以把他们放在同一组里面,问N个数最少要分几个组。

因为每个位都要不同,所以可以直接按位取反(异或即可)从map中看有没有能配对的。

#include <bits/stdc++.h>
#define int long long
#define per(i,j,k) for(int (i)=(j);(i)<=(k);++(i))
#define rep(i,j,k) for(int (i)=(j);(i)>=(k);--(i))
#define fr first
#define se second
#define endl '\n'
using namespace std;
const int N=2e5+5;int n,tmp,ans;
map<int,int>f;void solve(){cin>>n;per(i,1,n){cin>>tmp;f[tmp]++;}for(auto i:f){if(f[i.fr xor INT_MAX]){ans+=min(i.se,f[i.fr xor INT_MAX])+abs(f[i.fr xor INT_MAX]-i.se);f[i.fr xor INT_MAX]=0;f[i.fr]=0;}else{ans+=i.se;f[i.fr]=0;}}cout<<ans<<endl;
}void init(){f.clear();ans=0;
}
signed main(){ios::sync_with_stdio(false),cin.tie(nullptr);int t;cin>>t;while(t--)solve(),init();return 0;
}

E. Vlad and an Odd Ordering

对于1~N的数进行翻牌,每个数只翻1遍,问翻的第k个数是多少。按照下列规则来翻

第一次翻1 3 5 7 9(所有到N为止的奇数)

第二次翻2 6 10 14(+4)

第三次翻3 9 15 21(+6)

第四次翻4 12 20 28(+8)

以此类推。

但是我们可以发现第一次就把奇数翻完了,下面的第奇数次翻是没有意义的。

还剩下偶数部分

2 6 10 14

4 .....

6 .....

8 .....

10 .....

2后面的6都是+4变大,下面的6是+12变大,所以6开头的被2里面的全部覆盖。

2后面的10都是+4变大,下面的10是+20变大,所以10开头也被2里面的全部覆盖。

所以大胆猜测,只要出现过了,下面再翻就会全部重复。

在小范围内测试一下这个思路,并且按顺序输出翻的开头不重复的值。

    for(int i=2;i<=1e4;i+=2){if(!vis[i])cout<<i<<endl;vis[i]=true;for(int j=i;j<=1e4;j+=2*i){vis[j]=true;}}

得出结果开头都是2幂次

知道开头翻什么之后不会重复,间隔也知道,就可以得出第k个翻的数的值了。

若k属于第一次翻的范围内,则数x=(k-1)*4+2,且在n范围内一共有n/2+n%2个数可以翻(奇数整除后要加一,直接加n%2即可)

如果不在第一次翻的范围内,则k减去n/2+n%2,从开头为4的地方,重新计算x值。

即x=(k-1)*8+4

以此类推。

#include <bits/stdc++.h>
#define int long long
#define per(i,j,k) for(int (i)=(j);(i)<=(k);++(i))
#define rep(i,j,k) for(int (i)=(j);(i)>=(k);--(i))
#define fr first
#define se second
#define endl '\n'
using namespace std;int n,k;void solve(){cin>>n>>k;if(k<=n/2+n%2){cout<<(k-1)*2+1<<endl;}else{k-=n/2+n%2;int now=2;while(k>(n-now)/(now*2)+1){//当前范围内有多少now*2可以叠加k-=(n-now)/(now*2)+1;//(n-now,前面那部分不算),+1是以now开头的那张nownow*=2;}cout<<(k-1)*now*2+now<<endl;//公式定位}
}void init(){}signed main(){ios::sync_with_stdio(false),cin.tie(nullptr);int t;cin>>t;while(t--)solve(),init();return 0;
}

这篇关于Codeforces Round 928 (Div. 4)(A~E)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/727418

相关文章

Codeforces Round #240 (Div. 2) E分治算法探究1

Codeforces Round #240 (Div. 2) E  http://codeforces.com/contest/415/problem/E 2^n个数,每次操作将其分成2^q份,对于每一份内部的数进行翻转(逆序),每次操作完后输出操作后新序列的逆序对数。 图一:  划分子问题。 图二: 分而治之,=>  合并 。 图三: 回溯:

Codeforces Round #261 (Div. 2)小记

A  XX注意最后输出满足条件,我也不知道为什么写的这么长。 #define X first#define Y secondvector<pair<int , int> > a ;int can(pair<int , int> c){return -1000 <= c.X && c.X <= 1000&& -1000 <= c.Y && c.Y <= 1000 ;}int m

Codeforces Beta Round #47 C凸包 (最终写法)

题意慢慢看。 typedef long long LL ;int cmp(double x){if(fabs(x) < 1e-8) return 0 ;return x > 0 ? 1 : -1 ;}struct point{double x , y ;point(){}point(double _x , double _y):x(_x) , y(_y){}point op

Codeforces Round #113 (Div. 2) B 判断多边形是否在凸包内

题目点击打开链接 凸多边形A, 多边形B, 判断B是否严格在A内。  注意AB有重点 。  将A,B上的点合在一起求凸包,如果凸包上的点是B的某个点,则B肯定不在A内。 或者说B上的某点在凸包的边上则也说明B不严格在A里面。 这个处理有个巧妙的方法,只需在求凸包的时候, <=  改成< 也就是说凸包一条边上的所有点都重复点都记录在凸包里面了。 另外不能去重点。 int

Codeforces 482B 线段树

求是否存在这样的n个数; m次操作,每次操作就是三个数 l ,r,val          a[l] & a[l+1] &......&a[r] = val 就是区间l---r上的与的值为val 。 也就是意味着区间[L , R] 每个数要执行 | val 操作  最后判断  a[l] & a[l+1] &......&a[r] 是否= val import ja

CSS实现DIV三角形

本文内容收集来自网络 #triangle-up {width: 0;height: 0;border-left: 50px solid transparent;border-right: 50px solid transparent;border-bottom: 100px solid red;} #triangle-down {width: 0;height: 0;bor

创建一个大的DIV,里面的包含两个DIV是可以自由移动

创建一个大的DIV,里面的包含两个DIV是可以自由移动 <body>         <div style="position: relative; background:#DDF8CF;line-height: 50px"> <div style="text-align: center; width: 100%;padding-top: 0px;"><h3>定&nbsp;位&nbsp;

Codeforces Round 971 (Div. 4) (A~G1)

A、B题太简单,不做解释 C 对于 x y 两个方向,每一个方向至少需要 x / k 向上取整的步数,取最大值。 由于 x 方向先移动,假如 x 方向需要的步数多于 y 方向的步数,那么最后 y 方向的那一步就不需要了,答案减 1 代码 #include <iostream>#include <algorithm>#include <vector>#include <string>

CF#271 (Div. 2) D.(dp)

D. Flowers time limit per test 1.5 seconds memory limit per test 256 megabytes input standard input output standard output 题目链接: http://codeforces.com/contest/474/problem/D We s

CF #278 (Div. 2) B.(暴力枚举+推导公式+数学构造)

B. Candy Boxes time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output 题目链接: http://codeforces.com/contest/488/problem/B There