Global Round 2
A - Ilya and a Colorful Walk CodeForces - 1119A
Ilya lives in a beautiful city of Chordalsk.
There are nn houses on the street Ilya lives, they are numerated from 11 to nn from left to right; the distance between every two neighboring houses is equal to 11 unit. The neighboring houses are 11 and 22, 22 and 33, ..., n−1n−1 and nn. The houses nn and 11are not neighboring.
The houses are colored in colors c1,c2,…,cnc1,c2,…,cn so that the ii-th house is colored in the color cici. Everyone knows that Chordalsk is not boring, so there are at least two houses colored in different colors.
Ilya wants to select two houses ii and jj so that 1≤i<j≤n1≤i<j≤n, and they have different colors: ci≠cjci≠cj. He will then walk from the house ii to the house jj the distance of (j−i)(j−i) units.
Ilya loves long walks, so he wants to choose the houses so that the distance between them is the maximum possible.
Help Ilya, find this maximum possible distance.
Input
The first line contains a single integer nn (3≤n≤3000003≤n≤300000) — the number of cities on the street.
The second line contains nn integers c1,c2,…,cnc1,c2,…,cn (1≤ci≤n1≤ci≤n) — the colors of the houses.
It is guaranteed that there is at least one pair of indices ii and jj so that 1≤i<j≤n1≤i<j≤n and ci≠cjci≠cj.
Output
Print a single integer — the maximum possible distance Ilya can walk.
Examples
5
1 2 3 2 3
4
3
1 2 1
1
7
1 1 3 1 1 1 1
4
题意:找到两个数,他们数值不相同且距离最远,距离就是两个数位置的下标之差。
解法:贪心的想,要使得距离最远且数值不同,那么就固定一个值在端口,两次遍历取最大值,虽然感觉怪怪的但是就是过了。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn=;
int n;
int c[maxn];
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d",&c[i]);
int ans=;
for(int i=n;i>=;i--)
{
if(c[i]!=c[])
ans=max(ans,i-);
}
for(int i=;i<=n;i++)
{
if(c[i]!=c[n])
ans=max(ans,n-i);
}
printf("%d\n",ans);
return ;
}
B - Alyona and a Narrow Fridge CodeForces - 1119B
Alyona has recently bought a miniature fridge that can be represented as a matrix with hh rows and 22 columns. Initially there is only one shelf at the bottom of the fridge, but Alyona can install arbitrary number of shelves inside the fridge between any two rows. A shelf is two cells wide, does not occupy any space but separates the inside of the fridge to the lower and upper part.
An example of a fridge with h=7h=7 and two shelves. The shelves are shown in black. The picture corresponds to the first example.
Alyona has nn bottles of milk that she wants to put in the fridge. The ii-th bottle is aiai cells tall and 11 cell wide. She can put a bottle on some shelf if the corresponding space above the shelf is at least as tall as the bottle. She can notput a bottle on top of another bottle (if there is no shelf between them). Two bottles can not share a cell.
Alyona is interested in the largest integer kk such that she can put bottles 11, 22, ..., kk in the fridge at the same time. Find this largest kk.
Input
The first line contains two integers nn and hh (1≤n≤1031≤n≤103, 1≤h≤1091≤h≤109) — the number of bottles and the height of the fridge.
The second line contains nn integers a1a1, a2a2, ..., anan (1≤ai≤h1≤ai≤h) — the heights of the bottles.
Output
Print the single integer kk — the maximum integer such that Alyona can put the bottles 11, 22, ..., kk in the fridge at the same time. If Alyona can put all bottles in the fridge, print nn. It is easy to see that Alyona can always put at least one bottle in the fridge.
Examples
5 7
2 3 5 4 1
3
10 10
9 1 1 1 1 1 1 1 1 1
4
5 10
3 1 4 2 4
5
题意:一个冰箱高h,给你n个瓶子,问最多能放几个瓶子,要求按照题目所给的顺序放瓶子,即[1,2,3,4,5],只能从1开始取,且瓶子只能放在隔板上。
解法:由于是按顺序取瓶子,所以我们可以枚举k,取k个瓶子再排序,再根据贪心,相邻的两个瓶子放在同一层,每一层取较高的那一个,计算高度和,当高度和满足条件时就是我们要求的答案。
注意要开LL, 不然那中间ans 会爆int,导致答案出错。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn=;
LL n,h;
int a[maxn],b[maxn];
int main()
{
scanf("%lld %lld",&n,&h);
for(int i=;i<n;i++)
scanf("%lld",&a[i]);
for(int k=n;k>=;k--)
{
for(int i=;i<k;i++)
b[i]=a[i];
sort(b,b+k);
LL ans=;
for(int i=k-;i>=;i-=)
ans+=b[i];
if(ans<=h)
{
printf("%d\n",k);
break;
}
}
return ;
}
C - Ramesses and Corner Inversion CodeForces - 1119C
Ramesses came to university to algorithms practice, and his professor, who is a fairly known programmer, gave him the following task.
You are given two matrices AA and BB of size n×mn×m, each of which consists of 00 and 11 only. You can apply the following operation to the matrix AA arbitrary number of times: take any submatrix of the matrix AA that has at least two rows and two columns, and invert the values in its corners (i.e. all corners of the submatrix that contain 00, will be replaced by 11, and all corners of the submatrix that contain 11, will be replaced by 00). You have to answer whether you can obtain the matrix BBfrom the matrix AA.
An example of the operation. The chosen submatrix is shown in blue and yellow, its corners are shown in yellow.
Ramesses don't want to perform these operations by himself, so he asks you to answer this question.
A submatrix of matrix MM is a matrix which consist of all elements which come from one of the rows with indices x1,x1+1,…,x2x1,x1+1,…,x2 of matrix MM and one of the columns with indices y1,y1+1,…,y2y1,y1+1,…,y2 of matrix MM, where x1,x2,y1,y2x1,x2,y1,y2 are the edge rows and columns of the submatrix. In other words, a submatrix is a set of elements of source matrix which form a solid rectangle (i.e. without holes) with sides parallel to the sides of the original matrix. The corners of the submatrix are cells (x1,y1)(x1,y1), (x1,y2)(x1,y2), (x2,y1)(x2,y1), (x2,y2)(x2,y2), where the cell (i,j)(i,j) denotes the cell on the intersection of the ii-th row and the jj-th column.
Input
The first line contains two integers nn and mm (1≤n,m≤5001≤n,m≤500) — the number of rows and the number of columns in matrices AA and BB.
Each of the next nn lines contain mm integers: the jj-th integer in the ii-th line is the jj-th element of the ii-th row of the matrix AA (0≤Aij≤10≤Aij≤1).
Each of the next nn lines contain mm integers: the jj-th integer in the ii-th line is the jj-th element of the ii-th row of the matrix BB (0≤Bij≤10≤Bij≤1).
Output
Print "Yes" (without quotes) if it is possible to transform the matrix AA to the matrix BB using the operations described above, and "No" (without quotes), if it is not possible. You can print each letter in any case (upper or lower).
Examples
3 3
0 1 0
0 1 0
1 0 0
1 0 0
1 0 0
1 0 0
Yes
6 7
0 0 1 1 0 0 1
0 1 0 0 1 0 1
0 0 0 1 0 0 1
1 0 1 0 1 0 0
0 1 0 0 1 0 1
0 1 0 1 0 0 1
1 1 0 1 0 1 1
0 1 1 0 1 0 0
1 1 0 1 0 0 1
1 0 1 0 0 1 0
0 1 1 0 1 0 0
0 1 1 1 1 0 1
Yes
3 4
0 1 0 1
1 0 1 0
0 1 0 1
1 1 1 1
1 1 1 1
1 1 1 1
No
题意:给出两个n*m的01矩阵,现在每次可以选择任意一个有四个角的子矩阵(不存在为长度为1的边),并且将四个角取反,问最后是否能将第一个矩阵变为第二个矩阵。
解法:考虑由于每次都取反,所以到最后该操作并不会改变矩阵的整体奇偶性, 进一步思考, 由于在行/列中是两两进行改变, 其实根本不会改变a中某一列/行的奇偶性
这样的话, 如果a中某行/列出现了奇数次的不同, 那么就为No。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn=;
int n,m;
int cnt;
int a[maxn][maxn],b[maxn][maxn];
int main()
{
scanf("%d %d",&n,&m);
for(int i=;i<n;i++)
for(int j=;j<m;j++)
scanf("%d",&a[i][j]);
for(int i=;i<n;i++)
for(int j=;j<m;j++)
scanf("%d",&b[i][j]);
for(int i=;i<n;i++)
{
cnt=;
for(int j=;j<m;j++)
{
if(a[i][j]!=b[i][j])
cnt++;
}
if(cnt%!=)
{
printf("No\n");
return ;
}
}
for(int j=;j<m;j++)
{
cnt=;
for(int i=;i<n;i++)
{
if(a[i][j]!=b[i][j])
cnt++;
}
if(cnt%!=)
{
printf("No\n");
return ;
}
}
printf("Yes\n");
return ;
}
Global Round 2的更多相关文章
- 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 Round 2 题解
Codeforces Global Round 2 题目链接:https://codeforces.com/contest/1119 A. Ilya and a Colorful Walk 题意: 给 ...
- Codeforces Global Round 1 (A-E题解)
Codeforces Global Round 1 题目链接:https://codeforces.com/contest/1110 A. Parity 题意: 给出{ak},b,k,判断a1*b^( ...
- [题解向] CF#Global Round 1の题解(A $\to$ G)
这里是总链接\(Link\). \(A\) 题意:求\(\sum_{i=1}^{k} a_i\times b^{k-i}\)的奇偶性, \(k = \Theta(n \log n)\) --其实很容易 ...
- Codeforces Global Round 3
Codeforces Global Round 3 A. Another One Bites The Dust 有若干个a,有若干个b,有若干个ab.你现在要把这些串拼成一个串,使得任意两个相邻的位置 ...
- Codeforces Global Round 1 (CF1110) (未完结,只有 A-F)
Codeforces Global Round 1 (CF1110) 继续补题.因为看见同学打了这场,而且涨分还不错,所以觉得这套题目可能会比较有意思. 因为下午要开学了,所以恐怕暂时不能把这套题目补 ...
- 【手抖康复训练1 】Codeforces Global Round 6
[手抖康复训练1 ]Codeforces Global Round 6 总结:不想复习随意打的一场,比赛开始就是熟悉的N分钟进不去时间,2333,太久没写题的后果就是:A 题手抖过不了样例 B题秒出思 ...
- Codeforces Global Round 11 个人题解(B题)
Codeforces Global Round 11 1427A. Avoiding Zero 题目链接:click here 待补 1427B. Chess Cheater 题目链接:click h ...
- 【Codeforces Round 1110】Codeforces Global Round 1
Codeforces Round 1110 这场比赛只做了\(A\).\(B\).\(C\),排名\(905\),不好. 主要的问题在\(D\)题上,有\(505\)人做出,但我没做出来. 考虑的时候 ...
随机推荐
- hdoj2159【二位费用背包】
题意: 略: 推荐看一下那个背包九讲,第五讲非常清晰啊. 原文: 算法 费用加了一维,只需状态也加一维即可.设f[i][v][u]表示前i件物品付出两种代价分别为v和u时可获得的最大价值.状态转移方程 ...
- poj 3207 Ikki's Story IV - Panda's Trick【2-SAT+tarjan】
注意到相交的点对一定要一里一外,这样就变成了2-SAT模型 然后我建边的时候石乐志,实际上不需要考虑这个点对的边是正着连还是反着连,因为不管怎么连,能相交的总会相交,所以直接判相交即可 然后tarja ...
- 编译boost asio http/server 方法
这段时间学习boost 的asio 编程,想编译asio自带的http/server的程序,无奈在网上根本找不到方法,只能自己摸索学习. 登陆boost asio 的example 目录,(我 boo ...
- 笔记-JavaWeb学习之旅2
数据库的基本概念 1.数据库:DataBase 简称 DB,用于存储和管理数据的仓库 特点: 1.持久化存储数据的,其实数据库就是一个文件系统, 2.方便存储和管理数据 3.使用了统一操作数据库 -- ...
- profile和bashrc
转自某不知名网友 /etc/profile,/etc/bashrc 是系统全局环境变量设定~/.profile,~/.bashrc用户家目录下的私有环境变量设定当登入系统时候获得一个shell进程时, ...
- Mac 解登录密码Keychain
在终端输入: security unlock-keychain -p "login pwd" ~/Library/Keychains/login.keychain 在制作macOS ...
- SPFA/Dijkstra POJ 3013 Big Christmas Tree
题目传送门 题意:找一棵树使得造价最少,造价为每个点的子节点造价和*边的造价和 分析:最短路跑出1根节点到每个点的最短边权值,然后每个点的权值*最短边距和就是答案,注意INF开足够大,n<=1特 ...
- 模拟 百度之星资格赛 1003 IP聚合
题目传送门 /* 模拟水题,排序后找出重复的ip就可以了 */ #include <cstdio> #include <iostream> #include <algor ...
- 关于itchat用法的一篇博文
itchat的原理就是利用爬虫爬取了网页版微信的内容,并进行一系列的操作,运用微信,通过手机与电脑时登录的互通性,可以实现用微信对电脑的操作,通过itchat.msg_register方法,可以得到目 ...
- ASP.NET MVC5的一个轻量级的框架学习的第一天
第二步第三部 这是第一天的小试成功,怪自己太笨了,一个错排查好久,还好有源码看着了解,后续还得多努力,