Educational Codeforces Round 40 (Rated for Div. 2)
A. Diagonal Walking
time limit per test : 1 second
memory limit per test : 256 megabytes
input : standard input
output : standard output
题目描述
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail’s moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 ≤ n ≤ 100) — the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
题意理解
给定一串只包含字符U和字符R的字符串,
给定规则:
UR->D、RU->D
输出可以将字符串缩短的最小长度
解题思路
从头到尾遍历,用一个bool变量标记当前字母是否已经跟别人双宿双飞了;
如果当前字母跟前一项不同,且前一个字母未被使用,修改bool变量,计数器加一;
如果当前字母跟前一项不同,且前一个字母被使用,修改bool变量;
最后结果为字符串长度-计数器;
AC代码
#include<iostream>
#include<string>
using namespace std;
int main() {
int n;
while (cin >> n) {
string str;
cin >> str;
int len = str.size();
bool sign = true;
int ans = 0;
for (int i = 1; i < len; ++i)
{
if (str[i] != str[i - 1] && sign) {//translation
ans++;
sign = false;
}
else{
sign = true;
}
}
cout << len - ans << endl;
}
return 0;
}
####B. String Typing
time limit per test : 1 second
memory limit per test : 256 megabytes
input : standard input
output : standard output
####题目描述
You are given a string s consisting of n lowercase Latin letters. You have to type this string using your keyboard.
Initially, you have an empty string. Until you type the whole string, you may perform the following operation:
add a character to the end of the string.
Besides, at most once you may perform one additional operation: copy the string and append it to itself.
For example, if you have to type string abcabca, you can type it in 7 operations if you type all the characters one by one. However, you can type it in 5 operations if you type the string abc first and then copy it and type the last character.
If you have to type string aaaaaaaaa, the best option is to type 4 characters one by one, then copy the string, and then type the remaining character.
Print the minimum number of operations you need to type the given string.
####Input
The first line of the input containing only one integer number n (1 ≤ n ≤ 100) — the length of the string you have to type. The second line containing the string s consisting of n lowercase Latin letters.
####Output
Print one integer number — the minimum number of operations you need to type the given string.
####Examples
Input
7
abcabca
Output
5
Input
8
abcdefgh
Output
8
Note
The first test described in the problem statement.
In the second test you can only type all the characters one by one.
####题意理解
给定字符串,给定两种操作方式
单字符输入,和一次性输入当前字符串(只能用一次)
输出整个字符串输入所需的最少的操作次数
英语不好,坑在了当前字符串上,被hack掉了
我的第二种操作读成了可以将任意长的字符串复制一遍
附上hack样例
Test: #11, time: 0 ms., memory: 3428 KB, exit code: 0, checker exit code: 1, verdict: WRONG_ANSWER
Input
6
aabbbb
Output
5
Answer
6
Checker Log
wrong answer expected ‘6’, found ‘5’
####解题思路
遍历要一次性复制的字符头部长度,记录最大值就行了
####AC代码
#include<iostream>
#include<string>
#include<cmath>
#include<algorithm>
using namespace std;
int main() {
int n;
while (cin >> n) {
string str;
cin >> str;
int len = str.size();
int ans = 0;
for (int i = 1; i <= len/2; ++i)//遍历长度
{
bool sign=true;
for (int j = 0;j<i; ++j) {//逐个比较
if(str[j]!=str[i+j]){
sign=false;
break;
}
}
if(sign)
ans = max(ans, i);
}
if (ans == 0)
cout << len << endl;
else
cout << len -ans + 1<< endl;
}
return 0;
}
####C. Matrix Walk
time limit per test : 1 second
memory limit per test : 256 megabytes
input : standard input
output : standard output
####题目描述
There is a matrix A of size x × y filled with integers. For every , Ai, j = y(i - 1) + j. Obviously, every integer from [1…xy] occurs exactly once in this matrix.
You have traversed some path in this matrix. Your path can be described as a sequence of visited cells a1, a2, …, an denoting that you started in the cell containing the number a1, then moved to the cell with the number a2, and so on.
From the cell located in i-th line and j-th column (we denote this cell as (i, j)) you can move into one of the following cells:
(i + 1, j) — only if i < x;
(i, j + 1) — only if j < y;
(i - 1, j) — only if i > 1;
(i, j - 1) — only if j > 1.
Notice that making a move requires you to go to an adjacent cell. It is not allowed to stay in the same cell. You don’t know x and y exactly, but you have to find any possible values for these numbers such that you could start in the cell containing the integer a1, then move to the cell containing a2 (in one step), then move to the cell containing a3 (also in one step) and so on. Can you choose x and y so that they don’t contradict with your sequence of moves?
####Input
The first line contains one integer number n (1 ≤ n ≤ 200000) — the number of cells you visited on your path (if some cell is visited twice, then it’s listed twice).
The second line contains n integers a1, a2, …, an (1 ≤ ai ≤ 109) — the integers in the cells on your path.
####Output
If all possible values of x and y such that 1 ≤ x, y ≤ 109 contradict with the information about your path, print NO.
Otherwise, print YES in the first line, and in the second line print the values x and y such that your path was possible with such number of lines and columns in the matrix. Remember that they must be positive integers not exceeding 109.
####Examples
Input
Copy
8
1 2 3 6 9 8 5 2
Output
YES
3 3
Input
Copy
6
1 2 1 2 5 3
Output
NO
Input
Copy
2
1 10
Output
YES
4 9
Note
The matrix and the path on it in the first test looks like this:
Also there exist multiple correct answers for both the first and the third examples.
####题意理解
给定一段在位置宽高的地图上的路径,判断该路径是否合法,若合法,输出YES和其合法的条件,反之输出NO
YES的情况不止一种,随便输出一种就行
####解题思路
通过第一步确定初始位置,然后记录路径在地图上下左右移动的最远范围;
判断每一步与前一步的间隔,
若等于一,则为同一行,若不为一,必定不在同一行,记录其间隔值,
若间隔值不唯一,则路径不合法,即输出no
若间隔唯一,间隔即列数,可算出初始值的位置,
再跟据初始值的移动范围,可确定其最小左右边界,
若其左右边界大于间隔值,则路径不合法,输出no
若没有间隔,即可将路径放置于一行,或一列,需要注意其起始项
####AC代码
#include<iostream>
#include<string>
#include<cmath>
#include<algorithm>
using namespace std;
int main() {
int n;
int map[200500];
while (cin >> n) {
bool sign = true;
cin >> map[0];
int column = 1;
int x=0,y=0;
int x_min = 0, x_max = 0, y_min = 0, y_max = 0;
for (int i = 1; i < n; ++i) {
cin >> map[i];
//map[i] = i+1;
int temp = map[i] - map[i - 1];
if (temp == 0)//stay
sign = false;
if (temp == 1) {//right of the same line
x++;
x_max = max(x_max, x);
}
else if (temp == -1) {//left of the same line
x--;
x_min = min(x_min, x);
}
else if (column == 1) {//first:the same column
if (temp < 0) {
column = -temp;
y--;
y_min = min(y_min, y);
}
else{
column = temp;
y++;
y_max = max(y_max, y);
}
}
else if (temp == -column) {//up of the same column
y--;
y_min = min(y_min, y);
}
else if (temp == column) {//low of the same column
y++;
y_max = max(y_max, y);
}
else {
sign = false;
}
}
if (!sign) {
cout << "NO\n";
}
else {
int ty = map[0] % column;//column
if (ty == 0)ty = column;
int tx = (map[0] - 1) / column + 1;//line
if (-x_min > ty - 1) {//out of left range
sign = false;
cout << "NO\n";
}
else if (x_max > column - ty) {//out of right range
if (column == 1) {
cout << "YES\n";
cout << 1 << " " << map[0]+x_max << endl;
}
else {
sign = false;
cout << "NO\n";
}
}
else {
sign = true;
cout << "YES\n";
cout <<tx+y_max<< " " << column << endl;
}
}
}
return 0;
}
结论:菜不成声。。。