Codeforces 557A Ilya and Diplomas 区间选数

题意:给出3个区间 [L1,R1],[L2,R2],[L3,R3] 和正整数n,要求在3个区间内各选一个正整数,使得选出来的数之和为n。如果有多种选法,取从第一个区间内选出的数最大的选法。如果仍有多种选法,取从第二个区间中选出的数最大的选法,如果仍有多种选法,取从第三个区间内选出的数最大的选法。题目保证至少存在一种选法。







水题。要使第一个区间内选出的数尽量大,意思就是尽量让后两个区间选出的数尽量小,最小可以取到区间下界。于是第一个区间选的数ans1 = min(R1,n - L2 - L3)。同理分析,ans2 = min(R2,n - ans1 - L3)。ans3直接计算,等于n - ans1 - ans2。





#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <set>
using namespace std;

int n;
int l[3], r[3];

void input()
{
    for(int i = 0; i < 3; i++)
        scanf("%d%d", &l[i], &r[i]);
}

void solve()
{
    int ans1 = min(r[0], n - l[1] - l[2]);
    int ans2 = min(r[1], n - ans1 - l[2]);
    int ans3 = n - ans1 - ans2;
    printf("%d %d %d\n", ans1, ans2, ans3);
}

int main()
{
    while(scanf("%d", &n) != EOF)
    {
        input();
        solve();
    }
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值