Codeforces Global Round 2 题解
题目链接:https://codeforces.com/contest/1119
A. Ilya and a Colorful Walk
题意:
给出n个数,问从一个数到另外一个不同数的最长距离是多少。
题解:
从前往后,从后往前扫两遍即可。证明可用反证法,这里略去。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 3e5 + ;
int n;
int c[N];
int main() {
ios::sync_with_stdio(false);cin.tie();
cin >> n;
for(int i = ; i <= n ;i++) {
cin >> c[i] ;
}
int ans = ;
for(int i = ; i <= n ;i++) {
if(c[i] != c[]) ans=max(i - , ans);
}
for(int i = n - ; i >= ; i--) {
if(c[i] != c[n]) ans=max(n - i, ans);
}
cout << ans;
return ;
}
B. Alyona and a Narrow Fridge
题意:
给一个冰箱n行2列,然后n个瓶子,每个瓶子都有一个高度。现在要把最多的前k个瓶子放进冰箱里面去,最后会形成若干个隔层,每个隔层中最多两个瓶子。现在要求确定这个k最大为多少。
题解:
放瓶子的话肯定是贪心放的,将最高的尽可能放入一个隔层中。最后二分判断一下就行了。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e3 + ;
int n, h;
int a[N], b[N];
bool check(int x) {
for(int i = ; i <= x ; i++) b[i] = a[i];
sort(b + , b + x + ) ;
int tmp = h;
for(int i = x; i >= ; i -= ) {
int j = max(, i - ) ;
tmp -= max(b[i], b[j]) ;
if(tmp < ) break ;
}
return tmp >= ;
}
int main() {
ios::sync_with_stdio(false);cin.tie();
cin >> n >> h;
for(int i = ; i <= n ;i++)
cin >> a[i] ;
int l = , r = n + , mid ;
while(l < r) {
mid = (l + r) >> ;
if(check(mid)) l = mid + ;
else r = mid;
}
cout << l - ;
return ;
}
C. Ramesses and Corner Inversion
题意:
给出两个n*m的01矩阵,现在每次可以选择任意一个有四个角的子矩阵(不存在为长度为1的边),并且将四个角取反,问最后是否能将第一个矩阵变为第二个矩阵。
题解:
考虑每次选择(1,1),(1,y),(x,1),(x,y)这样的四个点,那么我们就能够让所有除开第一行第一列的值都与第二个矩阵相等。
现在就只剩下第一行第一列了,如果每行、每列的奇偶性两个矩阵相等,那么剩下的第一行第一列必然也与第二个矩阵相等的。所以就这么判断一下就好了。
代码如下:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = ;
int a[N][N], b[N][N] ;
int n, m ;
int main() {
ios::sync_with_stdio(false);cin.tie();
cin >> n >> m ;
for(int i = ; i <= n ; i++) {
for(int j = ; j <= m ; j++) {
cin >> a[i][j] ;
}
}
for(int i = ; i <= n ; i++) {
for(int j = ; j <= m ; j++) {
cin >> b[i][j] ;
}
}
int f = ;
for(int i = ; i <= n ; i++) {
int cnt1 = , cnt2 = ;
for(int j = ; j <= m ; j++) {
if(a[i][j]) cnt1++;
if(b[i][j]) cnt2++;
}
if((cnt1 & ) != (cnt2 & )) f = ;
}
for(int i = ; i <= m ; i++) {
int cnt1 = , cnt2 = ;
for(int j = ; j <= n ; j++) {
if(a[j][i]) cnt1++;
if(b[j][i]) cnt2++;
}
if((cnt1 & ) != (cnt2 & )) f = ;
}
if(f) cout << "No" ;
else cout << "Yes" ;
return ;
}
D. Frets On Fire
题意:
给出n个数字,然后有多个询问,每次询问一个区间[L,R],回答这n个数字从L起加到R中所有数的集合中有多少个元素。
题解:
首先可以发现这题中数的位置顺序不影响答案,那么我们可以对其排序。接下来就考虑每一个数的贡献。
假设我们现在考虑ai的贡献,设di = ai+1 - ai,D = R - L,如果di <= D,那么很显然贡献就为di;如果di > D,此时贡献就为D,因为多出的那一部分必然会被ai+1给遍历到,我们之后考虑ai+1的时候计算就行了,这样就可以不重复也不遗漏了。
具体做法的话就需要维护差值前缀和,然后对于每次询问二分查找位置,计算就行了。
代码如下:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + ;
int n;
ll a[N], s[N], sum[N]; int main() {
ios::sync_with_stdio(false);cin.tie() ;
cin >> n;
for(int i = ; i <= n; i++) {
cin >> a[i] ;
}
sort(a + , a + n + );
int l = unique(a + , a + n + ) - a - ;
for(int i = ; i < l ; i++) s[i] = a[i + ] - a[i] ;
sort(s + , s + l) ;
for(int i = ; i < l ; i++) sum[i] =sum[i - ] + s[i] ;
int q;
ll x, y;
cin >> q;
while(q--) {
cin >> x >> y ;
ll d = y - x + ;
int p = upper_bound(s + , s + l, d) - s - ;
ll ans = sum[p] ;
ans += 1ll * (l - p) * d;
cout << ans << ' ' ;
}
return ;
}
E. Pavel and Triangles
题意:
给出a0,a1...an-1,分别表示有a1个20长度的木棍,有a2个21长度的木棍...有an-1个2n-1长度的木棍。现在问怎么选取木棍,使得拼出三角形的数量最多(每个三角形由三根木棍拼成)。
题解:
通过分析就会发现,木棍的组合方式就只有(i,i,i)或者(i,j,j),其中i < j。这两种方式都是需要3根木棍,我们考虑贪心的方法来选取:
从后往前遍历,统计当前对数pairs,如果当前有奇数个并且pairs > 0,那么我们就用这剩下的一个随便与后面的一个pair配对,这样肯定是最优的。最后利用pairs统计答案即可。
代码如下:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = ;
int n;
int a[N];
int main() {
ios::sync_with_stdio(false);cin.tie();
cin >> n;
for(int i = ; i <= n ; i++) cin >> a[i] ;
ll pairs = ;
ll ans = ;
for(int i = n ; i >= ;i--) {
pairs += a[i] / ;
if(a[i] & && pairs > ) {
ans++;
pairs--;
}
}
ans += pairs * / ;
cout << ans ;
return ;
}
Codeforces Global Round 2 题解的更多相关文章
- Codeforces Global Round 3 题解
这场比赛让我上橙了. 前三题都是大水题,不说了. 第四题有点难想,即使想到了也不能保证是对的.(所以说下面D的做法可能是错的) E的难度是 $2300$,但是感觉很简单啊???说好的歪果仁擅长构造的呢 ...
- Codeforces Global Round 4 题解
技不如人,肝败吓疯…… 开场差点被 A 题意杀了,幸好仔细再仔细看,终于在第 7 分钟过掉了. 跟榜.wtf 怎么一群人跳题/倒序开题? 立刻紧张,把 BC 迅速切掉,翻到了 100+. 开 D.感觉 ...
- Codeforces Global Round 16题解
E. Buds Re-hanging 对于这个题该开始还是没想法的,但这显然是个思维题,还是要多多动手推样例,实践一下. 简化题意:给定一个有根树,规定某个点为树干,当且仅当这个点不是根,且这个点至少 ...
- Codeforces Global Round 1 (A-E题解)
Codeforces Global Round 1 题目链接:https://codeforces.com/contest/1110 A. Parity 题意: 给出{ak},b,k,判断a1*b^( ...
- Codeforces Global Round 11 个人题解(B题)
Codeforces Global Round 11 1427A. Avoiding Zero 题目链接:click here 待补 1427B. Chess Cheater 题目链接:click h ...
- CodeForces Global Round 1
CodeForces Global Round 1 CF新的比赛呢(虽然没啥区别)!这种报名的人多的比赛涨分是真的快.... 所以就写下题解吧. A. Parity 太简单了,随便模拟一下就完了. B ...
- Codeforces Global Round 1 - D. Jongmah(动态规划)
Problem Codeforces Global Round 1 - D. Jongmah Time Limit: 3000 mSec Problem Description Input Out ...
- Codeforces Global Round1 简要题解
Codeforces Global Round 1 A 模拟即可 # include <bits/stdc++.h> using namespace std; typedef long l ...
- Codeforces Global Round 1 (CF1110) (未完结,只有 A-F)
Codeforces Global Round 1 (CF1110) 继续补题.因为看见同学打了这场,而且涨分还不错,所以觉得这套题目可能会比较有意思. 因为下午要开学了,所以恐怕暂时不能把这套题目补 ...
随机推荐
- 【转】PHPCMS v9 自定义表单添加验证码验证
1. 在 \phpcms\templates\default\formguide\show.html 中添加验证码显示 <input type="text" id=&quo ...
- Vue 项目在其他电脑 npm run dev 运行报错的解决方法
一个 Vue 项目从一台电脑上传到 github 上之后,再另外一台电脑上 git clone .并使用 npm run dev 或 npm run start 发生以下报错的解决方法. 报错原因 ...
- Python基础灬序列(字符串、列表、元组)
序列 序列是指它的成员都是有序排列,并且可以通过下标偏移量访问到它的一个或几个成员.序列包含字符串.列表.元组. 字符串 chinese_zodiac = '鼠牛虎兔龙蛇马羊猴鸡狗猪' print(c ...
- 33.[LeetCode] Search in Rotated Sorted Array
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...
- 大数据-storm学习资料视频
storm学习资料视频 https://pan.baidu.com/s/18iQPoVFNHF1NCRBhXsMcWQ
- 【转载】windows安装python2.7后的注册表问题
原文出自:https://www.cnblogs.com/tlz888/p/6879227.html [提要]win平台上,python2.7官网的安装包在安装后不会添加环境变量且不会把安装信息写入注 ...
- YQCB冲刺周第三天
团队讨论照片 今天的任务为实现由用户记录一条数据,向数据库中添加一条数据. 遇到的问题为获取单选框.下拉菜单的参数.
- 线段树---poj2528 Mayor’s posters【成段替换|离散化】
poj2528 Mayor's posters 题意:在墙上贴海报,海报可以互相覆盖,问最后可以看见几张海报 思路:这题数据范围很大,直接搞超时+超内存,需要离散化: 离散化简单的来说就是只取我们需要 ...
- J2EE体系
J2EE的概念 目前,Java 2平台有3个版本,它们是适用于小型设备和智能卡的Java 2平台Micro版(Java 2 Platform Micro Edition,J2ME).适用于桌面系统的J ...
- 构建一个内网的私有CA步骤
1:使用openssl命令生成一个私钥,私钥必须放在/etc/pki/CA/private/目录下 (umask 077; openssl genrsa -out /etc/pki/CA/privat ...