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\)人做出,但我没做出来. 考虑的时候 ...
随机推荐
- 51nod 1344 【前缀和】
思路:求一下最小负数的前缀和 #include<cstdio> #include <map> #include<iostream> #include<stri ...
- VS代码中常用 正则表达式
1. #define ABC 1 修改为 enum 样式: #define (.+?)\s+(.+?)$ $1 = $2 ,
- 洛谷P2564 [SCOI2009]生日礼物(单调队列)
传送门 准确的来说这个应该叫尺取法? 先对所有的点按$x$坐标进行排序 我们维护两个指针$l,r$,每一次令$r$不断右移直到所有颜色齐全,再不断右移$l$直到颜色数不足,那么此时$[l-1,r]$这 ...
- 工作中常用css样式总结
一.HTML隐藏文本输入框 有三种方法: 1.<input type="hidden" value=""> 这是对任何元素都起作用的: 2.< ...
- 第十一篇 .NET高级技术之内置泛型委托
Func.Action 一.如果不是声明为泛型委托 委托的类型名称不能重载,也就是不能名字相同类型参数不同 二..Net中内置两个泛型委托Func.Action(在“对象浏览器”的mscorlib的S ...
- elasticsearch接口开发(新)
此文在上一篇文章的基础上稍做了些许修改,主要在springboot整合ES后的包路径上,如下是新的目录结构 下面贴出代码 MyConfig.java package com.ylht.config; ...
- POJ 1177 Picture(线段树 扫描线 离散化 求矩形并面积)
题目原网址:http://poj.org/problem?id=1177 题目中文翻译: 解题思路: 总体思路: 1.沿X轴离散化建树 2.按Y值从小到大排序平行与X轴的边,然后顺序处理 如果遇到矩形 ...
- Selenium | 基础入门
在maven项目搭建环境: <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifact ...
- 浅谈Java中static作用--转
static表示“全局”或者“静态”的意思,用来修饰成员变量和成员方法,也可以形成静态static代码块,但是Java语言中没有全局变量的概念. 被static修饰的成员变量和成员方法独立于该类的任何 ...
- PHP生成和获取XML格式数据
在做数据接口时,我们通常要获取第三方数据接口或者给第三方提供数据接口,而这些数据格式通常是以XML或者JSON格式传输,本文将介绍如何使用PHP生成XML格式数据供第三方调用以及如何获取第三方提供的X ...