Codeforces Beta Round #4 (Div. 2 Only) B. Before an Exam dp
B. Before an Exam
题目连接:
http://www.codeforces.com/contest/4/problem/B
Description
Tomorrow Peter has a Biology exam. He does not like this subject much, but d days ago he learnt that he would have to take this exam. Peter's strict parents made him prepare for the exam immediately, for this purpose he has to study not less than minTimei and not more than maxTimei hours per each i-th day. Moreover, they warned Peter that a day before the exam they would check how he has followed their instructions.
So, today is the day when Peter's parents ask him to show the timetable of his preparatory studies. But the boy has counted only the sum of hours sumTime spent him on preparation, and now he wants to know if he can show his parents a timetable sсhedule with d numbers, where each number sсhedulei stands for the time in hours spent by Peter each i-th day on biology studies, and satisfying the limitations imposed by his parents, and at the same time the sum total of all schedulei should equal to sumTime.
Input
The first input line contains two integer numbers d, sumTime (1 ≤ d ≤ 30, 0 ≤ sumTime ≤ 240) — the amount of days, during which Peter studied, and the total amount of hours, spent on preparation. Each of the following d lines contains two integer numbers minTimei, maxTimei (0 ≤ minTimei ≤ maxTimei ≤ 8), separated by a space — minimum and maximum amount of hours that Peter could spent in the i-th day.
Output
In the first line print YES, and in the second line print d numbers (separated by a space), each of the numbers — amount of hours, spent by Peter on preparation in the corresponding day, if he followed his parents' instructions; or print NO in the unique line. If there are many solutions, print any of them.
Sample Input
1 48
5 7
Sample Output
NO
Hint
题意
有n天,你一共学习了m个小时。
这n天每天你必须学习li个小时,但是不能超过ri小时。
问你m个小时能不能合理分配,满足这n天的要求。
如果可以,输出方案。
题解:
直接dp就好了,dp[i][j]表示到了第i天,一共学习了j个小时。
然后记录一下从哪儿转移过来的就好了。
不过数据范围好小,估计怎么做都可以……
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 35;
int n,m;
int l[maxn],r[maxn];
int dp[35][1200],from[35][1200];
void solve(int x,int y)
{
if(x!=1)solve(x-1,from[x][y]);
printf("%d ",y-from[x][y]);
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
scanf("%d%d",&l[i],&r[i]);
dp[0][0]=1;
for(int i=1;i<=n;i++)
{
for(int j=0;j<1000;j++)
{
if(dp[i-1][j])
{
for(int k=l[i];k<=r[i];k++)
{
dp[i][j+k]=1;
from[i][j+k]=j;
}
}
}
}
if(dp[n][m])
{
cout<<"YES"<<endl;
solve(n,m);
}
else cout<<"NO"<<endl;
}
Codeforces Beta Round #4 (Div. 2 Only) B. Before an Exam dp的更多相关文章
- Codeforces Beta Round #9 (Div. 2 Only) D. How many trees? dp
D. How many trees? 题目连接: http://www.codeforces.com/contest/9/problem/D Description In one very old t ...
- Codeforces Beta Round #80 (Div. 2 Only)【ABCD】
Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...
- Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】
Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...
- Codeforces Beta Round #79 (Div. 2 Only)
Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...
- Codeforces Beta Round #77 (Div. 2 Only)
Codeforces Beta Round #77 (Div. 2 Only) http://codeforces.com/contest/96 A #include<bits/stdc++.h ...
- Codeforces Beta Round #76 (Div. 2 Only)
Codeforces Beta Round #76 (Div. 2 Only) http://codeforces.com/contest/94 A #include<bits/stdc++.h ...
- Codeforces Beta Round #75 (Div. 2 Only)
Codeforces Beta Round #75 (Div. 2 Only) http://codeforces.com/contest/92 A #include<iostream> ...
- Codeforces Beta Round #74 (Div. 2 Only)
Codeforces Beta Round #74 (Div. 2 Only) http://codeforces.com/contest/90 A #include<iostream> ...
- Codeforces Beta Round #73 (Div. 2 Only)
Codeforces Beta Round #73 (Div. 2 Only) http://codeforces.com/contest/88 A 模拟 #include<bits/stdc+ ...
随机推荐
- Mac 下安装 ruby 环境解决 brew 安装 yarn 问题
在brew安装yarn提示 ruby的版本过低.在网上搜了一下发现 1. mac下自带的ruby 在 system 目录下 2. 其实可以用brew安装一个ruby brew install ruby ...
- Perl6多线程3: Promise start / in / await
创建一个Promise 并自动运行: my $p = Promise.start({say 'Hello, Promise!'}); 如果把代码改成如下, 我们会发现什么也没打印: ;say 'Hel ...
- C语言回调函数总结
/* Main program ---calls--> Library function ---calls--> Callback funtion */ #include <stdi ...
- Tutorial 4: Authentication & Permissions
转载自:http://www.django-rest-framework.org/tutorial/4-authentication-and-permissions/ Tutorial 4: Auth ...
- 转:google测试分享-分层测试
原文: http://blog.sina.com.cn/s/blog_6cf812be0102vctg.html 上一次分享了google测试分享-SET和TE,有一些自动化测试的细节没有说清楚,那这 ...
- leetcode 之Search in Rotated Sorted Array(三)
描述 Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 ...
- 查找网页元素对应的js代码
按F12打开调试窗口,切换到Sources选项卡,最右边的Event Listener Breakpoints里勾选Mouse下的mouseover即可,当鼠标移动到图片上时触发mouseover事件 ...
- python基础(9)--递归、二叉算法、多维数组、正则表达式
1.递归 在函数内部,可以调其他函数,如果一个函数在内部调用它本身,这个函数就是递归函数.递归算法对解决一大类问题是十分有效的,它往往使算法的描述简洁而且易于裂解 递归算法解决问题的特点: 1)递归是 ...
- 非常粗糙的react网页ppt
import React, {Component} from 'react'; import './slide.css'; class Page extends Component { constru ...
- 【caffe-Windows】微软官方caffe之matlab接口配置,以及安装caffe的注意事项
1.在此之前,记录一下之前的错误,在参考博客[caffe-Windows]caffe+VS2013+Windows+GPU配置+cifar使用进行caffe的安装时,其中的一些步骤可以不做,具体见下图 ...