【55.70%】【codeforces 557A】Ilya and Diplomas

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Soon a school Olympiad in Informatics will be held in Berland, n schoolchildren will participate there.

At a meeting of the jury of the Olympiad it was decided that each of the n participants, depending on the results, will get a diploma of the first, second or third degree. Thus, each student will receive exactly one diploma.

They also decided that there must be given at least min1 and at most max1 diplomas of the first degree, at least min2 and at most max2 diplomas of the second degree, and at least min3 and at most max3 diplomas of the third degree.

After some discussion it was decided to choose from all the options of distributing diplomas satisfying these limitations the one that maximizes the number of participants who receive diplomas of the first degree. Of all these options they select the one which maximizes the number of the participants who receive diplomas of the second degree. If there are multiple of these options, they select the option that maximizes the number of diplomas of the third degree.

Choosing the best option of distributing certificates was entrusted to Ilya, one of the best programmers of Berland. However, he found more important things to do, so it is your task now to choose the best option of distributing of diplomas, based on the described limitations.

It is guaranteed that the described limitations are such that there is a way to choose such an option of distributing diplomas that all n participants of the Olympiad will receive a diploma of some degree.

Input
The first line of the input contains a single integer n (3 ≤ n ≤ 3·106) — the number of schoolchildren who will participate in the Olympiad.

The next line of the input contains two integers min1 and max1 (1 ≤ min1 ≤ max1 ≤ 106) — the minimum and maximum limits on the number of diplomas of the first degree that can be distributed.

The third line of the input contains two integers min2 and max2 (1 ≤ min2 ≤ max2 ≤ 106) — the minimum and maximum limits on the number of diplomas of the second degree that can be distributed.

The next line of the input contains two integers min3 and max3 (1 ≤ min3 ≤ max3 ≤ 106) — the minimum and maximum limits on the number of diplomas of the third degree that can be distributed.

It is guaranteed that min1 + min2 + min3 ≤ n ≤ max1 + max2 + max3.

Output
In the first line of the output print three numbers, showing how many diplomas of the first, second and third degree will be given to students in the optimal variant of distributing diplomas.

The optimal variant of distributing diplomas is the one that maximizes the number of students who receive diplomas of the first degree. Of all the suitable options, the best one is the one which maximizes the number of participants who receive diplomas of the second degree. If there are several of these options, the best one is the one that maximizes the number of diplomas of the third degree.

Examples
input
6
1 5
2 6
3 7
output
1 2 3
input
10
1 2
1 3
1 5
output
2 3 5
input
6
1 3
2 2
2 2
output
2 2 2

【题目链接】:http://codeforces.com/contest/557/problem/A

【题解】

题目长(chang)得吓人。
就是让你选择每个等级的人有多少人;
优先级为1,2,3
尽量让优先级高的人人数多;
每个等级的人的人数有最小值、最大值的限制;
显然先假设让第2、3等级的人最小;
n-min2-min3分配给第一个人;
大于max1的部分再还给2和3;
2和3重复上述过程即可;

【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%I64d",&x)
#define pri(x) printf("%d",x)
#define prl(x) printf("%I64d",x)

typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

//const int MAXN = x;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);

int n;
int mi[4],ma[4];

int main()
{
    //freopen("F:\rush.txt","r",stdin);
    rei(n);
    rep1(i,1,3)
        rei(mi[i]),rei(ma[i]);
    int bpc = mi[2]+mi[3],n1,n2,n3;
    if (n-bpc>=mi[1] && n-bpc<=ma[1])
    {
        n1 = n-bpc;
        n=bpc;
    }
    else
        if (n-bpc>ma[1])
        {
            n1 = ma[1];
            n = n-ma[1];
        }
    if (n-mi[3]>=mi[2] && n-mi[3]<=ma[2])
    {
        n2 = n-mi[3];
        n = mi[3];
    }
    else
        if (n-mi[3]>ma[2])
        {
            n2 = ma[2];
            n-=ma[2];
        }
    n3 = n;
    printf("%d %d %d
",n1,n2,n3);
    return 0;
}
原文地址:https://www.cnblogs.com/AWCXV/p/7626842.html