国庆10.1

版权声明:莉莉莉 https://blog.csdn.net/qq_41700151/article/details/82952432

A - Turn the Rectangles CodeForces - 1008B
There are n rectangles in a row. You can either turn each rectangle by 90 degrees or leave it as it is. If you turn a rectangle, its width will be height, and its height will be width. Notice that you can turn any number of rectangles, you also can turn all or none of them. You can not change the order of the rectangles.

Find out if there is a way to make the rectangles go in order of non-ascending height. In other words, after all the turns, a height of every rectangle has to be not greater than the height of the previous rectangle (if it is such).

Input
The first line contains a single integer n (1≤n≤105) — the number of rectangles.

Each of the next n lines contains two integers wi and hi (1≤wi,hi≤109) — the width and the height of the i-th rectangle.

Output
Print “YES” (without quotes) if there is a way to make the rectangles go in order of non-ascending height, otherwise print “NO”.

You can print each letter in any case (upper or lower).

Examples
Input
3
3 4
4 6
3 5
Output
YES
Input
2
3 4
5 5
Output
NO
Note
In the first test, you can rotate the second and the third rectangles so that the heights will be [4, 4, 3].

In the second test, there is no way the second rectangle will be not higher than the first one.
题意: 有n个长方形,让你通过转动使他变成一个不升序的序列,而且长方形排列的顺序不能变。
思路: 先找第一个的那一条长边,后面的n-1个长方形先看长边是否比上一个的小,再看短边。
代码:

#include <iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#define ll long long
using namespace std;
struct node
{
    int a;
    int b;
} nod[100005];
int main()
{
    int n;
    ios::sync_with_stdio(false);
    cin>>n;
    for(int i=0; i<n; i++)
        cin>>nod[i].a>>nod[i].b;
    if(nod[0].a<nod[0].b)
        swap(nod[0].a,nod[0].b);
    int ans=nod[0].a;
    int maxx,minn;
    int flag=0;
    for(int i=1; i<n; i++)
    {
       maxx=max(nod[i].a,nod[i].b);
       if(maxx<=ans)
        ans=maxx;
       else
       {
           minn=min(nod[i].a,nod[i].b);
           if(minn<=ans)
            ans=minn;
           else
           {
               flag=1;
               break;
           }
       }
    }
    if(flag==1)
        cout<<"NO"<<endl;
    else
        cout<<"YES"<<endl;
    return 0;
}

B - Reorder the Array CodeForces - 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 n (1≤n≤105) — the length of the array.

The second line contains n integers a1,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
Input
7
10 1 1 1 5 5 3
Output
4
Input
5
1 1 1 1 1
Output
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.
题意: 给你n个数,让他们两两互换,比较换了之后的和之前的,如果比之前的大,就加1,输出最大的数量。
思路: 先排序,遍历一遍,如果第j个比第i个大,j和i都加1。
代码:

#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int a[100000+5];
int main()
{
    int n;
    ios::sync_with_stdio(false);
    cin>>n;
    for(int i=0;i<n;i++)
        cin>>a[i];
    sort(a,a+n);
    int k=0;
    for(int i=0;i<n;i++)
    {
        if(a[i]>a[k])
            k++;
    }
    cout<<k<<endl;

}

另一种方法是找规律,观察几组样例发现答案是总数n-出现最多的数字的次数,用map

#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;

int main()
{
    int n,a,maxx=-1;
    map <int,int> m;
    ios::sync_with_stdio(false);
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>a;
        m[a]++;
        if(m[a]>maxx)
            maxx=m[a];
    }
    cout<<n-maxx<<endl;


}

C - Romaji CodeForces - 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 s is Berlanese.

Input
The first line of the input contains the string s consisting of |s| (1≤|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
Input
sumimasen
Output
YES
Input
ninja
Output
YES
Input
codeforces
Output
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.
题意: 最后一个字母只能是元音字母或者n,并且辅音字母(除去n)后面必须有一个元音字母,判断所给的字符串是否满足这两个条件。
代码:

#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;

int main()
{
    string s;
    cin>>s;
    int flag=0,n=s.size();
    if(s[n-1]!='a'&&s[n-1]!='e'&&s[n-1]!='i'&&s[n-1]!='o'&&s[n-1]!='u'&&s[n-1]!='n')
    {
        cout<<"NO"<<endl;
        return 0;
    }
    for(int i=0;i<s.size()-1;i++)
    {
        if(s[i]!='a'&&s[i]!='e'&&s[i]!='i'&&s[i]!='o'&&s[i]!='u'&&s[i]!='n')
            if(s[i+1]!='a'&&s[i+1]!='e'&&s[i+1]!='i'&&s[i+1]!='o'&&s[i+1]!='u')
                flag=1;
    }
    if(flag==0)
        cout<<"YES"<<endl;
    else
        cout<<"NO"<<endl;
    return 0;


}

D - Game Shopping CodeForces - 1009A(水题)
Maxim wants to buy some games at the local game shop. There are n games in the shop, the i-th game costs ci.

Maxim has a wallet which can be represented as an array of integers. His wallet contains m bills, the j-th bill has value aj.

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 i 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 i-th game using this bill. After Maxim tried to buy the n-th game, he leaves the shop.

Maxim buys the i-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 i-th game. If he successfully buys the i-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 5), the bill disappears, after that the second bill (with value 3) 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 a3.

