• Codeforces Round 928 (Div. 4) (A-E)


    比赛地址 : 

    https://codeforces.com/contest/1926

    遍历每一个字符串,比较1和0的数量即可,那个大输出那个;

    1. #include<bits/stdc++.h>
    2. #define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    3. #define endl '\n'
    4. #define lowbit(x) (x&(-x))
    5. #define sz(a) (int)a.size()
    6. #define pb push_back
    7. #define all(a) a.begin(), a.end()
    8. #define int long long
    9. typedef long long LL;
    10. const int mod = 1e9+7;
    11. const int N = 2e5+10;
    12. using namespace std;
    13. inline void solve(){
    14. int n ; cin >> n ;
    15. for(int i=1;i<=n;i++){
    16. string s ; cin >> s ;
    17. int t = 0 ;
    18. for(char c : s){
    19. if(c=='A') t++;
    20. }
    21. if(t>=3) cout << "A" << endl;
    22. else cout << "B" << endl;
    23. }
    24. }
    25. signed main()
    26. {
    27. IOS
    28. int _ = 1;
    29. // cin >> _;
    30. while(_ --) solve();
    31. return 0;
    32. }

    B . 

    模拟,先找第一个出现的'1' , 然后求这一列1的个数(也就是正方形的一条边) : n,然后求'1'的总个数: s,判断n*n==s && 右下角那个点也是1,就能够判断是不是正方形,否则就是三角形 ;

    1. #include<bits/stdc++.h>
    2. #define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    3. #define endl '\n'
    4. #define lowbit(x) (x&(-x))
    5. #define sz(a) (int)a.size()
    6. #define pb push_back
    7. #define all(a) a.begin(), a.end()
    8. #define int long long
    9. typedef long long LL;
    10. const int mod = 1e9 + 7;
    11. const int N = 2e5 + 10;
    12. using namespace std;
    13. char a[13][13] ;
    14. inline void solve() {
    15. int n ; cin >> n ;
    16. int t = 0 ;
    17. bool tag = true;
    18. int x = 0 , y = 0 ;
    19. for(int i=1;i<=n;i++){
    20. for(int j=1;j<=n;j++){
    21. cin >> a[i][j] ;
    22. if(a[i][j]=='1' && tag){
    23. x = i ;
    24. y = j ;
    25. tag = false ;
    26. }
    27. if(a[i][j] == '1') t++;
    28. }
    29. }
    30. bool st = false ;
    31. int b = 0 ;
    32. for(int j=y;j<=n;j++){
    33. if(a[x][j]=='1') b++;// 长度
    34. }
    35. if(x+b-1<=n&&y+b-1<=n&&a[x+b-1][y+b-1]=='1') st = true ;
    36. if(t==b*b && st) cout << "SQUARE" << endl;
    37. else cout << "TRIANGLE" << endl ;
    38. }
    39. signed main()
    40. {
    41. IOS
    42. int _ = 1;
    43. cin >> _;
    44. while (_--) solve();
    45. return 0;
    46. }

    C

    前缀和 + 预处理

    可以通过前缀和进行预处理,为O(2e5+n),然后对于每一个数据,都能够通过O(1)进行输出 ;

    1. #include<bits/stdc++.h>
    2. using namespace std;
    3. typedef long long LL ;
    4. const int N = 2e5 + 10 ;
    5. LL a[N] ;
    6. int get(int x){
    7. int t = 0 ;
    8. while(x) t+=x%10,x/=10;
    9. return t ;
    10. }
    11. void init(){
    12. a[1] = 1 ;
    13. for(int i=2;i<=2e5;i++){
    14. a[i] = a[i-1] + get(i) ;
    15. }
    16. }
    17. int main() {
    18. int tt ; cin >> tt ;
    19. init() ;
    20. while(tt--){
    21. int n ; cin >> n ;
    22. cout << a[n] << endl;
    23. }
    24. return 0 ;
    25. }

    数学 推式子

    分情况讨论,推出数学式子 , 然后算出答案 ;

    1. #include<bits/stdc++.h>
    2. #define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    3. #define endl '\n'
    4. #define lowbit(x) (x&(-x))
    5. #define sz(a) (int)a.size()
    6. #define pb push_back
    7. #define all(a) a.begin(), a.end()
    8. typedef long long LL;
    9. const int mod = 1e9+7;
    10. const int N = 2e5+10;
    11. using namespace std;
    12. LL get0(int n){
    13. LL ans = (n+1)*n/2 ;
    14. return ans ;
    15. }
    16. LL get1(int n){
    17. int x = n / 10 ;
    18. int y = n % 10 ;
    19. LL t = x * (x-1) / 2 * 10 + 45 * x + get0(y) + (y+1) * x;
    20. return t ;
    21. }
    22. LL get2(int n){
    23. int x = n / 100 ;
    24. int y = n % 100 ;
    25. LL t = get1(y) + x * (x-1) / 2 * 100 + x * get1(99) + (y+1) * x;
    26. return t ;
    27. }
    28. LL get3(int n){
    29. int x = n / 1000 ;
    30. int y = n % 1000 ;
    31. LL t = get2(y) + x * (x-1) / 2 * 1000 + x * get2(999) + (y+1) * x;
    32. return t ;
    33. }
    34. LL get4(int n){
    35. int x = n / 10000 ;
    36. int y = n % 10000 ;
    37. LL t = get3(y) + x * (x-1) / 2 * 10000 + x * get3(9999) + (y+1) * x;
    38. return t ;
    39. }
    40. inline void solve(){
    41. int n ; cin >> n ;
    42. if(n<10) {
    43. cout << (n+1)*n/2 << endl;
    44. return ;
    45. }
    46. if(n>=10 && n<100){
    47. int x = n / 10 ;
    48. int y = n % 10 ;
    49. LL t = x * (x-1) / 2 * 10 + 45 * x + get0(y) + (y+1) * x;
    50. cout << t << endl;
    51. return ;
    52. }
    53. if(n>=100 && n<1000){
    54. int x = n / 100 ;
    55. int y = n % 100 ;
    56. LL t = get1(y) + x * (x-1) / 2 * 100 + x * get1(99) + (y+1) * x;
    57. cout << t << endl;
    58. return ;
    59. }
    60. if(n>=1000 && n<10000){
    61. int x = n / 1000 ;
    62. int y = n % 1000 ;
    63. LL t = get2(y) + x * (x-1) / 2 * 1000 + x * get2(999) + (y+1) * x;
    64. cout << t << endl;
    65. return ;
    66. }
    67. if(n>=10000 && n<100000){
    68. int x = n / 10000 ;
    69. int y = n % 10000 ;
    70. LL t = get3(y) + x * (x-1) / 2 * 10000 + x * get3(9999) + (y+1) * x;
    71. cout << t << endl;
    72. return ;
    73. }
    74. if(n>=100000 && n<1000000){
    75. int x = n / 100000 ;
    76. int y = n % 100000 ;
    77. LL t = get4(y) + x * (x-1) / 2 * 100000 + x * get4(99999) + (y+1) * x;
    78. cout << t << endl;
    79. return ;
    80. }
    81. }
    82. signed main()
    83. {
    84. IOS
    85. int _ = 1;
    86. cin >> _;
    87. while(_ --) solve();
    88. return 0;
    89. }

    D

    哈希表 + 位运算

    对于每个数来说,能与它一组的有且仅有二进制在低31位上的31位都完全相反这一个数,那么可以得到一个组最多两个数字,对于x,那么能够与它配对的数字也就是 : ((1<<31)-1) ^ x ,其中((1<<31)-1)代表2^31-1,其二进制的低31位全是一;

    通过上面就可以一遍遍历,用map记录之前出现过的数,遇到能匹配的,就删掉一个 ;

    1. int yy = (1<<31) - 1 ;
    2. / 一对中的所有位都不同的条件等价于当我们对 2个数进行异或时,该对产生的数的所有位都设置为 1
    3. inline void solve2(){
    4. int n ; cin >> n ;
    5. map<int,int> mp ;
    6. int ans = 0 ;
    7. for(int i=1;i<=n;i++){
    8. int x ; cin >> x ;
    9. if(!mp[x]) ++ans,++mp[yy^x];
    10. else --mp[x] ;
    11. }
    12. cout << ans << endl ;
    13. }

    哈希表 + 字符串

    如果不精通位运算,就可以直接采取hash表存字符串的方式;

    这里采用set + map进行模拟 ;

    1. LL a[N] ;
    2. string get(string s){
    3. string str = "" ;
    4. for(char c : s) str += c == '1' ? '0' : '1' ;
    5. return str;
    6. }
    7. inline void solve(){
    8. int n ; cin >> n ;
    9. for(int i=1;i<=n;i++) cin >> a[i] ;
    10. set<string> st ;
    11. map<string,int> mp ;
    12. int x = 0 ;
    13. for(int i=1;i<=n;i++){
    14. string s = bitset<32>(a[i]).to_string();
    15. s = s.substr(1);
    16. string str = get(s) ;
    17. if(st.count(str)){
    18. x++;
    19. if(mp[str]>1){
    20. mp[str]--;
    21. }else{
    22. st.erase(str);
    23. mp[str]--;
    24. }
    25. }else{
    26. st.insert(s) ;
    27. mp[s]++;
    28. }
    29. }
    30. cout << n - x << endl ;
    31. }

    E

    思维题

    对于第一轮,就已经把全部的奇数放完了,那么之后的就全部都是偶数 ;

    永远不会在非2的幂的棋步上放下棋 ,因为非2的幂的步上,一定包含一个奇数因子,会在第一轮就被放下 ;

    加入上一步还剩n个棋子,那么下一步一定会下其中的一半棋子,然后模拟就好了 ;

    1. #include<bits/stdc++.h>
    2. #define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    3. #define endl '\n'
    4. #define lowbit(x) (x&(-x))
    5. #define sz(a) (int)a.size()
    6. #define pb push_back
    7. #define all(a) a.begin(), a.end()
    8. #define int long long
    9. typedef long long LL;
    10. const int mod = 1e9+7;
    11. const int N = 2e5+10;
    12. using namespace std;
    13. // 第一轮 把奇数放完了,后面一定全是偶数
    14. // 永远不会在非2的幂的棋步上放下棋 ,因为非2的幂的步上,一定包含一个奇数因子,会在第一轮就被放下 ;
    15. void solve() {
    16. int n, k;
    17. cin >> n >> k;
    18. vector<int> v;
    19. while (n) {
    20. v.push_back((n + 1) / 2); //每次放奇数个
    21. n /= 2;
    22. }
    23. int tot = 0, pow2 = 1;
    24. for (int x : v) {
    25. if (tot < k && k <= tot + x) {
    26. cout << pow2 * (2 * (k - tot) - 1) << '\n';
    27. return;
    28. }
    29. tot += x;
    30. pow2 *= 2;
    31. }
    32. }
    33. signed main()
    34. {
    35. IOS
    36. int _ = 1;
    37. cin >> _;
    38. while(_ --) solve();
    39. return 0;
    40. }

  • 相关阅读:
    【计算机网络】IP协议(下)
    栈、队列应用
    Linux Shell脚本的10个有用的“面试问题和解答”
    使用Mybatis调整Db2的cfg
    1.7 信息化发展与应用
    git revert 简单用法【笔记】
    男孩姓卜取什么名字好听
    制作一个简单HTML校园网页(HTML+CSS)学校网站制作 校园网站设计与实现
    将一个无向图变成一个双联通图所需添加的最小边数
    [Model.py 01]Modification for creating terrain matrix.
  • 原文地址:https://blog.csdn.net/ros275229/article/details/136185407