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】的更多相关文章

  1. HDU 1087 简单dp,求递增子序列使和最大

    Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  2. Codeforces Round #260 (Div. 1) A. Boredom (简单dp)

    题目链接:http://codeforces.com/problemset/problem/455/A 给你n个数,要是其中取一个大小为x的数,那x+1和x-1都不能取了,问你最后取完最大的和是多少. ...

  3. 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 ...

  4. 简单dp --- HDU1248寒冰王座

    题目链接 这道题也是简单dp里面的一种经典类型,递推式就是dp[i] = min(dp[i-150], dp[i-200], dp[i-350]) 代码如下: #include<iostream ...

  5. poj2385 简单DP

    J - 简单dp Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:65536KB     64bit ...

  6. hdu1087 简单DP

    I - 简单dp 例题扩展 Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:32768KB     ...

  7. poj 1157 LITTLE SHOP_简单dp

    题意:给你n种花,m个盆,花盆是有顺序的,每种花只能插一个花盘i,下一种花的只能插i<j的花盘,现在给出价值,求最大价值 简单dp #include <iostream> #incl ...

  8. 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 ...

  9. Codeforces 41D Pawn 简单dp

    题目链接:点击打开链接 给定n*m 的矩阵 常数k 以下一个n*m的矩阵,每一个位置由 0-9的一个整数表示 问: 从最后一行開始向上走到第一行使得路径上的和 % (k+1) == 0 每一个格子仅仅 ...

随机推荐

  1. AWS系列-添加购买的https证书

    1.1 自行购买证书 1.2 上传证书 打开EC2的负载均衡 选择相应的ALB 添加侦听器 选择https 端口443 选择目标组 证书类型 上传证书到IAM 证书名称填写申请证书时候的那个域名 私有 ...

  2. 剑指 offer set 22 数组中的逆序数

    总结 1. 题目为归并排序的变形, 不过我完全没想到 2. 在归并排序进行字符组 merge 时, 统计逆序数. merge 后, 两个子数组是有序的了, 下次再 merge 的时候就能以 o(n) ...

  3. C语言函数的概念

    在<我们对函数进行了简单的解释,函数(Function)是一段可以重复使用的代码,这是从整体上对函数的认识. C语言本身带了很多库函数,并分门别类地放在了不同的头文件中,使用时只要引入对应的头文 ...

  4. Excel 信息对比_数组版

    Sub LOOKUP_UChur() Dim i As Long '=== sourceWorksheet = 数据源表名称 Dim sourceWorksheet As Worksheet Dim ...

  5. Hibernate的调用数据库的存储过程

    Hibernate并没有给出直接调用数据库的存储过程的API,所以咋们就要通过调用原生的的connection对象来实现对存储过程的条用 Hibernate调用存储过程的步骤: 1:获得原生conne ...

  6. WingIDE6.0神秘代码

    python2: import string import random import sha BASE16 = '0123456789ABCDEF' BASE30 = '123456789ABCDE ...

  7. 学习LINQ必备条件

    转自:http://www.cnblogs.com/VolcanoCloud/p/4451302.html 学习LINQ必备条件:隐式类型局部变量:对象集合初始化器:委托:匿名函数:lambda表达式 ...

  8. mysql 字符集研究

    一.创建一个测试数据库 及一个测试用的表.均使用默认的编码方式. show variables like 'char%': mysql> show variables like 'char%'; ...

  9. 全球数字货币交易所TOP20安全性评级报告

      链塔智库2018-05-03 10:28 分析师:常昊.王婧雯    来源: 链塔智库 全球加密数字货币市值超2.5万亿元,单日交易额超2000亿元,全球超过3000万人已投入加密数字货币领域. ...

  10. tomcat启动报错:注释指定的bean类.与现有的冲突.相同的名称和类

    错误: Unexpected exception parsing XML document from ServletContext resource [/WEB-INF/business/config ...