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. UVa 11178:Morley’s Theorem(两射线交点)

    Problem DMorley’s TheoremInput: Standard Input Output: Standard Output Morley’s theorem states that ...

  2. Python爬虫(七)

    源码: import requests import re from my_mysql import MysqlConnect # 获取详情页链接和电影名称 def get_urls(page): u ...

  3. Spring Security OAuth2 源码分析

    Spring Security OAuth2 主要两部分功能:1.生成token,2.验证token,最大概的流程进行了一次梳理 1.Server端生成token (post /oauth/token ...

  4. document.selection window.getSelection()

    IE9以下支持:document.selection  IE9.Firefox.Safari.Chrome和Opera支持:window.getSelection() 屏幕取词 function ge ...

  5. Struts2的OGNL的用法

    1 <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1 ...

  6. 如何做rom,体验做rom过程,附图文教程,感谢各位romer

    http://bbs.gfan.com/android-5408130-1-1.html 有人问我,我简单的写一下,来源XDA,运行环境ubuntu 10.4. ubuntu安装很简单,在window ...

  7. python基础-第十一篇-11.1JavaScript基础

    JavaScript是一门解释型编程语言,主要是增强html页面的动态效果 JavaScript是有三部分组成:ECMAScript.BOM.DOM 单行注释//   多行/*   */(必须是scr ...

  8. 剑指Offer——扑克牌顺子

    题目描述: LL今天心情特别好,因为他去买了一副扑克牌,发现里面居然有2个大王,2个小王(一副牌原本是54张^_^)...他随机从中抽出了5张牌,想测测自己的手气,看看能不能抽到顺子,如果抽到的话,他 ...

  9. Qt JSON解析生成笔记(把JSON转成一个类对象)

    对于这样一段json { "name": "布衣食", "gender": "Male", "age" ...

  10. JAVA—List集合总结

    List接口总结: List接口是Collection接口的子接口,从其名称可以看出,是一个元素有序(并不是按大小排序,具有顺序索引,类似于数组),默认按照元素的添加顺序设置元素的索引,List和Se ...