Codeforces Round #564 (Div. 2)
参考资料
[1]: the Chinese Editoria
A. Nauuo and Votes
•题意
x个人投赞同票,y人投反对票,z人不确定;
这 z 个人由你来决定是投赞同票还是反对票;
判断 x 与 y 的相对大小是否确定?
•题解
如果 x == y && z == 0,输出 '0';
如果 x-y > z,输出 '+';
如果 y-x > z,输出 '-';
反之,输出 '?';
•Code
#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define ll long long
#define mem(a,b) memset(a,b,sizeof(a))
#define memF(a,b,n) for(int i=0;i <= n;a[i++]=b);
const int maxn=1e3+; int x,y,z; char *Solve()
{
if(x == y && z == )
return "";
if(x-y > z)
return "+";
if(y-x > z)
return "-";
return "?";
}
int main()
{
// freopen("C:\\Users\\hyacinthLJP\\Desktop\\in&&out\\contest","r",stdin);
scanf("%d%d%d",&x,&y,&z);
puts(Solve()); return ;
}
B.Nauuo and Chess(构造)
•题意
给你 n 个棋子,求满足 "for all pairs of pieces i and j, |ri−rj|+|ci−cj| ≥ |i−j|."的最小的方形棋盘的列;
方形棋盘的右下角(m,m)与左上角(1,1)的距离为 2×(m-1);
•题解
①找到 2×(m-1) ≥ n-1 的最小的 m;
②将 1~n-1 个棋子从第一行开始填充,第一行填充完,填充最后一列;
•Code
#include<bits/stdc++.h>
using namespace std;
#define memF(a,b,n) for(int i=0;i <= n;a[i++]=b);
#define INF 0x3f3f3f3f
const int maxn=2e5+; int n; void Solve()
{
int m=(n+)/;
printf("%d\n",m);
int x=,y=;
for(int i=;i < n;++i)
{
printf("%d %d\n",x,y);
if(y < m)///先填充第一行
y++;
else
x++;
}
printf("%d %d\n",m,m);
}
int main()
{
scanf("%d",&n);
Solve(); return ;
}
C. Nauuo and Cards(贪心)
•题意
有 2n 张牌,其中 n 张标号 1~n,其余 n 中为空牌;
从这 2n 张牌中拿出 n 张放在手中,剩余的 n 张摞在桌子上(牌堆);
你可以进行如下操作:
将手中的任意一张牌插入到牌堆的底部,并将牌堆顶端的牌放入手中;
求最少的操作,使得摞在桌子上的 n 张牌从顶端到底端为 1,2,....,n;
•题解
第一句话很好理解,主要是 “答案为 max(...)”这句话不太好理解,下面说说我的进一步理解:
最终结果就是 1~n 摞在桌子上,并且有序;
那么,存在牌k,在经过操作后,使得[1,2,...,k]在牌堆的底端,并且接下来的操作只用标号为[k+1,....,n]的牌,将其依次插入牌堆的底端;
那么,这种情况下,答案就为 p[k]+1+n-k;
其中 p[k]+1 是将牌 k 插入牌堆底端需要的最小操作,n-k是将[k+1,....,n]依次插入牌堆的最小操作;
•Code
#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
const int maxn=2e5+; int n;
int a[maxn];
int b[maxn];
int p[maxn];///p[i]:第i张牌在b中的位置,如果在a中,那么p[i]=0; int F()///判断是否可以只将标号牌插入牌堆的底端使得最终状态满足条件
{
if(!p[])
return INF;
int cur=;
for(;p[cur] == p[]+cur-;cur++);
if(p[cur-] == n)///[1,2,...,cur-1]在b中最后的位置
{
int k=cur;
/**
在依次将第k(k∈[cur,n])张牌插入底部的时候要确保k在手上
如何确保k在手上呢?
假设[cur,k-1]成功插入到底部;
那么,这k-cur牌的插入势必会使得b中的前k-cur张牌拿到手中;
那么,只要b中前k-cur张牌含有k就行;
也就是p[k]<=k-cur;
*/
for(;k <= n && p[k] <= k-cur;k++);
if(k > n)///如果k=n+1,那么只操作[cur,n]便可满足条件
return n-(cur-);
}
return INF;
}
int G()
{
/**
①如果[1,..,i]中标号为x的牌,x在b中的位置在i之后
也就是说 x<i,p[x]>p[i];
那么 p[x]+1+n-x > p[i]+1+n-i;
对于这种情况,ans只会取x对应的操作次数
②如果经过p[i]+1次操作后使得[1,...i]插入牌堆的底端
但是,i之后存在标号为x,y的牌,x<y,p[x]>p[y]
那么,[i+1,...,n]是没法依次插入牌堆的
但,答案会是p[i]+1+n-i吗?
易得p[x] >= p[i]+2
第i张牌的操作次数为 p[i]+1+n-i;
第x张牌的操作次数为 p[x]+1+n-x;
两者做差得 p[x]+1+n-x-(p[i]+1+n-i)=p[x]-p[i]+i > 0
所以,ans只会取x对应的操作次数,而不会取i对应的操作次数
*/
int ans=;
for(int i=;i <= n;++i)
ans=max(ans,p[i]++n-i);
return ans;
}
int main()
{
scanf("%d",&n);
for(int i=;i <= n;++i)
{
scanf("%d",a+i);
p[a[i]]=;
}
for(int i=;i <= n;++i)
{
scanf("%d",b+i);
p[b[i]]=i;
}
printf("%d\n",min(F(),G())); return ;
}
•二次理解2019.10.21
因为明天要帮老师讲有关贪心的实验课,今天就将之前做的贪心的题复习了一下;
对这道题有了新的理解;
将所有操作分为两类:
(1)牌面为 1 的牌从手中打到桌子上
(2)牌面为 1 的牌在桌子上,并且在不打空白牌的情况下就可以完成
首先判断(2)是否满足,如果满足,那此时的解肯定是最优解;
如果不满足(2),如何快速求解(1)对应的最优解呢?
定义 $f_i$ 表示将牌放入手中所需的最小操作;
因为要使得桌子上的牌连续,所以,在打出 1 这张牌后,紧接着要打 2 这张牌,接着是 3,......
也就是说,$f_2 \leq f_1+1\ ,\ f_3 \leq f_1+2\ ,\cdots \ ,\ f_n \leq f_1+n-1$;
所以说,要在 $max_{i=1}^{n} \ \ (f_i-(i-1))$ 后打出牌 1 才能确保接下来打出的牌依次为 2,3,4,....,n;
所以,(1)的最优解为 $max_{i=1}^{n}\ (f_i-(i-1)) + n$;
•Code
#include<bits/stdc++.h>
using namespace std;
const int maxn=2e5+; int n;
int a[maxn];
int b[maxn];
int f[maxn]; int Calc()
{
if(f[] == )
return -;
for(int i=f[]+;i <= n;++i)
if(b[i] != b[i-]+)
return -; int ans=;
int k=;
for(int i=b[n]+;i <= n;++i)
{
if(f[i] > k)
return -;
ans++;
k++;
}
return ans;
}
int Solve()
{
int ans=Calc();
if(ans != -)
return ans; ans=f[];
for(int i=;i <= n;++i)
ans=max(ans,f[i]-(i-));
return ans+n;
}
int main()
{
scanf("%d",&n);
for(int i=;i <= n;++i)
{
scanf("%d",a+i);
f[a[i]]=;
}
for(int i=;i <= n;++i)
{
scanf("%d",b+i);
f[b[i]]=i;
}
printf("%d\n",Solve()); return ;
}
Codeforces Round #564 (Div. 2)的更多相关文章
- Codeforces Round #564 (Div. 1)
Codeforces Round #564 (Div. 1) A Nauuo and Cards 首先如果牌库中最后的牌是\(1,2,\cdots, k\),那么就模拟一下能不能每次打出第\(k+i\ ...
- Codeforces Round #564 (Div. 2) C. Nauuo and Cards
链接:https://codeforces.com/contest/1173/problem/C 题意: Nauuo is a girl who loves playing cards. One da ...
- Codeforces Round #564 (Div. 2) B. Nauuo and Chess
链接:https://codeforces.com/contest/1173/problem/B 题意: Nauuo is a girl who loves playing chess. One da ...
- Codeforces Round #564 (Div. 2) A. Nauuo and Votes
链接:https://codeforces.com/contest/1173/problem/A 题意: Nauuo is a girl who loves writing comments. One ...
- Codeforces Round #564 (Div. 2)B
B. Nauuo and Chess 题目链接:http://codeforces.com/contest/1173/problem/B 题目 Nauuo is a girl who loves pl ...
- Codeforces Round #564 (Div. 2)A
A. Nauuo and Votes 题目链接:http://codeforces.com/contest/1173/problem/A 题目 Nauuo is a girl who loves wr ...
- Codeforces Round #564 (Div. 2) D. Nauuo and Circle(树形DP)
D. Nauuo and Circle •参考资料 [1]:https://www.cnblogs.com/wyxdrqc/p/10990378.html •题意 给出你一个包含 n 个点的树,这 n ...
- Codeforces Round #366 (Div. 2) ABC
Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
随机推荐
- StatusBar用法
一.StatusBar组件介绍 StatusBar 是 React Native 0.20 起新增的跨平台组件,它可以用来设置并动态改变设备的状态栏显示特性. StatusBar 组件可以同时加载多个 ...
- 链表源代码(C语言实现)
源代码(C语言实现) ①.构造链表节点 typedef struct Node //一个单独的节点 { int ...
- LintCode刷题笔记-- Update Bits
标签: 位运算 描述: Given two 32-bit numbers, N and M, and two bit positions, i and j. Write a method to set ...
- 只在需要的时候 Polyfill 你的 JavaScript 代码
本文转载自 Pascal Klau,他是一名来自德国南部的实习生,他讨厌不必要的 HTTP 请求,也不爱吃西兰花.Pascal 将说明使用 polyfill 服务的一种方式,在这种方式下你可能可以完全 ...
- nodeJs koa-generator脚手架
koa-generator 脚手架 全局安装:cnpm install -g koa-generator 查看版本:koa2 --version 创建项目:koa2 project 默认的是用jade ...
- Kubernetes1.4正式发布
Kubernetes1.4正式发布. 昨天刚预测1.4即将正式发布,结果晚上Kubernetes1.4就正式发布了. 先看看Kubernetes发布历史: Kubernetes 1.0 - 2015年 ...
- 【JZOJ4888】【NOIP2016提高A组集训第14场11.12】最近公共祖先
题目描述 YJC最近在学习树的有关知识.今天,他遇到了这么一个概念:最近公共祖先.对于有根树T的两个结点u.v,最近公共祖先LCA(T,u,v)表示一个结点x,满足x是u.v的祖先且x的深度尽可能大. ...
- AtCoder Beginner Contest 084 C - Special Trains
Special Trains Problem Statement A railroad running from west to east in Atcoder Kingdom is now comp ...
- Hadoop及HIVE学习宝典收集
Hive经常使用命令https://cwiki.apache.org/confluence/display/Hive/GettingStartedhttp://richardxu.com/hiveql ...
- oracle函数 RPAD(c1,n[,c2])
[功能]在字符串c1的右边用字符串c2填充,直到长度为n时为止 [参数]C1 字符串 n 追加后字符总长度 c2 追加字符串,默认为空格 [返回]字符型 [说明]如果c1长度大于n,则返回c1左边n个 ...
