1. 程式人生 > >864】 A B C【模擬】 D【貪心+優先佇列】

864】 A B C【模擬】 D【貪心+優先佇列】

A - Fair Game
Petya and Vasya decided to play a game. They have n cards (n is an even number). A single integer is written on each card.

Before the game Petya will choose an integer and after that Vasya will choose another integer (different from the number that Petya chose). During the game each player takes all the cards with number he chose. For example, if Petya chose number 5 before the game he will take all cards on which 5 is written and if Vasya chose number 10 before the game he will take all cards on which 10 is written.

The game is considered fair if Petya and Vasya can take all n cards, and the number of cards each player gets is the same.

Determine whether Petya and Vasya can choose integer numbers before the game so that the game is fair.

Input
The first line contains a single integer n (2 ≤ n ≤ 100) — number of cards. It is guaranteed that n is an even number.

The following n lines contain a sequence of integers a1, a2, …, an (one integer per line, 1 ≤ ai ≤ 100) — numbers written on the n cards.

Output
If it is impossible for Petya and Vasya to choose numbers in such a way that the game will be fair, print “NO” (without quotes) in the first line. In this case you should not print anything more.

In the other case print “YES” (without quotes) in the first line. In the second line print two distinct integers — number that Petya should choose and the number that Vasya should choose to make the game fair. If there are several solutions, print any of them.

Example
Input
4
11
27
27
11
Output
YES
11 27
Input
2
6
6
Output
NO
Input
6
10
20
30
20
10
20
Output
NO
Input
6
1
1
2
2
3
3
Output
NO
Note
In the first example the game will be fair if, for example, Petya chooses number 11, and Vasya chooses number 27. Then the will take all cards — Petya will take cards 1 and 4, and Vasya will take cards 2 and 3. Thus, each of them will take exactly two cards.

In the second example fair game is impossible because the numbers written on the cards are equal, but the numbers that Petya and Vasya should choose should be distinct.

In the third example it is impossible to take all cards. Petya and Vasya can take at most five cards — for example, Petya can choose number 10 and Vasya can choose number 20. But for the game to be fair it is necessary to take 6 cards.
水題
題意 給個序列,問其中是不是 只有兩個不同的元素,並且兩個元素的個數還一樣。

#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int>pii;
#define first fi
#define second se
#define  LL long long
#define fread() freopen("in.txt","r",stdin)
#define fwrite() freopen("out.txt","w",stdout)
#define CLOSE() ios_base::sync_with_stdio(false)

const int MAXN = 1e5;
const int MAXM = 1e6;
const int mod = 1e9+7;
const int inf = 0x3f3f3f3f;

int arr[MAXN];
int nn[MAXN];
int main(){
    CLOSE();
//  fread();
//  fwrite();
    int n;scanf("%d",&n);
    for(int i=0;i<n;i++){
        int a;scanf("%d",&arr[i]);
        nn[arr[i]]++;
    }
    sort(arr,arr+n);
    int cnt=1;
    for(int i=1;i<n;i++){
        if(arr[i]!=arr[i-1]) cnt++;
    }

    if(cnt!=2) puts("NO");
    else {
        int size=unique(arr,arr+n)-arr;
        if(nn[arr[0]]!=nn[arr[1]]) puts("NO");
        else {
            puts("YES");
                printf("%d %d\n",arr[1],arr[0]);

        }
    }
    return 0;
}

B - Polycarp and Letters
Polycarp loves lowercase letters and dislikes uppercase ones. Once he got a string s consisting only of lowercase and uppercase Latin letters.

Let A be a set of positions in the string. Let’s call it pretty if following conditions are met:

letters on positions from A in the string are all distinct and lowercase;
there are no uppercase letters in the string which are situated between positions from A (i.e. there is no such j that s[j] is an uppercase letter, and a1 < j < a2 for some a1 and a2 from A).
Write a program that will determine the maximum number of elements in a pretty set of positions.

Input
The first line contains a single integer n (1 ≤ n ≤ 200) — length of string s.

The second line contains a string s consisting of lowercase and uppercase Latin letters.

Output
Print maximum number of elements in pretty set of positions for string s.

Example
Input
11
aaaaBaabAbA
Output
2
Input
12
zACaAbbaazzC
Output
3
Input
3
ABC
Output
0
Note
In the first example the desired positions might be 6 and 8 or 7 and 8. Positions 6 and 7 contain letters ‘a’, position 8 contains letter ‘b’. The pair of positions 1 and 8 is not suitable because there is an uppercase letter ‘B’ between these position.

In the second example desired positions can be 7, 8 and 11. There are other ways to choose pretty set consisting of three elements.

