首页 > 代码库 > Codeforces Round #378 (Div. 2)

Codeforces Round #378 (Div. 2)

A. Grasshopper And the String

One day, the Grasshopper was jumping on the lawn and found a piece of paper with a string. Grasshopper became interested what is the minimum jump ability he should have in order to be able to reach the far end of the string, jumping only on vowels of the English alphabet. Jump ability is the maximum possible length of his jump.

Formally, consider that at the begginning the Grasshopper is located directly in front of the leftmost character of the string. His goal is to reach the position right after the rightmost character of the string. In one jump the Grasshopper could jump to the right any distance from 1 to the value of his jump ability.

技术分享
The picture corresponds to the first example.

The following letters are vowels: ‘A‘, ‘E‘, ‘I‘, ‘O‘, ‘U‘ and ‘Y‘.

Input

The first line contains non-empty string consisting of capital English letters. It is guaranteed that the length of the string does not exceed 100.

Output

Print single integer a — the minimum jump ability of the Grasshopper (in the number of symbols) that is needed to overcome the given string, jumping only on vowels.

 

给一个字符串,蚂蚱只能落在起/终点和AEIOUY字母上,求最小的最远跳跃距离。O(LEN)扫描即可。

技术分享
 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 bool judge(char c) {
 6     return c == A || c == E || c == I || c == O || c == U || c == Y;
 7 }
 8 
 9 signed main(void) {
10     int dis = 0, tmp = 0;
11     string str; cin >> str;
12     int len = str.length();
13     for (int i = 0; i < len; ++i)
14         if (judge(str[i]))tmp = 0;
15         else dis = max(dis, ++tmp);
16     cout << dis + 1 << endl;
17 }
A.cpp

 

B. Parade

Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.

There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and risoldiers, who start to march from right leg.

The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L?-?R|.

No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri.

Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.

Input

The first line contains single integer n (1?≤?n?≤?105) — the number of columns.

The next n lines contain the pairs of integers li and ri (1?≤?li,?ri?≤?500) — the number of soldiers in the i-th column which start to march from the left or the right leg respectively.

Output

Print single integer k — the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.

Consider that columns are numbered from 1 to n in the order they are given in the input data.

If there are several answers, print any of them.

 

给出一些二元组(l,r),可以对至多一个二元组进行交换l、r的操作。求使|sumL - sumR|最大的交换方案。O(N)暴力枚举即可。

技术分享
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 #define N 100005
 5 
 6 int n;
 7 int l[N];
 8 int r[N]; 
 9 
10 signed main(void)
11 {
12     cin >> n;
13     for (int i = 1; i <= n; ++i)
14         cin >> l[i] >> r[i];
15     int L = 0, R = 0;
16     for (int i = 1; i <= n; ++i)
17         L += l[i], R += r[i];
18     int ans = abs(L - R), id = 0;
19     #define dl (l[i] - r[i])
20     for (int i = 1; i <= n; ++i)
21         if (ans < abs(L - R - 2*dl))
22             ans = abs(L - R - 2*dl), id = i;
23     cout << id << endl;
24 }
B.cpp

 

@Author: YouSiki

Codeforces Round #378 (Div. 2)