Codeforces Round #222 (Div. 2)

u010660276發表於2014-01-05
A. Playing with Dice

Two players are playing a game. First each of them writes an integer from 1 to 6, and then a dice is thrown. The player whose written number got closer to the number on the dice wins. If both payers have the same difference, it's a draw.

The first player wrote number a, the second player wrote numberb. How many ways to throw a dice are there, at which the first player wins, or there is a draw, or the second player wins?

Input

The single line contains two integers a andb (1 ≤ a, b ≤ 6) — the numbers written on the paper by the first and second player, correspondingly.

Output

Print three integers: the number of ways to throw the dice at which the first player wins, the game ends with a draw or the second player wins, correspondingly.

Sample test(s)
Input
2 5
Output
3 0 3
Input
2 4
Output
2 1 3
Note

The dice is a standard cube-shaped six-sided object with each side containing a number from 1 to 6, and where all numbers on all sides are distinct.

You can assume that number a is closer to numberx than number b, if|a - x| < |b - x|.

題意:兩個人先寫一個數,然後擲篩子,誰挨著近誰贏,問贏,平個有多少種情況

注意兩個人猜的數字相等的情況

#include<iostream>
using namespace std;
int numa,numb,numdraw;
int a,b;
int cntdraw()
{
    return !((a+b)%2);
}
int cnta()
{
    return a<b?((a+b)/2-numdraw):(6-(a+b)/2);
}
int cntb()
{
    return a>b?((a+b)/2-numdraw):(6-(a+b)/2);
}
int main()
{
    cin>>a>>b;
    if(a==b)
    {
        cout<<0<<' '<<6<<' '<<0<<endl;
        return 0;
    }
    numa=numb=numdraw=0;
    numdraw=cntdraw();
    numa=cnta();
    numb=cntb();
    cout<<numa<<' '<<numdraw<<' '<<numb<<endl;
}

B. Semifinals

Two semifinals have just been in the running tournament. Each semifinal had n participants. There are n participants advancing to the finals, they are chosen as follows: from each semifinal, we choosek people (0 ≤ 2k ≤ n) who showed the best result in their semifinals and all other places in the finals go to the people who haven't ranked in the topk in their semifinal but got to the n - 2k of the best among the others.

The tournament organizers hasn't yet determined the k value, so the participants want to know who else has any chance to get to the finals and who can go home.

Input

The first line contains a single integer n (1 ≤ n ≤ 105) — the number of participants in each semifinal.

Each of the next n lines contains two integersai andbi (1 ≤ ai, bi ≤ 109) — the results of the i-th participant (the number of milliseconds he needs to cover the semifinals distance) of the first and second semifinals, correspondingly. All results are distinct. Sequencesa1, a2, ..., an andb1, b2, ..., bn are sorted in ascending order, i.e. in the order the participants finished in the corresponding semifinal.

Output

Print two strings consisting of n characters, each equals either "0" or "1". The first line should correspond to the participants of the first semifinal, the second line should correspond to the participants of the second semifinal. Thei-th character in the j-th line should equal "1" if thei-th participant of the j-th semifinal has any chances to advance to the finals, otherwise it should equal a "0".

Sample test(s)
Input
4
9840 9920
9860 9980
9930 10020
10040 10090
Output
1110
1100
Input
4
9900 9850
9940 9930
10000 10020
10060 10110
Output
1100
1100
Note

Consider the first sample. Each semifinal has 4 participants. The results of the first semifinal are 9840, 9860, 9930, 10040. The results of the second semifinal are 9920, 9980, 10020, 10090.

  • If k = 0, the finalists are determined by the time only, so players 9840, 9860, 9920 and 9930 advance to the finals.
  • If k = 1, the winners from both semifinals move to the finals (with results 9840 and 9920), and the other places are determined by the time (these places go to the sportsmen who run the distance in 9860 and 9930 milliseconds).
  • If k = 2, then first and second places advance from each seminfial, these are participants with results 9840, 9860, 9920 and 9980 milliseconds.
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
const int maxn=1000010;
int one[maxn],two[maxn];
void print(int k,int n)
{
    for(int i=0;i<=k;i++)
    printf("1");
    for(int i=k+1;i<n;i++)
    printf("0");
}
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    scanf("%d%d",&one[i],&two[i]);
    int i=0,j=0,num=0;
    while(num<n)
    {
        if(one[i]<two[j])
        i++;
        else j++;
        num++;
    }
    int k=max(i-1,n/2-1);
    print(k,n);
    puts("");
    k=max(j-1,n/2-1);
    print(k,n);
    return 0;
}

