G - 贪心

Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Submit Status

Description

 

Simon and Garfunkel Corporation (SG Corp.) is a large steel-making company with thousand of customers. Keeping the customer satisfied is one of the major objective of Paul and Art, the managers.

Customers issue orders that are characterized by two integer values q<tex2html_verbatim_mark> , the amount of steel required (in tons) and d<tex2html_verbatim_mark> , the due date (a calender date converted in seconds). The due date has to be met if SG Corp. accepts the order. Stated another way, when an order is accepted, the corresponding amount of steel has to be produced before its due date. Of course, the factory can process no more than one order at a time.

Although the manufacturing process is rather complex, it can be seen as a single production line with a constant throughput. In the following, we assume that producing q<tex2html_verbatim_mark> tons of steel takes exactly q<tex2html_verbatim_mark> seconds (i.e., throughput is 1). The factory runs on a monthly production plan. Before the beginning of the month, all customers' orders are collected and Paul and Art determine which of them are going to be accepted and which ones are to be rejected in the next production period. A production schedule is then designed. To keep customers satisfied, Paul and Art want to minimize the total number of orders that are rejected. In the following, we assume that the beginning of the next production plan (i.e., the first day of the next month) corresponds to date 0.

Hogdson and Moore have been appointed as Chief Scientific Officers and you are requested to help them to compute an optimal solution and to build a schedule of all accepted orders (starting time and completion time).

Small Example

Consider the following data set made of 6 orders J1,..., J6<tex2html_verbatim_mark> . For a given order, Jj<tex2html_verbatim_mark> , qj<tex2html_verbatim_mark> denotes the amount of steel required and dj<tex2html_verbatim_mark> is the associated due date.

Order qj<tex2html_verbatim_mark> dj<tex2html_verbatim_mark>
J1<tex2html_verbatim_mark> 6 8
J2<tex2html_verbatim_mark> 4 9
J3<tex2html_verbatim_mark> 7 15
J4<tex2html_verbatim_mark> 8 20
J5<tex2html_verbatim_mark> 3 21
J6<tex2html_verbatim_mark> 5 22

You can check by hand that all orders cannot be accepted and it's very unlikely you could find a solution with less than two rejected orders. Here is an optimal solution: Reject J1<tex2html_verbatim_mark> and J4<tex2html_verbatim_mark> , accept all other orders and process them as follows.

Accepted Order Starting Time Completion Time
J2<tex2html_verbatim_mark> 0 4
J3<tex2html_verbatim_mark> 4 11
J5<tex2html_verbatim_mark> 11 14
J6<tex2html_verbatim_mark> 14 19

Note that the production line is never idle.

Input

The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.

Data Each test case is described by one input file that contains all the relevant data: The first line contains the number n<tex2html_verbatim_mark> of orders ( n<tex2html_verbatim_mark> can be as large as 800000 for some test cases). It is followed by n<tex2html_verbatim_mark> lines. Each of which describes an order made of two integer values: the amount of steel (in tons) required for the order (lower than 1000) and its due date (in seconds; lower than 2 x 106<tex2html_verbatim_mark> ).

Output

For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.

You are required to compute an optimal solution and your program has to write the number of orders that are accepted.

Sample Input

1

6
7 15
8 20
6 8
4 9
3 21
5 22

Sample Output

4

Some Hints from Hogdson and Moore

  • Hogdson and Moore claim that it is optimal to sequence accepted orders in non-decreasing order of due dates.
  • They also claim that there is an optimal solution such that for any two orders Ju<tex2html_verbatim_mark> and Jv<tex2html_verbatim_mark> with qu > qv<tex2html_verbatim_mark> and du < dv<tex2html_verbatim_mark> , if Ju<tex2html_verbatim_mark> is accepted then Jv<tex2html_verbatim_mark> is also accepted.
  • Finally, Hogdson and Moore advise you to ``Keep the Customer Satisfied"

Keep the Customer Satisfied

Gee but it's great to be back home
Home is where I want to be.
I've been on the road so long my friend,
And if you came along
I know you couldn't disagree. It's the same old story
Everywhere I go,
I get slandered,
Libeled,
I hear words I never heard
In the bible
And I'm on step ahead of the shoe shine
Two steps away from the county line
Just trying to keep my customers satisfied,
Satisfied. Deputy sheriff said to me
Tell me what you come here for, boy.
You better get your bags and flee.
You're in trouble boy,
And you're heading into more.

©Simon & Garfunkel

题意,钢铁厂会接到一些订单,其中有一些无法完成,需要拒绝,他们希望需要拒绝的订单越少越好,要求输出需要拒绝的订单数

贪心,先排序,限期紧的放在前面,在小于当前订单日期限值之前,你当然是肆无忌惮得接下订单了,如果你在选择某一个订单时发现期限会超出,先不要忙着拒绝它,如果你之前选择的某个订单需求数(需求数越大,所耗时间也就越大)比它还大,那就忍痛拒绝那个大订单,反正要求是接下的订单数越多越好,业绩什么的也就不管啦.........赔本赚吆喝吧

还有,怎么找那个更需要拒绝的订单?遍历一遍数组?数组最长可达80万如果遍历时间复杂度为O(n^2),三秒绝对跑不完,所以使用优先队列记录q,快得妥妥的

