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

Input
5
1 2 3 2 3
Output
4
Input
3
1 2 1
Output
1
Input
7
1 1 3 1 1 1 1
Output
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

Input
5 7
2 3 5 4 1
Output
3
Input
10 10
9 1 1 1 1 1 1 1 1 1
Output
4
Input
5 10
3 1 4 2 4
Output
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

Input
3 3
0 1 0
0 1 0
1 0 0
1 0 0
1 0 0
1 0 0
Output
Yes
Input
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
Output
Yes
Input
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
Output
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的更多相关文章

  1. CodeForces Global Round 1

    CodeForces Global Round 1 CF新的比赛呢(虽然没啥区别)!这种报名的人多的比赛涨分是真的快.... 所以就写下题解吧. A. Parity 太简单了,随便模拟一下就完了. B ...

  2. Codeforces Global Round 1 - D. Jongmah(动态规划)

    Problem   Codeforces Global Round 1 - D. Jongmah Time Limit: 3000 mSec Problem Description Input Out ...

  3. Codeforces Global Round 2 题解

    Codeforces Global Round 2 题目链接:https://codeforces.com/contest/1119 A. Ilya and a Colorful Walk 题意: 给 ...

  4. Codeforces Global Round 1 (A-E题解)

    Codeforces Global Round 1 题目链接:https://codeforces.com/contest/1110 A. Parity 题意: 给出{ak},b,k,判断a1*b^( ...

  5. [题解向] CF#Global Round 1の题解(A $\to$ G)

    这里是总链接\(Link\). \(A\) 题意:求\(\sum_{i=1}^{k} a_i\times b^{k-i}\)的奇偶性, \(k = \Theta(n \log n)\) --其实很容易 ...

  6. Codeforces Global Round 3

    Codeforces Global Round 3 A. Another One Bites The Dust 有若干个a,有若干个b,有若干个ab.你现在要把这些串拼成一个串,使得任意两个相邻的位置 ...

  7. Codeforces Global Round 1 (CF1110) (未完结,只有 A-F)

    Codeforces Global Round 1 (CF1110) 继续补题.因为看见同学打了这场,而且涨分还不错,所以觉得这套题目可能会比较有意思. 因为下午要开学了,所以恐怕暂时不能把这套题目补 ...

  8. 【手抖康复训练1 】Codeforces Global Round 6

    [手抖康复训练1 ]Codeforces Global Round 6 总结:不想复习随意打的一场,比赛开始就是熟悉的N分钟进不去时间,2333,太久没写题的后果就是:A 题手抖过不了样例 B题秒出思 ...

  9. Codeforces Global Round 11 个人题解(B题)

    Codeforces Global Round 11 1427A. Avoiding Zero 题目链接:click here 待补 1427B. Chess Cheater 题目链接:click h ...

  10. 【Codeforces Round 1110】Codeforces Global Round 1

    Codeforces Round 1110 这场比赛只做了\(A\).\(B\).\(C\),排名\(905\),不好. 主要的问题在\(D\)题上,有\(505\)人做出,但我没做出来. 考虑的时候 ...

随机推荐

  1. c语言函数参考

                                                                                                        ...

  2. c语言基本数据类型相关

    1byte = 8bit  数据类型  比特长度  位长度  IO表达  int  2/4  16/32  %d   unsigned (int)  2/4  16/32  %u  short int ...

  3. WPF DataGrid foreground 绑定问题

    初学WPF ,  希望对DataGrid 中所属的一个Column名下的值的颜色动态修改 <DataGridTextColumn Header="隐含回购利率(%)" Bin ...

  4. bzoj 3262 陌上花开 【CDQ分治】

    三维偏序 首先把所有花按 x一序,y二序,z三序 排序,然后去重,con记录同样的花的个数,然后进行cdq 现在假设有[l.r]区间,其中[l,mid] [mid+1,r],已经递归处理完毕.我们把区 ...

  5. python 标准库大全

    python 标准库 文本 string:通用字符串操作 re:正则表达式操作 difflib:差异计算工具 textwrap:文本填充 unicodedata:Unicode字符数据库 string ...

  6. centos7上使用locate命令-文件查找

    centos7上使用locate命令   小贴士:在centOS7以上的系统中使用“locate”文件查找命令,发现该命令不可用. 检查了下,原来是centos7默认没有安装该命令,在联网状态运行“y ...

  7. 简单记录下@RequestBody(关于它和@RequestParam接收数据方式的拓展)

    内容参考自博客:https://blog.csdn.net/ff906317011/article/details/78552426 这个标注是用来注释controller中的请求方法中的参数的,那么 ...

  8. drbd 配置

    DRBD(Distributed Replicated Block Device),DRBD 号称是 "网络 RAID",开源软件,由 LINBIT 公司开发.DRBD实际上是一种 ...

  9. PowerShell~发布你的mvc网站

    通过使用ps加上msbuild可以方便的编译你的.net应用程序,并且可以把它发布到你的磁盘上,部署非常方例! 我们在c盘添加一个hello网站,解决方案名是hello.sln,它的网站是hello. ...

  10. import和from .xxx import *的一点重要区别

    import zzz 不会导入zzz中已导入的模块 from .xxx import * 会导入xxx中已导入的模块 特别注意: 使用logging时,x模块导入了log.py,y模块导入了log.p ...