Your task is to get the number of games Maxim will buy.

Input
The first line of the input contains two integers n and m (1≤n,m≤1000) — the number of games and the number of bills in Maxim’s wallet.

The second line of the input contains n integers c1,c2,…,cn (1≤ci≤1000), where ci is the cost of the i-th game.

The third line of the input contains m integers a1,a2,…,am (1≤aj≤1000), where aj is the value of the j-th bill from the Maxim’s wallet.

Output
Print a single integer — the number of games Maxim will buy.

Examples
Input
5 4
2 4 5 2 4
5 3 4 6
Output
3
Input
5 2
20 40 50 20 40
19 20
Output
0
Input
6 4
4 8 15 16 23 42
1000 1000 1000 1000
Output
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.
题意: 用第一张钱买第一个游戏,买不起就用这张钱买下一个游戏,问能买几个游戏?
代码:

#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
int a[1000+5];
int c[1000+5];
int main()
{
    int n,m;
    ios::sync_with_stdio(false);
    cin>>n;
    cin>>m;
    for(int i=1;i<=n;i++)
        cin>>c[i];
    for(int i=1;i<=m;i++)
        cin>>a[i];
    int i=1,j=1;
    int ans=0;
    for(i;i<=n&&j<=m;)
    {
        if(c[i]<=a[j])
        {
            i++;
            j++;
            ans++;
        }
        else
            i++;
    }
    cout<<ans<<endl;
    return 0;


}

E-Minimum Ternary String (CodeForces - 1009B)(思维题)
You are given a ternary string (it is a string which consists only of characters ‘0’, ‘1’ and ‘2’).
You can swap any two adjacent (consecutive) characters ‘0’ and ‘1’ (i.e. replace “01” with “10” or vice versa) or any two adjacent (consecutive) characters ‘1’ and ‘2’ (i.e. replace “12” with “21” or vice versa).
For example, for string “010210” we can perform the following moves:
“010210” → “100210”;
“010210” → “001210”;
“010210” → “010120”;
“010210” → “010201”.
Note than you cannot swap “02” → “20” and vice versa. You cannot perform any other operations with the given string excluding described above.
You task is to obtain the minimum possible (lexicographically) string by using these swaps arbitrary number of times (possibly, zero).
String a is lexicographically less than string b (if strings a and b have the same length) if there exists some position i (1≤i≤|a|, where |s| is the length of the string s) such that for every j<i holds aj=bj, and ai<bi.
Input
The first line of the input contains the string s consisting only of characters ‘0’, ‘1’ and ‘2’, its length is between 1 and 105 (inclusive).
Output
Print a single string — the minimum possible (lexicographically) string you can obtain by using the swaps described above arbitrary number of times (possibly, zero).
Examples
Input
100210
Output
001120
Input
11222121
Output
11112222
Input
20
Output
20
题意: 给定一个串,在这个串里,1和2可以交换,1和0也可以交换,0和2不能交换,通过一番操作之后,找出字典序最小的一个串;
分析: 由题意可知,1可以和这两个数换,所以1可以在这个串中随意滑动,但是第一个2后面的0就不可以换到这个2前面来,辣么这个题就是找第一个2前面所有的0全部输出,再输出所有的1,第一个2之后的所有不为1的数全部原样输出,注意000111这个样例。
代码:

#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
int main()
{
   int a[4];
   ios::sync_with_stdio(false);
   memset(a,0,sizeof(a));
   string s;
   cin>>s;
   int n=s.size();
   int k=n;               //k初始化为n保证000111这组样例
   for(int i=n-1;i>=0;i--)
   {
       if(s[i]=='0')
        a[0]++;
       if(s[i]=='2')
        {
            k=i;              //找第一个2的位置
            a[2]++;
        }
       if(s[i]=='1')
        a[1]++;
   }
   for(int i=0;i<k;i++)
   {
       if(s[i]=='0')
        cout<<"0";
   }
   for(int i=0;i<a[1];i++)
    cout<<"1";
   for(int i=k;i<n;i++)
    if(s[i]!='1')
     cout<<s[i];
   return 0;

}

---------------------


猜你喜欢

转载自blog.csdn.net/qq_41700151/article/details/82952432