C. Maze

Pavel loves grid mazes. A grid maze is an n × m rectangle maze where each cell is either empty, or is a wall. You can go from one cell to another only if both cells are empty and have a common side.

Pavel drew a grid maze with all empty cells forming a connected area. That is, you can go from any empty cell to any other one. Pavel doesn't like it when his maze has too little walls. He wants to turn exactlyk empty cells into walls so that all the remaining cells still formed a connected area. Help him.

Input

The first line contains three integers n,m, k (1 ≤ n, m ≤ 500,0 ≤ k < s), where n and m are the maze's height and width, correspondingly,k is the number of walls Pavel wants to add and letters represents the number of empty cells in the original maze.

Each of the next n lines contains m characters. They describe the original maze. If a character on a line equals ".", then the corresponding cell is empty and if the character equals "#", then the cell is a wall.

Output

Print n lines containing m characters each: the new maze that fits Pavel's requirements. Mark the empty cells that you transformed into walls as "X", the other cells must be left without changes (that is, "." and "#").

It is guaranteed that a solution exists. If there are multiple solutions you can output any of them.

Sample test(s)
Input
3 4 2
#..#
..#.
#...
Output
#.X#
X.#.
#...
Input
5 4 5
#...
#.#.
.#..
...#
.#.#
Output
#XXX
#X#.
X#..
...#
.#.#
題意:給你一個迷宮,把其中的k個空地變成牆,是迷宮還是聯通的

dfs一遍把剩餘的空地標記一下。

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int maxn=550;
int n,m,k,num;
char grid[maxn][maxn];
int s,e;
int dx[]= {1,-1,0,0};
int dy[]= {0,0,1,-1};
int cnt;
void dfs(int x,int y)
{
    if(cnt>=num-k)
        return;
    grid[x][y]='X';
    for(int i=0; i<4; i++)
    {
        int tx=x+dx[i],ty=y+dy[i];
        if(tx>=0&&tx<n&&ty>=0&&ty<m&&grid[tx][ty]=='.')
        {
            cnt++;
            dfs(tx,ty);
        }
    }
}
void print()
{
    for(int i=0; i<n; i++)
    {
        for(int j=0; j<m; j++)
            if(grid[i][j]=='.') cout<<'X';
            else if(grid[i][j]=='X')cout<<'.';
            else cout<<grid[i][j];
        cout<<endl;
    }
}
int main()
{
    scanf("%d%d%d",&n,&m,&k);
    num=0;
    getchar();
    for(int i=0; i<n; i++)
    {
        scanf("%s",grid[i]);
        //cout<<grid[i]<<endl;
        getchar();
    }
    for(int i=0; i<n; i++)
        for(int j=0; j<m; j++)
            if(grid[i][j]=='.')
            {
                num++,s=i,e=j;
            }
    cnt=0;
    dfs(s,e);
    print();
    return 0;
}
D. Preparing for the Contest

Soon there will be held the world's largest programming contest, but the testing system still has m bugs. The contest organizer, a well-known university, has no choice but to attract university students to fix all the bugs. The university has n students able to perform such work. The students realize that they are the only hope of the organizers, so they don't want to work for free: the i-th student wants to get ci 'passes' in his subjects (regardless of the volume of his work).

Bugs, like students, are not the same: every bug is characterized by complexity aj, and every student has the level of his abilities bi. Student i can fix a bug j only if the level of his abilities is not less than the complexity of the bug: bi ≥ aj, and he does it in one day. Otherwise, the bug will have to be fixed by another student. Of course, no student can work on a few bugs in one day. All bugs are not dependent on each other, so they can be corrected in any order, and different students can work simultaneously.

The university wants to fix all the bugs as quickly as possible, but giving the students the total of not more than s passes. Determine which students to use for that and come up with the schedule of work saying which student should fix which bug.

Input

The first line contains three space-separated integers: n, m and s (1 ≤ n, m ≤ 105, 0 ≤ s ≤ 109) — the number of students, the number of bugs in the system and the maximum number of passes the university is ready to give the students.

The next line contains m space-separated integers a1, a2, ..., am (1 ≤ ai ≤ 109) — the bugs' complexities.

