题意:给出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;
}