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 tominimize 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 thenJv<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

解题思路:

首先按照截止时间的先后排序。对于任意两个任务a和b,如果a的截止时间在b之前,且a的加工时间比b长,那么接受了a订单必然要接受b订单。反过来呢,如果b的加工时间超过了截止时间,那么就找之前的订单,删掉加工时间最长的那个订单。这样接受的订单数没有变化,而总的加工时间变短了,为以后接受更多订单做准备。总要拒绝一些订单的,所以用优先队列维护q

代码:

#include <cstdio>
#include <queue>
#include <algorithm>
using namespace std;
const int MAX=;
int n,c;
struct node
{
int q,d;
friend bool operator<(node a,node b)
{
return a.q<b.q;
}
}a[MAX];
bool cmp(node a,node b)
{
return a.d<b.d;
}
void init()
{
scanf("%d",&n);
for(int i=;i<n;i++)
scanf("%d%d",&a[i].q,&a[i].d);
}
void slove()
{
priority_queue<node>Q;
sort(a,a+n,cmp);
int t=;c=;
for(int i=;i<n;i++)
{
t+=a[i].q;
c++;
Q.push(a[i]);
if(t>a[i].d)
{
t-=Q.top().q;
Q.pop();
c--;
}
} }
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
init();
slove();
printf("%d\n",c);
if(t) printf("\n");
}
return ;
}

高效算法——G - 贪心的更多相关文章

  1. 高效算法——E - 贪心-- 区间覆盖

    E - 贪心-- 区间覆盖 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=85904#problem/E 解题思路: 贪心思想, ...

  2. 高效算法——D 贪心,区间覆盖问题

    Given several segments of line (int the X axis) with coordinates [Li , Ri ]. You are to choose the m ...

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

    G - 贪心 Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Desc ...

  4. 深入N皇后问题的两个最高效算法的详解 分类: C/C++ 2014-11-08 17:22 117人阅读 评论(0) 收藏

    N皇后问题是一个经典的问题,在一个N*N的棋盘上放置N个皇后,每行一个并使其不能互相攻击(同一行.同一列.同一斜线上的皇后都会自动攻击). 一. 求解N皇后问题是算法中回溯法应用的一个经典案例 回溯算 ...

  5. CVPR2020论文介绍: 3D 目标检测高效算法

    CVPR2020论文介绍: 3D 目标检测高效算法 CVPR 2020: Structure Aware Single-Stage 3D Object Detection from Point Clo ...

  6. 高效算法——Most financial institutions 贪心 H

    H - 贪心 Crawling in process... Crawling failed Time Limit:3000MS     Memory Limit:0KB     64bit IO Fo ...

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

    Description   John Doe is a famous DJ and, therefore, has the problem of optimizing the placement of ...

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

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

  9. 集训第四周(高效算法设计)F题 (二分+贪心)

    Description   A set of n<tex2html_verbatim_mark> 1-dimensional items have to be packed in iden ...

随机推荐

  1. 19、XHTML

    XHTML 可扩展超文本标签语言(EXtensible HyperText Markup Language). 是一种 W3C 标准. 更严格,更纯净的HTML代码. 目标是取代HTML代码. XHT ...

  2. ES6数组去重

    今天五一,在出去玩之前赶紧写篇博客,时刻不要忘记学习^_^!! 提到数组去重,想必大家都不陌生,会的同学可能噼里啪啦写出好几个,下面来看看之前常见的去重代码: 'use strict'; var ar ...

  3. .NET Core跨平台开发

    对于.NET开源计划想必关注的人已经跃跃欲试了,但是真正将其用于开发的目前来说不多.毕竟截至本文发布时.NET Core才发布到1.0RC2版本.正式版预计还有一段时间.况且大多数人都是持观望态度,就 ...

  4. u盘复制提示文件过大

    应该有很多个朋友也遇到过同样的问题,就是我们的u盘的明明可用的空间还有很多,甚至一个空的16g的u盘,但从window等操作系统向u盘拷贝文件的时候,却不能容下诸如iso4g的镜像文件,难道是生产u盘 ...

  5. Orace数据库锁表的处理与总结<摘抄与总结一>

    TM锁(表级锁)类型共有5种,分别称为共享锁(S锁).排它锁(X锁).行级共享锁(RS锁).行级排它锁(RX锁).共享行级排它锁(SRX锁) 当Oracle执行DML语句时,系统自动在所要操作的表上申 ...

  6. Xcode的控制台调试命令

    XCode4.0以后,编译器换成了LLVM 编译器 2.0 与以前相比,更加强大:1.LLVM 编译器是下一带开源的编译技术.完全支持C, Objective-C, 和 C++.2.LLVM 速度比 ...

  7. Spring MVC中注解 @ModelAttribute

    1.@ModelAttribute放在方法之上,在当前Control内的所有方法映射多个URL的请求,都会执行该方法 @ModelAttribute public void itemsCommon(H ...

  8. 完美让IE兼容input placeholder属性的jquery实现

    调用时直接引用jquery与下面的js就行了,相对网上的大多数例子来说,这个是比较完美的方案. /* * 球到西山沟 * http://www.cnzj5u.com * 2014/11/26 12:1 ...

  9. clipboard.js IE下 不允许复制时, 问题修改

    问题描述:https://github.com/zenorocha/clipboard.js/wiki/Known-IssuesOn IE9-11 there's a prompt that asks ...

  10. JeeSite试用

    JeeSite主要定位于企业信息化领域.网址:http://www.oschina.net/p/jeesite 从描述来看,各种NB,下来看的最主要原因是最近还在更新,觉得有问题可以有一批人一起研究研 ...