Kattis - bank 【简单DP】
Kattis - bank 【简单DP】
Description
Oliver is a manager of a bank near KTH and wants to close soon. There are many people standing in the queue wanting to put cash into their accounts after they heard that the bank increased the interest rates by 42% (from 0.01% per year to 0.0142% per year).
However, there are too many people and only one counter is open which can serve one person per minute. Greedy as Oliver is, he would like to select some people in the queue, so that the total amount of cash stored by these people is as big as possible and that money then can work for the bank overnight.
There is a problem, though. Some people don’t have the time to wait until the bank closes because they have to run somewhere else, so they have to be served before a certain time, after which they just leave. Oliver also turned off the infrared door sensor outside the bank, so that no more people can enter, because it’s already too crowded in the hall.
Task
Help Oliver calculate how much cash he can get from the people currently standing in the queue before the bank closes by serving at most one person per minute.
Input
The first line of input contains two integers N
(1≤N≤10000) and T (1≤T≤47), the number of people in the queue and the time in minutes until Oliver closes the bank. Then follow N lines, each with 2 integers ci and ti, denoting the amount of cash in Swedish crowns person i has and the time in minutes from now after which person i leaves if not served. Note that it takes one minute to serve a person and you must begin serving a person at time ti at the latest. You can assume that 1≤ci≤100000 and 0 ≤ ti < T
Output
Output one line with the maximum amount of money you can get from the people in the queue before the bank closes.
Sample Input 1
4 4
1000 1
2000 2
500 2
1200 0
Sample Output 1
4200
Sample Input 2
3 4
1000 0
2000 1
500 1
Sample Output 2
3000
题意
大概就是有很多人去银行排队存钱,银行当然希望存的钱越多越好。但是每个客户的忍耐是有限度的。银行每分钟处理一个客户后每个客户有一个最大的等待时间,就是到了这个时间,银行还不存他的钱,他就走了。求银行最大能得到多少存款。
思路一
这个是先将数据按照等待时间的大小 从小到大排序,然后用01背包的模型,但是在状态转移之前 判断一下 目前的时间 是不是小于等于客户的最大等待时间,如果是,就可以进行转移。但是,最后转移的状态不是dp[t], 所以 在规划过程中找一下最大值,输出最大值就可以了。
代码一
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<string>
#include<sstream>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<stack>
using namespace std;
const int MAX = 0x3f3f3f3f;
const int MIN = 0xc0c0c0c0;
const int maxn = 1e4 + 5;
struct node
{
int v, t, pre;
}q[maxn];
int dp[50];
int comp(node x, node y)
{
if (x.t == y.t) return x.v > y.v;
return x.t < y.t;
}
int main()
{
int n, t;
cin >> n >> t;
int i, j, k;
for (i = 0; i < n; i++)
scanf("%d%d", &q[i].v, &q[i].t);
sort(q, q+n, comp);
memset(dp, 0, sizeof(dp));
int MAXN = MIN;
for (i = 0; i < n; i++)
{
for (j = t; j >= 1; j--)
{
if (j - 1 <= q[i].t)
{
dp[j] = max(dp[j], dp[j - 1] + q[i].v);
}
if (dp[j] > MAXN) //找出规划过程中的最大值
MAXN = dp[j];
}
}
cout << MAXN << endl;
}
思路二
我们可以先按客户存钱的多少 从大到小排序 然后定义一个数组 比如 dp[t],用来存放当前时间点的客户存钱数 刚开始初始化为0; 对客户的人数 从 0 -> N FOR 一遍 FOR 的时候 每次判断一下 dp[t [i] ] 是否 == 0 如果 == 0 的话 就直接 赋值就可以了 如果 != 0 那就要往前找 因为 我们首先是按照钱的多少来排序的,如果在DP数组往前找 找不到一个位置 == 0(或者理解为前面的都已经安排好了) 那么这个客户 如果处理了 就不是最优解了,因为 在它前面被安排下的 都是钱数比它大的 而它自己的最大忍耐时间却到了,又不能往后走,往前走又没有位置,所以最后 对dp[0] - dp[t] 求和一下 就是最优解
代码二
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<string>
#include<sstream>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#include<numeric>
using namespace std;
const int MAX = 0x3f3f3f3f;
const int MIN = 0xc0c0c0c0;
const int maxn = 1e4 + 5;
struct node
{
int v, t;
}q[maxn];
int dp[50];
int comp(node x, node y)
{
if (x.v == y.v)
return x.t < y.t;
return x.v > y.v;
}
int main()
{
int n, t;
cin >> n >> t;
memset(dp, 0, sizeof(dp));
int i, j;
for (i = 0; i < n; i++)
scanf("%d%d", &q[i].v, &q[i].t);
sort(q, q+n, comp);
int tot = 0;
for (i = 0; i < n; i++)
{
for (j = q[i].t; j >= 0; j--)
{
if (dp[j] == 0)
{
dp[j] = q[i].v;
tot += dp[j];
break;
}
}
}
cout << tot << endl;
}
Kattis - bank 【简单DP】的更多相关文章
- HDU 1087 简单dp,求递增子序列使和最大
Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
- Codeforces Round #260 (Div. 1) A. Boredom (简单dp)
题目链接:http://codeforces.com/problemset/problem/455/A 给你n个数,要是其中取一个大小为x的数,那x+1和x-1都不能取了,问你最后取完最大的和是多少. ...
- codeforces Gym 100500H A. Potion of Immortality 简单DP
Problem H. ICPC QuestTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100500/a ...
- 简单dp --- HDU1248寒冰王座
题目链接 这道题也是简单dp里面的一种经典类型,递推式就是dp[i] = min(dp[i-150], dp[i-200], dp[i-350]) 代码如下: #include<iostream ...
- poj2385 简单DP
J - 简单dp Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:65536KB 64bit ...
- hdu1087 简单DP
I - 简单dp 例题扩展 Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:32768KB ...
- poj 1157 LITTLE SHOP_简单dp
题意:给你n种花,m个盆,花盆是有顺序的,每种花只能插一个花盘i,下一种花的只能插i<j的花盘,现在给出价值,求最大价值 简单dp #include <iostream> #incl ...
- hdu 2471 简单DP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2571 简单dp, dp[n][m] +=( dp[n-1][m],dp[n][m-1],d[i][k ...
- Codeforces 41D Pawn 简单dp
题目链接:点击打开链接 给定n*m 的矩阵 常数k 以下一个n*m的矩阵,每一个位置由 0-9的一个整数表示 问: 从最后一行開始向上走到第一行使得路径上的和 % (k+1) == 0 每一个格子仅仅 ...
随机推荐
- 理解Loadrunner中的Browser Emulation Simulate
案例 测试环境描述: 客户端 5台 Windows2000机器.服务器端 20台机器 一台F5(负载均衡设备,提供一个唯一的IP供客户端访问) 客户端绑定Host后,使用域名http://www.* ...
- CSS之各种居中
本博客讨论居中情况设定为 总宽度不定,内容宽度不定 的情况.(改变大小时,仍然居中). 特别说明:在元素设置 position:absolute; 来设置居中效果时,除去博客下介绍的css3方法外,还 ...
- 怎么隐藏MathType标尺
因为MathType公式编辑能力非常的好用,所以非常的受大家的欢迎.MathType用现有的模板可以直接输入输出各种公式,而且MathType中有着各式各样的数学符号满足了大家日常公式的需求,为大家的 ...
- jQuery返回顶部实用插件YesTop
只需一句jQuery代码实现返回顶部效果体验:http://hovertree.com/texiao/yestop/ 使用方法:只需引用jQuery库和YesTop插件(jquery.yestop.j ...
- 使用Servlet3.0新特性asyncSupported=true时抛异常java.lang.IllegalStateException: Not supported
最近在运用Servlet3.0新特性:异步处理功能的时候出现以下了2个问题: 运行时会抛出以下两种异常: 一月 19, 2014 3:07:07 下午 org.apache.catalina.core ...
- Oracle的存储过程编程
是一个可以用编程的方式来操作SQL的集合. | |目录 1什么是存储过程? 2存储过程的优点? 3存储过程的缺点? 4存储过程的用途? 5存储过程注意事项? 6如何写存储过程? 7如何执行存储过程? ...
- Rsync文件同步机备份工具使用
一,Rsync简介 Rsync是一款开源的,快速的,多功能的,可实现全量及增量的本地或远程数据同步的优秀工具.适用于多种操作平台. 全称是Remote synchronization 具有可使本地和远 ...
- Elastic Load Balancing with Sticky Sessions
Elastic Load Balancing with Sticky Sessions — Shlomo Swidler https://shlomoswidler.com/2010/04/elast ...
- closure--- 闭包与并行运算
闭包有效的减少了函数所需定义的参数数目.这对于并行运算来说有重要的意义.在并行运算的环境下,我们可以让每台电脑负责一个函数,然后将一台电脑的输出和下一台电脑的输入串联起来.最终,我们像流水线一样工 ...
- Yii框架2.0的Gii
Yii框架的Gii在我看来算是个快速创建器,当然对于学习来说意义不大,但对于已经懂得他的原理并用他开发的话,就是个快速开发的好工具. 他能快速的创建控制器,模块,crup,插件,Module. 打开g ...