当前位置: 首页 > news >正文

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

比赛地址 : 

https://codeforces.com/contest/1926

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

#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl '\n'
#define lowbit(x) (x&(-x))
#define sz(a) (int)a.size()
#define pb push_back
#define all(a) a.begin(), a.end()
#define int long long
typedef long long LL;
const int mod = 1e9+7;
const int N = 2e5+10;using namespace std;inline void solve(){int n ; cin >> n ;for(int i=1;i<=n;i++){string s ; cin >> s ;int t = 0 ;for(char c : s){if(c=='A') t++;}if(t>=3) cout << "A" << endl;else cout << "B" << endl;}
}signed main()
{IOSint _ = 1;// cin >> _;while(_ --) solve();return 0;
}

B . 

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

#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl '\n'
#define lowbit(x) (x&(-x))
#define sz(a) (int)a.size()
#define pb push_back
#define all(a) a.begin(), a.end()
#define int long long
typedef long long LL;
const int mod = 1e9 + 7;
const int N = 2e5 + 10;using namespace std;char a[13][13] ;inline void solve() {int n ; cin >> n ;int t = 0 ;bool tag = true;int x = 0 , y = 0 ;for(int i=1;i<=n;i++){for(int j=1;j<=n;j++){cin >> a[i][j] ;if(a[i][j]=='1' && tag){x = i ;y = j ;tag = false ;}if(a[i][j] == '1') t++;}}bool st = false ;int b = 0 ;for(int j=y;j<=n;j++){if(a[x][j]=='1') b++;// 长度		}if(x+b-1<=n&&y+b-1<=n&&a[x+b-1][y+b-1]=='1') st = true ;if(t==b*b && st) cout << "SQUARE" << endl;else cout << "TRIANGLE" << endl ;}signed main()
{IOSint _ = 1;cin >> _;while (_--) solve();return 0;
}

C

前缀和 + 预处理

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

#include<bits/stdc++.h>
using namespace std;
typedef long long LL ;
const int N = 2e5 + 10 ;
LL a[N] ;int get(int x){int t = 0 ;while(x) t+=x%10,x/=10;return t ;
}void init(){a[1] = 1 ;for(int i=2;i<=2e5;i++){a[i] = a[i-1] + get(i) ;}
}int main() {int tt ; cin >> tt ;init() ;while(tt--){int n ; cin >> n ;cout << a[n] << endl;}return 0 ;
}

数学 推式子

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

#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl '\n'
#define lowbit(x) (x&(-x))
#define sz(a) (int)a.size()
#define pb push_back
#define all(a) a.begin(), a.end()
typedef long long LL;
const int mod = 1e9+7;
const int N = 2e5+10;using namespace std;LL get0(int n){LL ans = (n+1)*n/2 ;return ans ;
}
LL get1(int n){int x = n / 10 ;int y = n % 10 ;LL t = x * (x-1) / 2 * 10 + 45 * x + get0(y) + (y+1) * x;return t ;
}LL get2(int n){int x = n / 100 ;int y = n % 100 ;LL t = get1(y) + x * (x-1) / 2 * 100 + x * get1(99) + (y+1) * x;return t ;
}
LL get3(int n){int x = n / 1000 ;int y = n % 1000 ;LL t = get2(y) + x * (x-1) / 2 * 1000 + x * get2(999) + (y+1) * x;return t ;
}
LL get4(int n){int x = n / 10000 ;int y = n % 10000 ;LL t = get3(y) + x * (x-1) / 2 * 10000 + x * get3(9999) + (y+1) * x;return t ;
}inline void solve(){int n ; cin >> n ;if(n<10) {cout << (n+1)*n/2 << endl;return ;}if(n>=10 && n<100){int x = n / 10 ;int y = n % 10 ;LL t = x * (x-1) / 2 * 10 + 45 * x + get0(y) + (y+1) * x;cout << t << endl;return ;}if(n>=100 && n<1000){int x = n / 100 ;int y = n % 100 ;LL t = get1(y) + x * (x-1) / 2 * 100 + x * get1(99) + (y+1) * x;cout << t << endl;return ;}if(n>=1000 && n<10000){int x = n / 1000 ;int y = n % 1000 ;LL t = get2(y) + x * (x-1) / 2 * 1000 + x * get2(999) + (y+1) * x;cout << t << endl;return ;}if(n>=10000 && n<100000){int x = n / 10000 ;int y = n % 10000 ;LL t = get3(y) + x * (x-1) / 2 * 10000 + x * get3(9999) + (y+1) * x;cout << t << endl;return ;}if(n>=100000 && n<1000000){int x = n / 100000 ;int y = n % 100000 ;LL t = get4(y) + x * (x-1) / 2 * 100000 + x * get4(99999) + (y+1) * x;cout << t << endl;return ;}
}signed main()
{IOSint _ = 1;cin >> _;while(_ --) solve();return 0;
}

D

哈希表 + 位运算

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

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

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

哈希表 + 字符串

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

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

LL a[N] ;string get(string s){string str = "" ;for(char c : s) str += c == '1' ? '0' : '1' ;return str;
}inline void solve(){int n ; cin >> n ;for(int i=1;i<=n;i++) cin >> a[i] ;set<string> st ;map<string,int> mp ;int x = 0 ;for(int i=1;i<=n;i++){string s = bitset<32>(a[i]).to_string();s = s.substr(1);string str = get(s) ;if(st.count(str)){x++;if(mp[str]>1){mp[str]--;}else{st.erase(str);mp[str]--;}}else{st.insert(s) ;mp[s]++;	}  }cout << n - x << endl ;
}

E

思维题

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

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

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

#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl '\n'
#define lowbit(x) (x&(-x))
#define sz(a) (int)a.size()
#define pb push_back
#define all(a) a.begin(), a.end()
#define int long long
typedef long long LL;
const int mod = 1e9+7;
const int N = 2e5+10;using namespace std;// 第一轮 把奇数放完了,后面一定全是偶数
// 永远不会在非2的幂的棋步上放下棋 ,因为非2的幂的步上,一定包含一个奇数因子,会在第一轮就被放下 ;void solve() {int n, k;cin >> n >> k;vector<int> v;while (n) {v.push_back((n + 1) / 2); //每次放奇数个 n /= 2;}int tot = 0, pow2 = 1;for (int x : v) {if (tot < k && k <= tot + x) {cout << pow2 * (2 * (k - tot) - 1) << '\n';return;}tot += x;pow2 *= 2;}
}signed main()
{IOSint _ = 1;cin >> _;while(_ --) solve();return 0;
}

http://www.lryc.cn/news/304543.html

相关文章:

  • git远程操控gitee
  • 常见面试题:TCP的四次挥手和TCP的滑动窗口
  • 力扣随笔之两数之和 Ⅱ -输入有序数组(中等167)
  • 最优传输(Optimal Transport)
  • MIT-6.824-Lab2,Raft部分笔记|Use Go
  • 使用openeuler 22.03替代CentOS 7.9,建立虚拟机详细步骤
  • 代理技术引领出海征程
  • 谷粒商城篇章9 ---- P248-P261/P292-P294 ---- 消息队列【分布式高级篇六】
  • 【Spring连载】使用Spring Data访问 MongoDB(五)----生命周期事件
  • JavaSec 之 SQL 注入简单了解
  • 第十一章——期约与异步函数
  • 工具方法合集-utils.js
  • 安卓11-设置HDMI分辨率流程
  • Vue3+vite搭建基础架构(11)--- 菜单栏功能和Tab页功能实现
  • 餐饮神秘顾客公司:关于餐饮行业神秘顾客调查注意事项
  • 概率密度函数(PDF)与神经网络中的激活函数
  • .netcore 6.0/7.0项目迁移至.netcore 8.0 注意事项
  • 算法打卡day2|数组篇|Leetcode 977.有序数组的平方、 209.长度最小的子数组、59.螺旋矩阵II
  • Hive【内部表、外部表、临时表、分区表、分桶表】【总结】
  • 随手写的小程序2 一个nc能控制的程序
  • Android中通过属性动画实现文字轮播效果
  • 最长的回文串
  • 2023 H1 中国边缘公有云服务市场 Top2,百度智能云加速推动分布式云智能化升级
  • Emlog博客网站快速搭建并结合内网穿透实现远程访问本地站点
  • 力扣经典题目解析--旋转图像(字节二面)
  • 【ARMv8M Cortex-M33 系列 8.1 -- RT-Thread 堆内存 检查命令 free 实现及介绍】
  • milvus Delete API流程源码分析
  • CentOS使用Docker搭建Halo网站并实现无公网ip远程访问
  • 【JVM】垃圾回收算法
  • 如何和将原始request的Header中的值传递给openfeign请求的Header? 以及又如何获取openfeign请求中Header中的值