In the third example the given string s does not contain any lowercase letters, so the answer is 0.
題意: 給定個字串,求其中連續的小寫字串中不同的字元個數最多的個數?

#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int>pii;
#define first fi
#define second se
#define  LL long long
#define fread() freopen("in.txt","r",stdin)
#define fwrite() freopen("out.txt","w",stdout)
#define CLOSE() ios_base::sync_with_stdio(false)

const int MAXN = 300;
const int MAXM = 1e6;
const int mod = 1e9+7;
const int inf = 0x3f3f3f3f;

char str[MAXN];
char s[MAXN][MAXN];
int main(){
    CLOSE();
//  fread();
//  fwrite();    
    int n; scanf("%d",&n);
    scanf("%s",str);
    int ans=0;
    int cnt=0; int ge=0;
    str[n]='A';
    for(int i=0;str[i];i++){
        if(str[i]>='a'&&str[i]<='z') {
            s[cnt][ge++]=str[i];
        }
        else {
            sort(s[cnt],s[cnt]+ge);
            ge=unique(s[cnt],s[cnt]+ge)-s[cnt];
            ans=max(ans,ge);
            cnt++; ge=0;
        }
    }
    printf("%d\n",ans);
    return 0;
}

C Bus
A bus moves along the coordinate line Ox from the point x = 0 to the point x = a. After starting from the point x = 0, it reaches the point x = a, immediately turns back and then moves to the point x = 0. After returning to the point x = 0 it immediately goes back to the point x = a and so on. Thus, the bus moves from x = 0 to x = a and back. Moving from the point x = 0 to x = a or from the point x = a to x = 0 is called a bus journey. In total, the bus must make k journeys.

The petrol tank of the bus can hold b liters of gasoline. To pass a single unit of distance the bus needs to spend exactly one liter of gasoline. The bus starts its first journey with a full petrol tank.

There is a gas station in point x = f. This point is between points x = 0 and x = a. There are no other gas stations on the bus route. While passing by a gas station in either direction the bus can stop and completely refuel its tank. Thus, after stopping to refuel the tank will contain b liters of gasoline.

What is the minimum number of times the bus needs to refuel at the point x = f to make k journeys? The first journey starts in the point x = 0.

Input
The first line contains four integers a, b, f, k (0 < f < a ≤ 106, 1 ≤ b ≤ 109, 1 ≤ k ≤ 104) — the endpoint of the first bus journey, the capacity of the fuel tank of the bus, the point where the gas station is located, and the required number of journeys.

Output
Print the minimum number of times the bus needs to refuel to make k journeys. If it is impossible for the bus to make k journeys, print -1.

Example
Input
6 9 2 4
Output
4
Input
6 10 2 4
Output
2
Input
6 5 4 3
Output
-1
Note
In the first example the bus needs to refuel during each journey.

In the second example the bus can pass 10 units of distance without refueling. So the bus makes the whole first journey, passes 4 units of the distance of the second journey and arrives at the point with the gas station. Then it can refuel its tank, finish the second journey and pass 2 units of distance from the third journey. In this case, it will again arrive at the point with the gas station. Further, he can refill the tank up to 10 liters to finish the third journey and ride all the way of the fourth journey. At the end of the journey the tank will be empty.

In the third example the bus can not make all 3 journeys because if it refuels during the second journey, the tanks will contain only 5 liters of gasoline, but the bus needs to pass 8 units of distance until next refuelin

題意 :起點座標(0,0),終點座標(a,0)。加油站座標(0,f), 從起點到終點算一次旅行,從終點返回起點算一次旅行,這樣不斷旅行k次。
每到一次加油站都可以加滿油,油箱滿油為b, 汽車走一單位費1汽油。問能否完成k次旅行,如果可以請輸出最小的加油數目。

分析 k才1e4,所以我們可以模擬旅行的過程,就好。

我寫的太醜了。

#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int>pii;
#define first fi
#define second se
#define  LL long long
#define fread() freopen("in.txt","r",stdin)
#define fwrite() freopen("out.txt","w",stdout)
#define CLOSE() ios_base::sync_with_stdio(false)

const int MAXN = 1e5;
const int MAXM = 1e6;
const int mod = 1e9+7;
const int inf = 0x3f3f3f3f;

