R373 (Div 2)

来源:互联网 发布:java输出1到100的素数 编辑:程序博客网 时间:2024/06/06 06:54

A. Vitya in the Countryside

Every summer Vitya comes to visit his grandmother in the countryside. This summer, he got a huge wart. Every grandma knows that one should treat warts when the moon goes down. Thus, Vitya has to catch the moment when the moon is down.

Moon cycle lasts 30 days. The size of the visible part of the moon (in Vitya's units) for each day is0,1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, and then cycle repeats, thus after the second1 again goes0.

As there is no internet in the countryside, Vitya has been watching the moon forn consecutive days and for each of these days he wrote down the size of the visible part of the moon. Help him find out whether the moon will be up or down next day, or this cannot be determined by the data he has.

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 92) — the number of consecutive days Vitya was watching the size of the visible part of the moon.

The second line contains n integers ai (0 ≤ ai ≤ 15) — Vitya's records.

It's guaranteed that the input data is consistent.

Output

If Vitya can be sure that the size of visible part of the moon on day n + 1 will be less than the size of the visible part on day n, then print "DOWN" at the only line of the output. If he might be sure that the size of the visible part will increase, then print "UP". If it's impossible to determine what exactly will happen with the moon, print -1.

输入一个数字——0:UP;15:DOWN;其他:不能判断(-1)

输入两个及以上——最后两个数是14,15——DOWN;最后两个数是1, 0——UP;倒数第二个数<最后一个数——UP;倒数第二个数>最后一个数——DOWN

#include<iostream>#include<vector>using namespace std;int main(){int n; cin >> n;vector<int> records;for (int i = 0; i < n; i++){int num; cin >> num;records.push_back(num);}if (n <= 1){    if (n == 1)    {        if (records[0] == 15)            cout << "DOWN" << endl;        else if (records[0] == 0)            cout << "UP" << endl;        else cout << -1 << endl;    }    else    cout << -1 << endl;return 0;}int n1 = records[n - 2];int n2 = records[n - 1];if (n1 > n2){if (n2 == 0)cout << "UP" << endl;else cout << "DOWN" << endl;}else{if (n2 == 15)cout << "DOWN" << endl;else cout << "UP" << endl;}}


B. Anatoly and Cockroaches

Anatoly lives in the university dorm as many other students do. As you know, cockroaches are also living there together with students. Cockroaches might be of two colors: black and red. There aren cockroaches living in Anatoly's room.

Anatoly just made all his cockroaches to form a single line. As he is a perfectionist, he would like the colors of cockroaches in the line toalternate. He has a can of black paint and a can of red paint. In one turn he can either swap any two cockroaches, or take any single cockroach and change it's color.

Help Anatoly find out the minimum number of turns he needs to make the colors of cockroaches in the line alternate.

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of cockroaches.

The second line contains a string of length n, consisting of characters 'b' and 'r' that denote black cockroach and red cockroach respectively.

Output

Print one integer — the minimum number of moves Anatoly has to perform in order to make the colors of cockroaches in the line to alternate.

Examples
Input
5rbbrr
Output
1
Input
5bbbbb
Output
2
Input
3rbr
Output
0
最后颜色相间的字符串只可能有两种:r开头或b开头。对这两种情况分别计算一下与原来的串有多少不同,设有x个原本是r的,现在换成了b,y个原本是b的,现在换成了r。

那么最后的结果是max{x, y}。

#include<iostream>#include<string>using namespace std;int main(){int n; cin >> n;string s; cin >> s;int  c1_r = 0, c2_r = 0;for (int i = 0; i < n; i++){if (i % 2 == 0 && s[i] != 'r')c1_r++;if (i % 2 == 1 && s[i] != 'b')c2_r++;}int c1 = (c1_r > c2_r) ? c1_r : c2_r;int c1_b = 0, c2_b = 0;for (int i = 0; i < n; i++){if (i % 2 == 0 && s[i] != 'b')c1_b++;if (i % 2 == 1 && s[i] != 'r')c2_b++;}int c2 = (c1_b > c2_b) ? c1_b : c2_b;if (c1 < c2) cout << c1 << endl;else cout << c2 << endl;}


————————————————分割线   只做出两题————————————————————

C. Efim and Strange Grade

Efim just received his grade for the last test. He studies in a special school and his grade can be equal to any positive decimal fraction. First he got disappointed, as he expected a way more pleasant result. Then, he developed a tricky plan. Each second, he can ask his teacher to round the grade at any place after the decimal point (also, he can ask to round to the nearest integer).


There are t seconds left till the end of the break, so Efim has to act fast. Help him find what is the maximum grade he can get in no more than t seconds. Note, that he can choose to not use all t seconds. Moreover, he can even choose to not round the grade at all.
In this problem, classic rounding rules are used: while rounding number to the n-th digit one has to take a look at the digit n + 1. If it is less than 5 than the n-th digit remain unchanged while all subsequent digits are replaced with 0. Otherwise, if the n + 1 digit is greater or equal to 5, the digit at the position n is increased by 1 (this might also change some other digits, if this one was equal to 9) and all subsequent digits are replaced with 0. At the end, all trailing zeroes are thrown away.
For example, if the number 1.14 is rounded to the first decimal place, the result is 1.1, while if we round 1.5 to the nearest integer, the result is 2. Rounding number 1.299996121 in the fifth decimal place will result in number 1.3.
Input
The first line of the input contains two integers n and t (1 ≤ n ≤ 200 000, 1 ≤ t ≤ 109) — the length of Efim's grade and the number of seconds till the end of the break respectively.
The second line contains the grade itself. It's guaranteed that the grade is a positive number, containing at least one digit after the decimal points, and it's representation doesn't finish with 0.
Output
Print the maximum grade that Efim can get in t seconds. Do not print trailing zeroes.

得分最高尽量从高位开始四舍五入,计算第i位进位到第i - 1位所需时间,动态规划

grade[i] >= 5: dp[i] = 1

grade[i] == 4: dp[i] = dp[i] + 1

grade[i] < 4: dp[i] = INF

找出第一个满足dp[i] < t 的i,就是最后一次的进位。

#include<iostream>#include<string>#include<vector>using namespace std;#define INF 1000000005int main() {int n; int t; cin >> n >> t;string score; cin >> score;vector<int> dp;int pos;for (pos = 0; pos < n; pos++) {if (score[pos] == '.') break;}for (int i = pos + 1; i <= n; i++) {dp.push_back(INF);}for (int i = n - 1; i > pos; i--) {if (score[i] - '0' >= 5) {dp[i - pos - 1] = 1;}else if (score[i] - '0' < 4) {dp[i - pos - 1] = INF;}else {dp[i - pos - 1] = dp[i - pos] == INF ? INF : dp[i - pos] + 1;}}int a;for (a = 0; a < n - pos - 1; a++) {if (dp[a] <= t) break;}string result = score;int carry = a == n - pos - 1 ? 0 : 1;for (int i = pos + a; i >= 0; i--) {if (result[i] == '.') i--;if (carry == 0) break;int num = result[i] - '0';num += carry;result[i] = char(num % 10 + '0');carry = num / 10;}if (carry == 1) cout << '1';for (int i = 0; i <= pos + a; i++) {if (!(i == pos + a && result[i] == '.'))cout << result[i];}cout << endl;}


0 0
原创粉丝点击