ABC291题解(D-G)
ABC291
D - Flip Cards
Solution:
考虑DP,定义状态\(F_{i,0}\)为第\(i\)张卡片正面朝上的方案数,\(F_{i,1}\)为第\(i\)张卡片背面朝上的方案数,每次check是否相同然后转移即可
int f[N][2];
int a[N];
int b[N];
void solve(){
int n;
cin >> n;
for (int i = 1;i <= n;i ++) {
cin >> a[i] >> b[i];
}
f[1][0] = 1;
f[1][1] = 1;
for (int i = 2;i <= n;i ++) {
f[i][0] = (f[i - 1][0] * (a[i - 1] != a[i]) + f[i - 1][1] * (b[i - 1] != a[i])) % mod;
f[i][1] = (f[i - 1][0] * (a[i - 1] != b[i]) + f[i - 1][1] * (b[i - 1] != b[i])) % mod;
}
cout << (f[n][0] + f[n][1]) % mod << endl;
}
E - Find Permutation
Solution:
考虑到题目所说的大小关系,可以联系到是一个有向图的形式,如果\(a\)和\(b\)有一条有向边,则含义为\(a\leqslant b\),所以我们可以发现,其实题目只是求一个拓扑序,观察第二个样例,我们还可以发现不能同时有多个点在拓扑序的同一时刻入队,所以就构造完了。
vector<int> G[N];
queue<int> qq;
int c[N];
int d[N];
int n, m;
vector <int > ans;
int f[N];
bool topsort(){
int z=0;
for (int i=1;i<=n;i++){
if (!d[i])qq.push(i),z++,ans.push_back(i);
}
while (!qq.empty()){
if(qq.size() != 1) {
return false;
}
int t = qq.front();
qq.pop();
for (int i : G[t]){
d[i]--;
if (!d[i]){
qq.push(i);
ans.push_back(i);
z++;
}
}
}
return z==n;
}
void solve(){
cin >> n >> m;
map<PII,bool> mp;
for (int i = 1;i <= m;i ++) {
int x,y;
cin >> x >> y;
d[y]++;
G[x].push_back(y);
}
if(topsort()) {
cout << "Yes" << endl;
int now = 1;
vector<int> last(n + 1);
for (int i : ans) {last[i] = now++;}
for (int i = 1;i <= n;i ++) cout << last[i] << ' '; cout << endl;
}
else cout << "No" << endl;
}
F-Teleporter and Closed off
Solution:
观察到\(m \leqslant 10\),且题目要求的是不经过一个点的时候的最短路,所以对于不经过的点,我们只需要枚举其前面最多\(10\)个位置,他们是需要"跨过"当前不能经过的点的,所以DP先处理出来两个最短路,一个从\(1\)到后面的点的,一个从后面的点到\(n\)的最短路,枚举完点就可以很快的算出答案了。
int f[N][2];
void solve(){
int n , m;
cin >> n >> m;
vector<string> s(n + 1);
for (int i = 2;i <= n;i ++) f[i][1] = 1e18;
for (int i = 1;i <= n - 1;i ++) f[i][0] = 1e18;
for (int i = 1;i <= n;i ++) {
cin >> s[i];
s[i] = " " + s[i];
for (int j = 1;j <= m && i + j <= n;j ++) {
if(s[i][j] == '1') f[i + j][1] = min(f[i + j][1], f[i][1] + 1);
}
}
for (int i = n;i >= 1;i --) {
for (int j = 1;j <= m && i + j <= n;j ++) {
if(s[i][j] == '1') {
f[i][0] = min(f[i][0],f[i + j][0] + 1);
}
}
}
for (int i = 2;i <= n - 1;i ++) {
int ans = 1e18;
for (int j = max(1ll,i + 1 - m);j <= i - 1;j ++) {
for (int k = i + 1 - j;k <= min(m , n);k ++) {
if(s[j][k] == '1') {
ans = min(ans , f[j][1] + f[j + k][0] + 1);
}
}
}
if(ans != 1e18)
cout << ans << ' ';
else
cout << -1 << ' ';
}
}
G - OR Sum
Solution:
暴力的复杂度是\(O(N^{2})\),所以考虑优化。复杂度来自两个地方,一个是枚举循环的情况,\(O(N)\),还有是扫描一遍算或的值,\(O(N)\),前一个循环不好优化,因为每个数都很小,所以考虑优化后面这一个运算的复杂度。首先我们可以对每位进行分析,\(A|B = A + B - A \& B\),这样子拆开式子后,我们就可以发现,前两项会是一个定值,就是所有元素的和,将\(B\)倒序之后,后面一项,对于每一位的情况就是\(\sum_{i = 1}^{n}A_{i + k} \& B_{n - i}\),显然是个卷积的形式,二进制最多五位,做五次卷积分别算贡献就做出来了。
void solve(){
int n;
cin >> n;
int s = 0;
for (int i = 1;i <= n;i ++) cin >> A[i], s += A[i];
for (int i = 1;i <= n;i ++) cin >> B[i], s += B[i];
for (int c = 0;c < 5;c ++) {
memset(f,0,sizeof(f));memset(g,0,sizeof(g));
for (int i = 0;i < n;i ++) {
f[i] = f[i + n] = (A[i + 1] >> c) & 1;
g[i] = (B[n - i] >> c) & 1;
}
poly_mul(f,g,2 * n);
for (int i = 0;i < n;i ++) {
ans[i] += f[n + i] * (1ll << c);
}
}
int now = -1;
for (int i = 0;i < n;i ++) {
now = max(now , s - ans[i]);
}
cout << now << endl;
}
ABC291题解(D-G)的更多相关文章
- 【题解】G.Graph(2015-2016 ACM-ICPC, NEERC, Northern Subregional Contest)
题目链接G题 题意 序列 \(a_1,a_2,⋯,a_n\) 是一个排列, 当且仅当它含有 1 到 n 的所有整数. 排列 \(a_1,a_2,⋯,a_n\) 是一个有向图的拓扑排序,当且仅当对于每条 ...
- Codeforces 549G Happy Line[问题转换 sort]
G. Happy Line time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- hdu 5407
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5407 题意:给定一个数n,求LCM(C(n,0),C(n,1),C(n,2)...,C(n,n)) 根 ...
- 「2017 Multi-University Training Contest 2」2017多校训练2
1001 Is Derek lying 题目链接 HDU6045 Is Derek lying? 给出两个人选择题的回答,问得分分别为x和y是否可能.(\(1\le N \le 80000,0\le ...
- Wannafly 22A
题解 另g = gcd(a1,a2,a3....) 那么k * g % m的方案书就是答案 这个式子子显然是有循环节的 x * g = 0 mod m ,x * g + y * m = 0 exgcd ...
- Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) D. Artsem and Saunders 数学 构造
D. Artsem and Saunders 题目连接: http://codeforces.com/contest/765/problem/D Description Artsem has a fr ...
- Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) A B C D 水 模拟 构造
A. Neverending competitions time limit per test 2 seconds memory limit per test 512 megabytes input ...
- 备战省赛组队训练赛第十七场(UPC)
upc:传送门 A: 题解[1] G: 题解[1] D,G,H,J,L 题解 by 鲁东大学
- ABC260 作战总结
ABC260 作战总结 今后开始写一些模拟赛外的其他比赛的总结(也许有题解?). 开场点到另一场\(\text{ARC}\)去了,耽误了点时间,切完前四题发现已经过了\(37\)分钟了,看来自己读题+ ...
- Storyboards Tutorial 03
这一节主要介绍segues,static table view cells 和 Add Player screen 以及 a game picker screen. Introducing Segue ...
随机推荐
- matlab读写文件
dsp,fpga这些产生的文件大部分都是二进制存储的,知道格式后可以直接读取. 读取后的"可懂"数据如何以"可懂"的形式保存在.dat,.txt,.csv之类文 ...
- sequelize的创建接口以及模糊查询
第一步: 在routes里面复制index.js更改为xxx.js(例如arctile.js) res.json()返回的就是json文件 第二步: 在views里app.js引用路由 第三步:查找数 ...
- 【NumPy】Python将数组中低于一定百分比的值替换
情景举例 现有一个一维数组(或二维进行遍历)存放着很多,找到低于中位数20%的值并将小于该值的数全部替换为该值. 涉及方法 np.median(data, axis=0)用于计算数组中元素的中位数(中 ...
- simis报错总结
--笔记开始: 1.在前台模块处理时,[单位应收核定]比[人员缴费信息]的在职人员多一人,但是总金额一样,可能是以下原因造成!!! A.从后台看,若正常核定在职的ab08比ac13多一个人,可能是ac ...
- js- throw and try-catch-finally
总结一下: 1.throw 抛出错误 2.在使用try-catch时 try{}抛出错误,catch{}块语句才执行,另外catch(e)可创建例如e的标识符,对try{}块抛出异常进行捕捉 3.在使 ...
- airtest的手势滑动方法封装
这个网上应该很多类似的方法封装,各种实现方式也很多,但是感觉最简单实用的还是swipe了:代码很简单,直接上方法了. 很多方法都不会告诉你会导入什么包,其实很多小白入门可能就是这么简单的一步就被卡 ...
- vscode设置终端主题,设置终端选中颜色
vscode定义主题色官网: https://code.visualstudio.com/api/references/theme-color#integrated-terminal-colors 问 ...
- NFS只能挂载为nobody的解决办法
方法一 mount中指定参数 mount -t nfs -o vers=3 192.168.23.23:/data1 /data1 这种方法不推荐,但可以解决临时挂载的需求 方法二 cat /etc/ ...
- Matlab %壹
第一章 基本操作 MATLAB as A Calculator operators: + - * / ^ 顺序: Parenthesis () Power (^) *or/ +or- 特殊的: sqr ...
- ubuntu clean swap
https://askubuntu.com/questions/1357/how-to-empty-swap-if-there-is-free-ram#:~:text=a single line-,s ...