UVa - 1153 - Keep the Customer Satisfied
来源:互联网 发布:鲁道夫·阿贝尔知乎 编辑:程序博客网 时间:2024/06/07 20:23
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 , the amount of steel required (in tons) and d , 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 tons of steel takes exactly q 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 . For a given order, Jj , qj denotes the amount of steel required and dj is the associated due date.
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 and J4 , accept all other orders and process them as follows.
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 of orders (n can be as large as 800000 for some test cases). It is followed by nlines. 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 ).
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
167 158 206 84 93 215 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 and Jv with qu > qvand du < dv , if Ju is accepted then Jv 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 alongI 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
用pair作为基本结构保存q和d,在用一个数组vector<pair<int, int> >来存储输入数据。然后按照等待时间 d 排序,注意用sort排序默认是用first,这里的的first是q,所以要重写一个比较函数,用second排序。
接下来就是贪心算法,用优先队列priority_queue<pair<int, int> > 存储结果,这里体现了用pair的优点,刚好优先队列中就是first高优先级高。
一个个入队,如果总时间超过了当前限制时间,则把队中花费时间最长的元素(就是q最大的)删掉。
AC代码:
#include <iostream>#include <cstdio>#include <cstdlib>#include <cctype>#include <cstring>#include <string>#include <sstream>#include <vector>#include <set>#include <map>#include <algorithm>#include <stack>#include <queue>#include <bitset> #include <cassert> #include <cmath>#include <functional>using namespace std;typedef pair<int, int> Pair;vector<Pair> state;int n, ans, tmp;// 默认Pair的排序是first,但是这里排序需要用d,就是second,所以要写一个比较函数bool cmp(const Pair &a, const Pair &b){return a.second < b.second;}void solve(){priority_queue<Pair> q; // 用优先队列,其中是按first比较大小的ans = tmp = 0; // 记得初始化。。。for (int i = 0; i < n; i++) { // 贪心求解tmp += state[i].first;q.push(state[i]);ans++;if (tmp > state[i].second) { // 如果超了当前的等待时间Pair t = q.top(); // 最大的是q最大的元素q.pop();ans--;tmp -= t.first;}}cout << ans << endl;}int main(){ios::sync_with_stdio(false);int T;cin >> T;while (T--) {state.clear();cin >> n;int q, d;for (int i = 0; i < n; i++) {cin >> q >> d;state.push_back(make_pair(q, d));}sort(state.begin(), state.end(), cmp); // 按照d的大小排序solve();if (T) {cout << endl;}}return 0;}
- uva uva 1153 - Keep the Customer Satisfied
- UVa - 1153 - Keep the Customer Satisfied
- UVA - 1153 Keep the Customer Satisfied
- UVA 1153 - Keep the Customer Satisfied
- UVA 1153(p255)----Keep the Customer Satisfied
- UVA 1153 Keep the Customer Satisfied
- uva 1153 - Keep the Customer Satisfied(贪心+优先队列)
- UVa 1153 Keep the Customer Satisfied 解题报告(贪心)
- UVa 1153 - Keep the Customer Satisfied (贪心 + 优先队列)
- UVa #1153 Keep the Customer Satisfied (习题8-12)
- UVa 1153 - Keep the Customer Satisfied(贪心)
- uva 1153—— Keep the Customer Satisfied
- UVA 1153 - Keep the Customer Satisfied(贪心)
- UVA 1153 Keep the Customer Satisfied(贪心2.0)
- Uva 1153 Keep the Customer Satisfied(贪心)
- UVa 1153 Keep the Customer Satisfied (贪心+优先队列)
- uva 1613 Keep the Customer Satisfied
- Keep the Customer Satisfied
- 使用U盘安装WIN7系统疑难问题总结
- Contains Duplicate II
- LaTeX练习
- C++——try、throw、catch实例学习程序
- LeetCode——Merge Sorted Array
- UVa - 1153 - Keep the Customer Satisfied
- 反射
- jsp页面 ajax实现多图上传
- Upload files by sending multipart request programmatically
- 网络编程
- 今天工作总结和学习总结
- android 事件处理机制之requestDisallowInterceptTouchEvent
- Asset Store 下载的package存放位置
- 集合2和JDK5性特性