programming-challenges Shoemaker's Problem (110405) 题解
Greedy.
证明:
Let's say we have job 1, 2, ..., n, and they have time and fine as t1, f1, t2, f2, ..., tn, fn
and they are in the order of t1/f1 <= t2/f2 <= t3/f3 <= ... <= tn/fn
So this is the objective schedule. Now we change 1 with m (1 < m <= n)
By the original order, we need pay fine as much as: F1 = t1 * (f2 + ... + fn) + t2 * (f3 + ... + fn) + ... + tm * (fm+1 + ... + fn) + R
By the new order, we need pay fine as much as: F2 = tm * (f1 + ... + fm-1 + fm+1 + ... + fn) + t1 * (f2 + ... + fm-1 + fm+1 + ... + fn) + ... + fm-1 * fm+1 + ... + fn) + R
F1 - F2 = (t1 + t2 + ... + tm-1) * fm - (tm * f1 + tm * f2 + ... + tm * fm-1)
As t1 * fm <= tm * f1, t2 * fm <= tm * f2, ..., tm-1 * fm <= tm * fm-1 F1 - F2 <= 0
So the original order is the best order.
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <assert.h>
#include <algorithm>
#include <math.h>
#include <ctime>
#include <functional>
#include <string.h>
#include <stdio.h>
#include <numeric>
#include <float.h> using namespace std; /*
4.6.5
*/ struct Rec {
int time, cost, id;
Rec(int atime, int acost, int aid) : time(atime), cost(acost), id(aid) {}
}; bool camp(Rec r1, Rec r2) {
return r1.time * r2.cost < r1.cost * r2.time;
} int main() {
int TC = 0; cin >> TC;
bool blank = false;
for (int tc = 1; tc <= TC; tc++) {
int num = 0; cin >> num;
vector<Rec> recs;
for (int i = 0; i < num; i++) {
int time, cost; cin >> time >> cost;
recs.push_back(Rec(time, cost, i + 1));
} stable_sort(recs.begin(), recs.end(), camp); if (blank) {
cout << endl;
}
blank = true;
for (int i = 0; i < recs.size(); i++) {
if (i > 0) cout << " ";
cout << recs[i].id;
}
cout << endl;
}
return 0;
}
programming-challenges Shoemaker's Problem (110405) 题解的更多相关文章
- UVA 10026 Shoemaker's Problem
Shoemaker's Problem Shoemaker has N jobs (orders from customers) which he must make. Shoemaker can w ...
- BZOJ2298:[HAOI2011]problem a——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=2298 https://www.luogu.org/problemnew/show/P2519 一次 ...
- 【Educational Codeforces Round 38 (Rated for Div. 2)】 Problem A-D 题解
[比赛链接] 点击打开链接 [题解] Problem A Word Correction[字符串] 不用多说了吧,字符串的基本操作 Problem B Run for your prize[贪心] ...
- UVA - 10239 The Book-shelver's Problem
Description Problem D The Book-shelver's Problem Input: standard input Output: standard output Time ...
- codeforces 459D - Pashmak and Parmida's problem【离散化+处理+逆序对】
题目:codeforces 459D - Pashmak and Parmida's problem 题意:给出n个数ai.然后定义f(l, r, x) 为ak = x,且l<=k<=r, ...
- 湘潭大学1185 Bob's Problem
Bob's Problem Accepted : 114 Submit : 589 Time Limit : 1000 MS Memory Limit : 65536 KB 题目描写叙述 Bo ...
- Codeforces Round #261 (Div. 2)459D. Pashmak and Parmida's problem(求逆序数对)
题目链接:http://codeforces.com/contest/459/problem/D D. Pashmak and Parmida's problem time limit per tes ...
- 洛谷P1919 【模板】A*B Problem升级版 题解(FFT的第一次实战)
洛谷P1919 [模板]A*B Problem升级版(FFT快速傅里叶) 刚学了FFT,我们来刷一道模板题. 题目描述 给定两个长度为 n 的两个十进制数,求它们的乘积. n<=100000 如 ...
- Lintcode399 Nuts & Bolts Problem solution 题解
[题目描述] Given a set of n nuts of different sizes and n bolts of different sizes. There is a one-one m ...
随机推荐
- android studio2.2 配置NDK
1.配置环境: Android studio2.2 配置NDK NDK版本[android-ndk-r13b-windows-x86_64.zip] NDK下载网址:[https://dl.googl ...
- Linux学习总结(15)——提高 Vim 和 Shell 效率的 9 个建议
你上一次使用 CAPSLOCK 键是什么时候?很久没有了对不对?噢,我也是,它已经被遗忘了,它浪费了键盘上一个黄金位置.让我们把它重映射成 Control 键来发挥它的作用吧!这里告诉了你在不同的操作 ...
- nodeJS npm grunt grunt-cli
1.安装好nodeJS后 ,一般都会把npm也安装好的.nodeJs集成npm的,可通过在cmd 分别运行 node -v和 npm -v来查看他们的版本,假设显示可说明可继续以下的操作 2.想安装g ...
- [jzoj 5178] [NOIP2017提高组模拟6.28] So many prefix? 解题报告(KMP+DP)
题目链接: https://jzoj.net/senior/#main/show/5178 题目: 题解: 我们定义$f[pos]$表示以位置pos为后缀的字符串对答案的贡献,答案就是$\sum_{i ...
- .net中MVC Webapi多参数调用控制器方法
http://blog.csdn.net/wulex/article/details/71545471 路由 public static void Register(HttpConfiguration ...
- SSM博客实战(9)-拦截器验证权限和登录与注销的实现
转载 https://liuyanzhao.com/6300.html
- LeetCode(10)Regular Expression Matching
题目如下: Python代码: # -*- coding:utf-8 -*- def ismatch(s,p): #先将dp[s+1][p+1]二维数组全置为False dp = [[False] * ...
- js产生随机数的几个方法
1.Math.random(); 结果为0-1间的一个随机数(包括0,不包括1) 2.Math.floor(num); 参数num为一个数值,函数结果为num的整数部分. 3.Math.round(n ...
- Spring Framework 开发参考手册中文(在线HTML)
https://blog.csdn.net/zfrong/article/details/3971722
- select选中值,传this
<select onChange = "a(this)"></select> function a(obj){ $(obj).find("opti ...