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=300005;
int n;
int c[maxn];
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&c[i]);
int ans=0;
for(int i=n;i>=1;i--)
{
if(c[i]!=c[1])
ans=max(ans,i-1);
}
for(int i=1;i<=n;i++)
{
if(c[i]!=c[n])
ans=max(ans,n-i);
}
printf("%d
",ans);
return 0;
}
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.
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=10005;
LL n,h;
int a[maxn],b[maxn];
int main()
{
scanf("%lld %lld",&n,&h);
for(int i=0;i<n;i++)
scanf("%lld",&a[i]);
for(int k=n;k>=0;k--)
{
for(int i=0;i<k;i++)
b[i]=a[i];
sort(b,b+k);
LL ans=0;
for(int i=k-1;i>=0;i-=2)
ans+=b[i];
if(ans<=h)
{
printf("%d
",k);
break;
}
}
return 0;
}
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.
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=515;
int n,m;
int cnt;
int a[maxn][maxn],b[maxn][maxn];
int main()
{
scanf("%d %d",&n,&m);
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
scanf("%d",&a[i][j]);
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
scanf("%d",&b[i][j]);
for(int i=0;i<n;i++)
{
cnt=0;
for(int j=0;j<m;j++)
{
if(a[i][j]!=b[i][j])
cnt++;
}
if(cnt%2!=0)
{
printf("No
");
return 0;
}
}
for(int j=0;j<m;j++)
{
cnt=0;
for(int i=0;i<n;i++)
{
if(a[i][j]!=b[i][j])
cnt++;
}
if(cnt%2!=0)
{
printf("No
");
return 0;
}
}
printf("Yes
");
return 0;
}