1. 程式人生 > >[Codeforces Round #436 (Div. 2)]

[Codeforces Round #436 (Div. 2)]

A , B , C , D , E

F 有時間更新額 (感覺自己懶死了 , 逃~~)

算了 把E題先放到前面來吧
忘記看大神的寫法了。。。。有時間更新
Talk is cheap , show me the code (初中英語水平~~逃)

Polycarp is in really serious trouble — his house is on fire! It’s time to save the most valuable items. Polycarp estimated that it would take ti seconds to save i-th item. In addition, for each item, he estimated the value of di — the moment after which the item i will be completely burned and will no longer be valuable for him at all. In particular, if ti ≥ di, then i-th item cannot be saved.

Given the values pi for each of the items, find a set of items that Polycarp can save such that the total value of this items is maximum possible. Polycarp saves the items one after another. For example, if he takes item a first, and then item b, then the item a will be saved in ta seconds, and the item b — in ta + tb seconds after fire started.
Input

The first line contains a single integer n (1 ≤ n ≤ 100) — the number of items in Polycarp’s house.

Each of the following n lines contains three integers ti, di, pi (1 ≤ ti ≤ 20, 1 ≤ di ≤ 2 000, 1 ≤ pi ≤ 20) — the time needed to save the item i, the time after which the item i will burn completely and the value of item i.
Output

In the first line print the maximum possible total value of the set of saved items. In the second line print one integer m — the number of items in the desired set. In the third line print m distinct integers — numbers of the saved items in the order Polycarp saves them. Items are 1-indexed in the same order in which they appear in the input. If there are several answers, print any of them.

題意:救火,n件物品(t,d,p)救它要t,在d之後就不用救了(已經燒了),價值P,求拯救的最大值和拯救的次序
大致一看,如果不考慮d的因素,就是一道揹包DP 。
考慮已知答案,求次序:是貪心的做法,按照d的大小排序,貪心先救最先損壞的(這也是人之常情)
所以按照d排序後,用揹包DP的演算法,之後根據最大值,DFS(偽DFS,~~~)


/*******************************************************************
  author : *********
 problem : #
    time : 2017-09-26 21:11:26
 *******************************************************************/

#include <iostream>
#include <fstream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <time.h>
#include <limits.h>
#include <assert.h>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <list>
#include <bitset>
#include <vector>
using namespace std;

#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int>
#define pi pair<int,int>
#define Pr(kcase,ans) printf("Case #%d: %lld\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<" "; cout<<a[n]<<endl;
#define lowbit(x) ((x)&-(x))
#define SI(a) ((a).size())
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,0x3f,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define MEMx(a,b) memset(a,b,sizeof(a));
#define MAX 35
#define INF (0x3f3f3f3f)
#define F (1000000007)
#define LL long long
const int N = 2005;
int f[105][N];
struct WHATA {int t, d, p, n;} WTF[105];
bool cmp(WHATA a, WHATA b) {return a.d < b.d;}
vector<int>v;
void DFS(int s, int d, int p) {
    // printf("s = %d d = %d p = %d \n",s,d,p);
    if (s == 0 || p == 0) return;
    if(f[s-1][d] == p) DFS(s-1,d,p);
    else {
        RepD(i,d-WTF[s].t) {
            // printf("f[%d][%d] = %d wtf[%d].p = %d p = %d\n"
                // ,s-1,i,f[s-1][i],s,WTF[s].p,p);
            if(f[s-1][i] + WTF[s].p == p) {
                DFS(s-1,i,p-WTF[s].p);
                break;
            }
        }
        v.pb(s);
    }
}
int main(int argc, char const *argv[])
{
    // freopen("data.in", "r", stdin);
    int n; scanf("%d", &n);
    For(i, n) scanf("%d%d%d", &WTF[i].t, &WTF[i].d, &WTF[i].p), WTF[i].n = i;
    sort(WTF + 1, WTF + n + 1, cmp);
    // Rep(i,n) printf("%d\n",WTF[i].d);
    For(i, n) {
        Rep(j, N) {
            if (j - WTF[i].t >= 0 && j < WTF[i].d)
                f[i][j] = max(f[i - 1][j], f[i - 1][j - WTF[i].t] + WTF[i].p);
            else f[i][j] = f[i - 1][j];
        }
    }
    int pos , maxx = 0;
    Rep(i, N) if (maxx < f[n][i]) maxx = f[n][i] , pos = i;
    DFS(n, pos, maxx);
    printf("%d\n%d\n",maxx,v.size());
    Rep(i,v.size()) printf(i==v.size()-1?"%d\n":"%d ",WTF[v[i]].n);
    return 0;
}

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.

A 題照例水題 , 直接統計就好了
題意:給你一個數組,問能否分成兩個長度相等,裡面元素都一樣的子集
強行使用STL


/*******************************************************************
  author : *******
 problem : A
    time : 2017-09-26 18:52:08
 *******************************************************************/

#include <iostream>
#include <fstream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <time.h>
#include <limits.h>
#include <assert.h>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <list>
#include <bitset>
#include <vector>
using namespace std;

#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int>
#define pi pair<int,int>
#define Pr(kcase,ans) printf("Case #%d: %lld\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<" "; cout<<a[n]<<endl;
#define lowbit(x) ((x)&-(x))
#define SI(a) ((a).size())
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,0x3f,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define MEMx(a,b) memset(a,b,sizeof(a));
#define MAX 35
#define INF (0x3f3f3f3f)
#define F (1000000007)
#define LL long long

int main(int argc, char const *argv[])
{
    ios::sync_with_stdio(false);// you may need this
    // fstream cin("data.in");
    // freopen("data.in", "r", stdin);
    int n;cin>>n;
    std::vector<int> arr(105,0);
    std::vector<int> brr;
    Rep(i,n) {
        int x ; cin>>x;
        if(arr[x]==0) brr.pb(x);
        arr[x]++;
    }
    if(brr.size()!=2 || arr[brr[0]] != arr[brr[1]]) return 0*printf("NO\n");
    sort(brr.begin(), brr.end());
    cout<<"YES"<<endl;
    cout<<brr[0]<<" "<<brr[1]<<endl;
    return 0;
}

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.

B 題 感覺也好水 的說
題意:給你一個字串,對其中每個連續小寫字母組成的串,問其中串中不同字母數的最大值是多少?
直接模擬就好了


/*******************************************************************
  author : ********
 problem : #
    time : 2017-09-26 19:06:25
 *******************************************************************/

#include <iostream>
#include <fstream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <time.h>
#include <limits.h>
#include <assert.h>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <list>
#include <bitset>
#include <vector>
using namespace std;

#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int>
#define pi pair<int,int>
#define Pr(kcase,ans) printf("Case #%d: %lld\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<" "; cout<<a[n]<<endl;
#define lowbit(x) ((x)&-(x))
#define SI(a) ((a).size())
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,0x3f,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define MEMx(a,b) memset(a,b,sizeof(a));
#define MAX 35
#define INF (0x3f3f3f3f)
#define F (1000000007)
#define LL long long

int main(int argc, char const *argv[])
{
    ios::sync_with_stdio;
    // fstream cin("data.in");
    // freopen("data.in", "r", stdin);
    int len;cin>>len;
    string s;cin>>s;    
    int now = 0 , maxx = 0;
    bool lowletter[26] = {false};
    Rep(i,len) {;
        if(s[i]<='z' && s[i]>='a') {
            if(lowletter[s[i] - 'a'] == false) now++ , lowletter[s[i]-'a'] = true;
        }
        else  {
            maxx = max(maxx,now);
            now = 0;
            MEM(lowletter);
        }
    }
    maxx = max(maxx,now);
    cout<<maxx<<endl;
    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.

題意:公交車從0到a 在f加油,油箱中有b ,往返算兩次,問k次最少加多次油

感覺弄了個傻傻的寫法,直接模擬,每次到加油站計算剩下的油是否夠到下次加油站。
如果夠,就不加;否則 就加;


/*******************************************************************
  author : ********
 problem : #
    time : 2017-09-26 19:23:18
 *******************************************************************/

#include <iostream>
#include <fstream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <time.h>
#include <limits.h>
#include <assert.h>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <list>
#include <bitset>
#include <vector>
using namespace std;

#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int>
#define pi pair<int,int>
#define Pr(kcase,ans) printf("Case #%d: %lld\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<" "; cout<<a[n]<<endl;
#define lowbit(x) ((x)&-(x))
#define SI(a) ((a).size())
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,0x3f,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define MEMx(a,b) memset(a,b,sizeof(a));
#define MAX 35
#define INF (0x3f3f3f3f)
#define F (1000000007)
#define LL long long
struct Car
{
    int soil;
}car;
int main(int argc, char const *argv[])
{
    ios::sync_with_stdio;
    // fstream cin("data.in");
    int a,b,f,k;cin>>a>>b>>f>>k;
    car.soil = b ;
    LL ans = 0 ;
    For(i,k) {
        if(i==1) car.soil -= f;
        if(car.soil<0) {
            ans = -1;break;
        }
        int ll = i&1 ? 2*(a-f) : 2*f;
        if(i==k) ll = i&1? a-f : f;
        // printf("i = %d ll = %d left = %d \n",i,ll,car.soil);
        if(ll>car.soil) {
            car.soil = b ;
            ans++;
        }
        if(car.soil<ll) {
            ans = -1;
            break;
        }
        car.soil-=ll;
    }
    printf("%lld\n",ans );
    return 0;
}

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≤200000) — 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.

D 題 本質上是不難的 , 主要是怎麼寫的簡單~
題意:對給定陣列,最少修改多少個數字,使陣列變成1~n的全排列,輸出最少改變下字典序最小的答案
對可以修改的位置判斷是否修改,(感覺說的好簡單)


/*******************************************************************
  author : ********
 problem : #
    time : 2017-09-26 19:50:02
 *******************************************************************/

#include <iostream>
#include <fstream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <time.h>
#include <limits.h>
#include <assert.h>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <list>
#include <bitset>
#include <vector>
using namespace std;

#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int>
#define pi pair<int,int>
#define Pr(kcase,ans) printf("Case #%d: %lld\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<" "; cout<<a[n]<<endl;
#define lowbit(x) ((x)&-(x))
#define SI(a) ((a).size())
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,0x3f,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define MEMx(a,b) memset(a,b,sizeof(a));
#define MAX 35
#define INF (0x3f3f3f3f)
#define F (1000000007)
#define LL long long

int main(int argc, char const *argv[])
{
    // ios::sync_with_stdio;
    fstream cin("data.in");
    int n;cin>>n;
    std::vector<int> vis(n+1,0);
    std::vector<int> ans;
    ans.pb(0);
    std::vector<int> arr;
    std::vector<int> mark(n+1,0);
    For(i,n) {
        int x;cin>>x;
        ans.pb(x);
        vis[x]++;
    }
    For(i,n) {
        if(vis[i]==0) arr.pb(i);
    }
    sort(arr.begin(), arr.end());
    int tot = 0;
    printf("%d\n",arr.size());
    // Rep(i,arr.size()) printf(i==arr.size()-1?"%d\n":"%d ",arr[i]);
    // For(i,n) printf(i==n?"%d\n":"%d ",ans[i]);
    For(i,n) {
        int tmp;
        if(vis[ans[i]]==1 && mark[ans[i]]==0) tmp = ans[i];
        else if(mark[ans[i]]==0 && ans[i]<arr[tot]) {
            tmp = ans[i];
            mark[ans[i]] = 1;
            vis[ans[i]]--;          
        }
        else {
            tmp = arr[tot++];
            vis[ans[i]]--;
        }
        printf(i==n ?"%d\n":"%d " ,tmp);
    }
    return 0;
}