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 is 0, 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 second 1 again goes 0.
As there is no internet in the countryside, Vitya has been watching the moon for n 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.
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.
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.
5 3 4 5 6 7
UP
7 12 13 14 15 14 13 12
DOWN
1 8
-1
In the first sample, the size of the moon on the next day will be equal to 8, thus the answer is "UP".
In the second sample, the size of the moon on the next day will be 11, thus the answer is "DOWN".
In the third sample, there is no way to determine whether the size of the moon on the next day will be 7 or 9, thus the answer is -1.
题目大意:有30个固定顺序的数字串 0, 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,
最后走到1之后会再从0开始,然后题目给你一个数字n,代表一共有几个数字,之后n个
数字。问你 第n+1个数字是比第n个大还是小还是不能判断,分别输出up,down,-1;
思路:很简单的一题,其实就是n == 1的时候,0是一定up的,15是一定down的,其余的
都不能判断,因为其余的数字在规定序列里都有2个,所以不可以判断他的趋势,n>1 的
时候,前两步一样,如果不是就看最后两个数字是上升还是下降。
这题许多人疯狂被hack。。。包括许多学长,我队友CillyB,6分钟1Y,而且过了终测,
我一直很佩服他思维的缜密性跟全面性,他总是可以一眼看出题目的本质,这是我要学
习的,而且他觉得正常人应该都可以想到n==1分类,膜拜ing~
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
int n, a[100];
while(~scanf("%d",&n))
{
for(int i = 1; i <= n; i++)
scanf("%d",&a[i]);
if(n == 1)
{
if(a[1] == 0) printf("UP\n");
if(a[1] == 15) printf("DOWN\n");
else printf("-1\n");
}
else
{
if(a[n] == 0) printf("UP\n");
if(a[n] == 15) printf("DOWN\n");
else
printf(a[n] > a[n-1] ? "UP\n" :"DOWN\n");
}
}
return 0;
}
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 are n 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.
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.
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.
5 rbbrr
1
5 bbbbb
2
3 rbr
0
In the first sample, Anatoly has to swap third and fourth cockroaches. He needs 1 turn to do this.
In the second sample, the optimum answer is to paint the second and the fourth cockroaches red. This requires 2 turns.
In the third sample, the colors of cockroaches in the line are alternating already, thus the answer is 0.
题目大意:一个字符串,一共2个字符,b,r,问你怎样才能用最短的次数似的b,r相间,即
每相邻两个字符不同;
思路:其实思路很简单的。。。相间的序列要么是brbrbr,要么是rbrbrb,用原序列跟这两个
“正规的序列”比较,每个序列用两个变量(a1,b1,a2,b2)记录应该在这个位置“变成”b跟
应该“变成”r的,注意每个变量只需要记录一种形式(应该变成r的次数或者应该变成b的次数),
然后取每个序列的两个变量(a1,b1)的最大值,为什么是最大值呢,因为取最小值,说明还
有应该变得字符没变,取最大值,首先先跟交换最小值的那个次数,这样,r或者b(要变得次数
小的那个字符)已经跟“正规序列”相同了,然后剩下的没法交换,就只能直接改变了。所以要
取最大值然后两种“正规序列”分别做这种操作。这时候取两种序列中变化次数最小的的值了。
很好的一个思维题。。
队友CillyB代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn = 100005;
char str[maxn];
int main(void)
{
int n;
while(cin >> n)
{
scanf(" %s", str);
int a1 = 0, b1 = 0, a2 = 0, b2 = 0, num1 = 0, num2 = 0;
for(int i = 0; i < n; i++)
if(str[i] == 'r') num1++;
else num2++;
for(int i = 0; i < n; i++)
{
if(i%2 && str[i] != 'b') a1++;
if(i%2 == 0 && str[i] != 'r') b1++;
if(i%2 && str[i] != 'r') a2++;
if(i%2 == 0 && str[i] != 'b') b2++;
}
int t = min(max(a1, b1), max(a2, b2));
printf("%d\n", t);
}
return 0;
}