#include"iostream"
#include"algorithm"
#include"queue"
using namespace std; const int maxn=800000+10; struct node
{
int q,d;
}a[maxn]; bool cmp(struct node a1,struct node a2)
{
return a1.d<a2.d;
} int n; void Init()
{
cin>>n;
for(int i=0;i<n;i++) cin>>a[i].q>>a[i].d;
} void Work()
{
sort(a,a+n,cmp);
int ans=n,cur=0;
priority_queue<int>que;
for(int i=0;i<n;i++)
{
cur+=a[i].q;
que.push(a[i].q);
if(cur>a[i].d)
{
cur-=que.top();
que.pop();
ans--;
}
}
cout<<ans<<endl;
} int main()
{
int T,t;
cin>>T;
t=T;
while(T--)
{
if(t!=T+1) cout<<endl;
Init();
Work();
}
return 0;
}

集训第四周(高效算法设计)G题 (贪心)的更多相关文章

  1. 『嗨威说』算法设计与分析 - 贪心算法思想小结(HDU 2088 Box of Bricks)

    本文索引目录: 一.贪心算法的基本思想以及个人理解 二.汽车加油问题的贪心选择性质 三.一道贪心算法题点拨升华贪心思想 四.结对编程情况 一.贪心算法的基本思想以及个人理解: 1.1 基本概念: 首先 ...

  2. 集训第四周(高效算法设计)A题 Ultra-QuickSort

    原题poj 2299:http://poj.org/problem?id=2299 题意,给你一个数组,去统计它们的逆序数,由于题目中说道数组最长可达五十万,那么O(n^2)的排序算法就不要再想了,归 ...

  3. 集训第四周(高效算法设计)P题 (构造题)

    Description   There are N<tex2html_verbatim_mark> marbles, which are labeled 1, 2,..., N<te ...

  4. 集训第四周(高效算法设计)O题 (构造题)

    A permutation on the integers from 1 to n is, simply put, a particular rearrangement of these intege ...

  5. 集训第四周(高效算法设计)N题 (二分查找优化题)

    原题:poj3061 题意:给你一个数s,再给出一个数组,要求你从中选出m个连续的数,m越小越好,且这m个数之和不小于s 这是一个二分查找优化题,那么区间是什么呢?当然是从1到数组长度了.比如数组长度 ...

  6. 集训第四周(高效算法设计)M题 (扫描法)

    原题:UVA11078 题意:给你一个数组,设a[],求一个m=a[i]-a[j],m越大越好,而且i必须小于j 怎么求?排序?要求i小于j呢.枚举?只能说超时无上限.所以遍历一遍数组,设第一个被减数 ...

  7. 集训第四周(高效算法设计)I题 (贪心)

    Description Shaass has n books. He wants to make a bookshelf for all his books. He wants the bookshe ...

  8. 集训第四周(高效算法设计)E题 (区间覆盖问题)

    UVA10382 :http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=21419 只能说这道题和D题是一模一样的,不过要进行转化, ...

  9. 集训第四周(高效算法设计)D题 (区间覆盖问题)

    原题 UVA10020  :http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=19688 经典的贪心问题,区间上贪心当然是右区间越 ...

随机推荐

  1. layui table 详细讲解

     layui.use('table', function () {             var table = layui.table;             /*第一种原始写法*/       ...

  2. JavaScript编程艺术-第6章(JavaScript美术馆改进版)代码

    基于[第4章(JavaScript美术馆)代码]进行改进(***HTML与JS分离***) (*亲测可用) HTML: JS: CSS:

  3. linux 磁盘 分区、格式化、挂载

    将容量结果易读的容量格式显示出来df -h 分区 初次接触仅分成两个分区(“/与Swap”)预留一个备用的剩余磁盘容量 磁盘分区 fdisk #df /找出磁盘文件名#fdisk /dev/hdc#m ...

  4. poj 3253 Fence Repair (水哈夫曼树)

    题目链接: http://poj.org/problem?id=3253 题目大意: 有一根木棍,需要截成n节,每节都有固定的长度,一根长度为x的木棒结成两段,需要花费为x,问截成需要的状态需要最小的 ...

  5. ACM配置指南

    Ubuntu桌面入门指南 ACM比赛系统ubuntu 使用指南 ACM核武器 简明 Vim 练级攻略 Vim命令合集 代码编辑神器VIM(附我写acm程序时的配置) my_vimrc in ubunt ...

  6. 题解报告:hdu 2544 最短路

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2544 Problem Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t ...

  7. C#菜鸟正则表达式一

    LZ菜鸟,仅整理笔记,顺带记录一下,谓之增加印象. LZ认为,没必要太纠结原理,模型, 屌丝能用就对了,剩下的事情用多了自然会去探索. 中文:正则表达式,英文:Regular  ExPression, ...

  8. 腾讯云COS对象存储的简单使用

    叮当哥之前买了一年的腾讯云服务器,昨日偶然发现腾讯云送了叮当哥半年的cos对象存储服务器,于是就撸起袖子传了几张珍藏的高清大图上去,现将其上传的简单使用步骤总结一波(其它操作参加官方SDK文档API) ...

  9. [BZOJ1878][SDOI2009]HH的项链 莫队

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1878 不带修改的莫队,用一个桶记录一下当前区间中每种颜色的数量就可以做到$O(1)$更新了 ...

  10. [SPOJ1811]Longest Common Substring 后缀自动机 最长公共子串

    题目链接:http://www.spoj.com/problems/LCS/ 题意如题目,求两个串的最大公共子串LCS. 首先对其中一个字符串A建立SAM,然后用另一个字符串B在上面跑. 用一个变量L ...