The next line contains n space-separated integers b1, b2, ..., bn (1 ≤ bi ≤ 109) — the levels of the students' abilities.

The next line contains n space-separated integers c1, c2, ..., cn (0 ≤ ci ≤ 109) — the numbers of the passes the students want to get for their help.

Output

If the university can't correct all bugs print "NO".

Otherwise, on the first line print "YES", and on the next line print m space-separated integers: the i-th of these numbers should equal the number of the student who corrects the i-th bug in the optimal answer. The bugs should be corrected as quickly as possible (you must spend the minimum number of days), and the total given passes mustn't exceed s. If there are multiple optimal answers, you can output any of them.

Sample test(s)
Input
3 4 9
1 3 1 2
2 1 3
4 3 6
Output
YES
2 3 2 3
Input
3 4 10
2 3 1 2
2 1 3
4 3 6
Output
YES
1 3 1 3
Input
3 4 9
2 3 1 2
2 1 3
4 3 6
Output
YES
3 3 2 3
Input
3 4 5
1 3 1 2
2 1 3
5 3 6
Output
NO
Note

Consider the first sample.

The third student (with level 3) must fix the 2nd and 4th bugs (complexities 3 and 2 correspondingly) and the second student (with level 1) must fix the 1st and 3rd bugs (their complexity also equals 1). Fixing each bug takes one day for each student, so it takes 2 days to fix all bugs (the students can work in parallel).

The second student wants 3 passes for his assistance, the third student wants 6 passes. It meets the university's capabilities as it is ready to give at most 9 passes.


題意:有n個人,m個病毒,s張通行證,然後給出m個病毒的等級,n個人的等級,以及n個人去殺病毒所需要的pass數量,問所最少花費幾天可以殺光病毒,並輸出每個病毒被那一個人所清理。PS:人要殺病毒必須等級大於等於病毒,一個人只需支付一次pass。
剛開始有點思路,但有的地方沒想通,參考了別人的程式碼

#include<iostream>
#include<algorithm>
#include<queue>
#include<cstdio>
using namespace std;
const int maxn=100010;
struct node
{
    int abi,pass,id;
    friend bool operator<(const node &a,const node &b)
    {
        return a.pass>b.pass;
    }
};
node stu[maxn],vir[maxn];
int N,M,S;
bool cmp(node a,node b)
{
    return a.abi>b.abi;
}
bool can(int mid)
{
    int b=1,sum=S,i=1;
    priority_queue<node> q;
    while(b<=M)
    {
        for(;i<=N;i++)
        {
            if(stu[i].abi>=vir[b].abi)//每次找比他大的入隊
            q.push(stu[i]);
            else break;
        }
        if(q.empty()) return false;
        node tmp=q.top();
        q.pop();
        if(sum<tmp.pass) return false;
        sum-=tmp.pass;
        b+=mid;//這個大的可以連續工作mid天,消滅mid個病毒
    }
    return true;
}
void put(int ans)
{
    int i=1,b=1;
    int who[maxn];
    priority_queue<node> q;
    while(b<=M)
    {
        for(;i<=N;i++)
        {
            if(stu[i].abi>=vir[b].abi)
            q.push(stu[i]);
            else break;
        }
        node tmp=q.top();
        q.pop();
        int t=min(M,b+ans-1);
        for(int k=b;k<=t;k++)
        who[vir[k].id]=tmp.id;
        if(t>=M) break;
        b+=ans;
    }
    bool first=true;
    for(int i=1;i<=M;i++)
    {
        if(first){cout<<who[i];first=false;}
        else cout<<" "<<who[i];
    }
    cout<<endl;
}
int main()
{
    cin>>N>>M>>S;
    for(int i=1;i<=M;i++){scanf("%d",&vir[i].abi);vir[i].id=i;}
    for(int i=1;i<=N;i++){scanf("%d",&stu[i].abi);stu[i].id=i;}
    for(int i=1;i<=N;i++)scanf("%d",&stu[i].pass);
    sort(vir+1,vir+1+M,cmp);
    sort(stu+1,stu+1+N,cmp);
    if(!can(M))
    {
        cout<<"NO"<<endl;
        return 0;
    }
    int l=0,r=M,mid;
    while(l<r)
    {
        mid=(l+r)/2;
        if(can(mid)) r=mid;
        else l=mid+1;
    }
    cout<<"YES"<<endl;
    put(l);
    return 0;
}


相關文章