题目大意:现有d个区间, 现在在每个区间中选一个数,使得总和等于他给定的一个数。
思路:先把每个区间的最小值选了, 在遍历每个区间, 如果当前这个区间的最大值选了还不能组合成sumTime,就选最大的数, 否则就选一个合适的数
code:
#include <iostream>
using namespace std;
int d = 0, sum = 0, flag = 0, sumTime = 0, ans[32];
struct node
{
int x, y;
}day[32];
void dfs(int c)
{
int i = 0;
if(day[c].y-day[c].x<sumTime)
{
sumTime -= day[c].y-day[c].x;
ans[c] += day[c].y-day[c].x;
dfs(c+1);
}
else
{
flag = 1;
ans[c] += sumTime;
cout<<"YES"<<endl;
for(i = 0; i<d-1; i++)
cout<<ans[i]<<" ";
cout<<ans[i]<<endl;
return ;
}
}
int main()
{
int i = 0, max = 0, min = 0;
while(cin>>d>>sumTime)
{
flag = min = max = 0;
for(i = 0; i<d; i++)
{
cin>>day[i].x>>day[i].y;
max += day[i].y;
min += day[i].x;
ans[i] = day[i].x;
}
if(max<sumTime) cout<<"NO"<<endl;
else if(min>sumTime) cout<<"NO"<<endl;
else
{
sumTime -= min;
dfs(0);
if(!flag)
cout<<"NO"<<endl;
}
}
return 0;
}