int main(){
    CLOSE();
//  fread();
//  fwrite();
    int a,b,f,k;scanf("%d%d%d%d",&a,&b,&f,&k);
        int res = b ; int ans=0;
        for(int i=1;i<=k;i++){
            if(i&1){
                if(2*a-f>res){
                    if(i!=k){
                        ans++;
                        if(b<2*(a-f)||res<f) {
                            puts("-1");
                            return 0;
                        }
                        res=b-(a-f);
                    }else {
                        if(res<a) {
                            ans++; 
                            if(b<(a-f)||res<f) {
                                puts("-1");
                                return 0;
                            } 
                        }

                    }
                }else {
                    res-=a;
                }
            }else {
                if(a+f>res){
                    if(i!=k){
                        ans++;
                        if(res<(a-f)||b<2*f){
                            puts("-1");
                            return 0;
                        }
                        res=b-f;
                    }else {
                        if(res<a) {
                            ans++;
                            if(res<(a-f)||b<f){
                                puts("-1");
                                return 0;
                            }
                        }
                    }
                }else {
                    res-=a;
                }
            }
        //  printf("%d\n",res);
        }
        printf("%d\n",ans);
    return 0;
}

D - Make a Permutation!
Ivan has an array consisting of n elements. Each of the elements is an integer from 1 to n.

Recently Ivan learned about permutations and their lexicographical order. Now he wants to change (replace) minimum number of elements in his array in such a way that his array becomes a permutation (i.e. each of the integers from 1 to n was encountered in his array exactly once). If there are multiple ways to do it he wants to find the lexicographically minimal permutation among them.

Thus minimizing the number of changes has the first priority, lexicographical minimizing has the second priority.

In order to determine which of the two permutations is lexicographically smaller, we compare their first elements. If they are equal — compare the second, and so on. If we have two permutations x and y, then x is lexicographically smaller if xi < yi, where i is the first index in which the permutations x and y differ.

Determine the array Ivan will obtain after performing all the changes.

Input
The first line contains an single integer n (2 ≤ n ≤ 200 000) — the number of elements in Ivan’s array.

The second line contains a sequence of integers a1, a2, …, an (1 ≤ ai ≤ n) — the description of Ivan’s array.

Output
In the first line print q — the minimum number of elements that need to be changed in Ivan’s array in order to make his array a permutation. In the second line, print the lexicographically minimal permutation which can be obtained from array with q changes.

Example
Input
4
3 2 2 3
Output
2
1 2 4 3
Input
6
4 5 6 3 2 1
Output
0
4 5 6 3 2 1
Input
10
6 8 4 6 7 1 6 3 4 5
Output
3
2 8 4 6 7 1 9 3 10 5
Note
In the first example Ivan needs to replace number three in position 1 with number one, and number two in position 3 with number four. Then he will get a permutation [1, 2, 4, 3] with only two changed numbers — this permutation is lexicographically minimal among all suitable.

In the second example Ivan does not need to change anything because his array already is a permutation.

題意 :給定一個序列,問改變最少的元素使其成為1 2 3 4 5 …n的一個子序列,如果改變的個數相同,則輸出字典序最小的改變後的序列。
分析: 貪心
本來想貪心暴力過,結果超時,然後加了一個優先佇列就過了。

#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int>pii;
#define first fi
#define second se
#define  LL long long
#define fread() freopen("in.txt","r",stdin)
#define fwrite() freopen("out.txt","w",stdout)
#define CLOSE() ios_base::sync_with_stdio(false)

const int MAXN = 2e5+11;
const int MAXM = 1e6;
const int mod = 1e9+7;
const int inf = 0x3f3f3f3f;
inline int read(){
    int x=0;char ch=getchar();
    while(ch<'0'||ch>'9') {
        ch=getchar();
    }
    while(ch>='0'&&ch<='9') {
    x=x*10+ch-'0';
    ch=getchar();
    }
    return x;
}
int arr[MAXN];
int vis[MAXN];
int viss[MAXN];
priority_queue< int  , vector < int > , greater < int >  > Q;
int main(){
    CLOSE();
//  fread();
//  fwrite();
    int n;n=read();
    int cnt=0;
    for(int i=1;i<=n;i++){
        arr[i]=read();
        if(vis[arr[i]])  cnt++;
        vis[arr[i]]++;
    }
    printf("%d\n",cnt);
    if(cnt==0){
    for(int i=1;i<=n;i++) printf("%d ",arr[i]);
    }else {
        for(int i=1;i<=n;i++) if(vis[i]!=1)  Q.push(i);
        int ge;
        for(int i=1;i<=n;i++){
            if(vis[arr[i]]==1){
                printf("%d ",arr[i]);
                continue;
            }else{ 
                while(!Q.empty()){
                    ge=Q.top();
                    if(vis[ge]){
                        if(ge>=arr[i]&&!viss[arr[i]]){
                            ge=arr[i]; viss[arr[i]]=1;
                            break;
                        }
                        Q.pop();
                    }
                    else{
                     vis[ge]=1;  vis[arr[i]]--; 
                     break;
                    } 
                }
                printf("%d ",ge);
            }
        }
    } 
    return 0;
}