Romaji CF 1008A
Vitya has just started learning Berlanese language. It is known that Berlanese uses the Latin alphabet. Vowel letters are "a", "o", "u", "i", and "e". Other letters are consonant.
In Berlanese, there has to be a vowel after every consonant, but there can be any letter after any vowel. The only exception is a consonant "n"; after this letter, there can be any letter (not only a vowel) or there can be no letter at all. For example, the words "harakiri", "yupie", "man", and "nbo" are Berlanese while the words "horse", "king", "my", and "nz" are not.
Help Vitya find out if a word ss is Berlanese.
Input
The first line of the input contains the string ss consisting of |s||s|(1≤|s|≤1001≤|s|≤100) lowercase Latin letters.
Output
Print "YES" (without quotes) if there is a vowel after every consonant except "n", otherwise print "NO".
You can print each letter in any case (upper or lower).
Examples
sumimasen
YES
ninja
YES
codeforces
NO
Note
In the first and second samples, a vowel goes after each consonant except "n", so the word is Berlanese.
In the third sample, the consonant "c" goes after the consonant "r", and the consonant "s" stands on the end, so the word is not Berlanese.
题目意思:元音字符有a e i o u,辅音字符是除了元音字符以外的字母。
辅音字母后面只能跟元音字母,除了辅音字母n,它后面可以跟任意字符或者不跟字符;
元音字符后面可以跟任意字符。
给你一个字符串,如果满足以上条件,则输出YES,否则输出NO。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <string> 6 #define ll long long int 7 char s[110]; 8 char a[6]="aeiou"; 9 int judge(char c)///判断元音的被调函数 10 { 11 int i; 12 for(i=0;i<6;i++) 13 { 14 if(c==a[i]) 15 { 16 return 1; 17 break; 18 } 19 } 20 return 0; 21 } 22 int main() 23 { 24 int i,n,len,flag; 25 gets(s); 26 len= strlen(s); 27 flag=0; 28 for(i=0;i<len;i++) 29 { 30 if(s[i]=='n') 31 { 32 continue; 33 } 34 if(!judge(s[i])) 35 { 36 if(i==len-1) 37 { 38 flag=1; 39 } 40 if(s[i+1]=='n') 41 { 42 flag=1; 43 break; 44 } 45 else if(!judge(s[i+1])) 46 { 47 flag=1; 48 break; 49 } 50 } 51 } 52 if(!flag) 53 { 54 printf("YES "); 55 } 56 else 57 { 58 printf("NO "); 59 } 60 return 0; 61 }
Game Shopping CF1009A
Maxim wants to buy some games at the local game shop. There are nngames in the shop, the ii-th game costs cici.
Maxim has a wallet which can be represented as an array of integers. His wallet contains mm bills, the jj-th bill has value ajaj.
Games in the shop are ordered from left to right, Maxim tries to buy every game in that order.
When Maxim stands at the position ii in the shop, he takes the first bill from his wallet (if his wallet is empty then he proceeds to the next position immediately) and tries to buy the ii-th game using this bill. After Maxim tried to buy the nn-th game, he leaves the shop.
Maxim buys the ii-th game if and only if the value of the first bill (which he takes) from his wallet is greater or equal to the cost of the ii-th game. If he successfully buys the ii-th game, the first bill from his wallet disappears and the next bill becomes first. Otherwise Maxim leaves the first bill in his wallet (this bill still remains the first one) and proceeds to the next game.
For example, for array c=[2,4,5,2,4] and array a=[5,3,4,6] the following process takes place: Maxim buys the first game using the first bill (its value is 55), the bill disappears, after that the second bill (with value 33) becomes the first one in Maxim's wallet, then Maxim doesn't buy the second game because c2>a2, the same with the third game, then he buys the fourth game using the bill of value a2 (the third bill becomes the first one in Maxim's wallet) and buys the fifth game using the bill of value a3a3.
Your task is to get the number of games Maxim will buy.
Input
The first line of the input contains two integers nn and mm (1≤n,m≤1000) — the number of games and the number of bills in Maxim's wallet.
The second line of the input contains nn integers c1,c2,…,cn(1≤ci≤1000), where ci is the cost of the ii-th game.
The third line of the input contains mm integers a1,a2,…,am (1≤aj≤1000), where aj is the value of the jj-th bill from the Maxim's wallet.
Output
Print a single integer — the number of games Maxim will buy.
Examples
5 4
2 4 5 2 4
5 3 4 6
3
5 2
20 40 50 20 40
19 20
0
6 4
4 8 15 16 23 42
1000 1000 1000 1000
4
Note
The first example is described in the problem statement.
In the second example Maxim cannot buy any game because the value of the first bill in his wallet is smaller than the cost of any game in the shop.
In the third example the values of the bills in Maxim's wallet are large enough to buy any game he encounter until he runs out of bills in his wallet.
题目意思:商店里有n个游戏,主人公的钱包里有m张钞票,之后依次给出每个游戏的价格和钱包中的钞票价值。如果当前钞票价值能买当前游戏,就会买游戏用掉钞票,之后使用下一张钞票;不能买就换成下一个游戏,直到能够买为止,求最多能买多少游戏。
解题思路:直接模拟就可以了。
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 int c[1010]; 6 int a[1010]; 7 int main() 8 { 9 int n,m,i,j; 10 int ans; 11 scanf("%d%d",&n,&m); 12 for(i=1; i<=n; i++) 13 { 14 scanf("%d",&c[i]); 15 } 16 for(i=1; i<=m; i++) 17 { 18 scanf("%d",&a[i]); 19 } 20 i=1; 21 j=1; 22 ans=0; 23 while(i<=n) 24 { 25 if(a[j]>=c[i]) 26 { 27 j++; 28 ans++; 29 } 30 if(j==m+1) 31 { 32 break; 33 } 34 i++; 35 } 36 printf("%d ",ans); 37 return 0; 38 }
Reorder the Array CF 1008C
You are given an array of integers. Vasya can permute (change order) its integers. He wants to do it so that as many as possible integers will become on a place where a smaller integer used to stand. Help Vasya find the maximal number of such integers.
For instance, if we are given an array [10,20,30,40], we can permute it so that it becomes [20,40,10,30]. Then on the first and the second positions the integers became larger (20>10, 40>20) and did not on the third and the fourth, so for this permutation, the number that Vasya wants to maximize equals 2. Read the note for the first example, there is one more demonstrative test case.
Help Vasya to permute integers in such way that the number of positions in a new array, where integers are greater than in the original one, is maximal.
Input
The first line contains a single integer nn (1≤n≤105) — the length of the array.
The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤109) — the elements of the array.
Output
Print a single integer — the maximal number of the array's elements which after a permutation will stand on the position where a smaller element stood in the initial array.
Examples
7
10 1 1 1 5 5 3
4
5
1 1 1 1 1
0
Note
In the first sample, one of the best permutations is [1,5,5,3,10,1,1]. On the positions from second to fifth the elements became larger, so the answer for this permutation is 4.
In the second sample, there is no way to increase any element with a permutation, so the answer is 0.
题目意思:给你一个数组,让你重新排列,使得当前位置的数比原来的数大的位置最多有多少个。
解题思路:这道题应该怎么想呢?我在看着一道题的时候,突然想到了一个成语,田忌赛马!和这道题倒是很像,但这是自己的马之间互相比赛,问最多赢几场!但这道题还没有上升到这种博弈的高度,其实换一下思路和好搞,就是看看那些小的数能否换成比它大的数嘛!我开始的想法是将原来数组从大到小排序,看看让较大的化成较小的,同时用vis数组控制只能交换一次,得到了下面的代码:
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 int a[100010]; 6 int vis[100010]; 7 int my_cmp(int x,int y) 8 { 9 return x>y; 10 } 11 int main() 12 { 13 int n,i,j,ans,flag; 14 scanf("%d",&n); 15 for(i=0;i<n;i++) 16 { 17 scanf("%d",&a[i]); 18 } 19 sort(a,a+n,my_cmp); 20 ans=0; 21 for(i=0;i<n;i++) 22 { 23 flag=0; 24 for(j=i+1;j<n;j++) 25 { 26 if(vis[j]) 27 { 28 continue; 29 } 30 if(a[i]>a[j]&&!vis[j]) 31 { 32 ans++; 33 flag=1; 34 vis[j]=1; 35 break; 36 } 37 } 38 if(!flag) 39 { 40 break; 41 } 42 } 43 printf("%d ",ans); 44 return 0; 45 }
但这个代码效率并不高,其实可以使用双指针来做。变成从小到大排序,优先使用最小的数进行比较。用两个指针来遍历,指针i与指针j,一开始指针i指在第1个数,指针j指在第2个数,如果a[j]>a[i],i指针向后移一位,ans++;否则,i指针还是指在原来的位置;j指针向后移,直到j>n。
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 int a[100010]; 6 int vis[100010]; 7 int my_cmp(int x,int y) 8 { 9 return x>y; 10 } 11 int main() 12 { 13 int n,i,j,ans,flag; 14 scanf("%d",&n); 15 for(i=0;i<n;i++) 16 { 17 scanf("%d",&a[i]); 18 } 19 sort(a,a+n,my_cmp); 20 ans=0; 21 for(i=0;i<n;i++) 22 { 23 flag=0; 24 for(j=i+1;j<n;j++) 25 { 26 if(vis[j]) 27 { 28 continue; 29 } 30 if(a[i]>a[j]&&!vis[j]) 31 { 32 ans++; 33 flag=1; 34 vis[j]=1; 35 break; 36 } 37 } 38 if(!flag) 39 { 40 break; 41 } 42 } 43 printf("%d ",ans); 44 return 0; 45 }