uva-10026-贪心
题意:有N项工作,每项工作完成需要n天,如果不开始做每天罚fee,开始做即不罚钱,求任务的执行顺序,使得罚钱最少.如果有多组答案,取下标排列最小的那组
解题思路:
考虑工作tn(dn,fn) ,
假如先做工作tj, 0<j<n,0<i<n,那么罚的钱是 dj*f0+dj*f1+.....dj*fn+dj*fi = totalj1,
在做工作ti, di*f0+di*f1+....di*fn = totalj2, totalj1 + totalj2 = (di+dj)*fo + (di+dj)*f1 +....(di+dj)fn + (dj*fi)
假如先做工作ti,0<i<n,那么罚的钱是 di*f0+di*f1+....di*fn+di*fj = totali1,
再做tj,dj*f0+dj*f1+.....dj*fn=totali2, totali1+totali2 = (di+dj)*fo + (di+dj)*f1 +....(di+dj)fn +(di*fj)
差别就是di*fj和dj*fi
#include <algorithm>
#include <iostream> namespace cc
{
using std::cin;
using std::cout;
using std::endl;
using std::move;
using std::qsort;
const int N = ;
class Node
{
public:
int fee;
int day;
int index;
Node(int fee, int day, int index) : day(day), fee(fee), index(index)
{
}
Node() = default;
}; int cmp(const void *a, const void *b)
{
Node *n1 = (Node *)(a);
Node *n2 = (Node *)(b);
int total1 = n1->fee * n2->day;
int total2 = n2->fee * n1->day;
if (total1 == total2)
{
return n1->index - n2->index;
}
if (total1 < total2)
return ;
return -;
} void solve()
{
int n;
cin >> n;
int t = ;
while (n--)
{
if(t!=)
cout<<endl;
++t;
int k;
cin >> k;
int fee, day;
int total = ;
Node nodes[N];
for (int i = ; i < k; i++)
{
cin >> day >> fee;
Node node(fee, day, i + );
nodes[total++] = node;
}
qsort(nodes, total, sizeof(Node), cmp); cout << nodes[].index;
for (int i = ; i < total; i++)
cout << " " << nodes[i].index;
cout << endl;
}
} } // namespace cc int main()
{
#ifndef ONLINE_JUDGE
freopen("/Users/caicai/in", "r", stdin);
#endif // !ONLINE_JUDGE cc::solve();
return ;
}
uva-10026-贪心的更多相关文章
- UVA 10026 Shoemaker's Problem 鞋匠的难题 贪心+排序
题意:鞋匠一口气接到了不少生意,但是做鞋需要时间,鞋匠只能一双一双地做,根据协议每笔生意如果拖延了要罚钱. 给出每笔生意需要的天数和每天的罚钱数,求出最小罚钱的排列顺序. 只要按罚款/天数去从大到小排 ...
- (贪心5.2.1)UVA 10026 Shoemaker's Problem(利用数据有序化来进行贪心选择)
/* * UVA_10026.cpp * * Created on: 2013年10月10日 * Author: Administrator */ #include <iostream> ...
- uva 10026 Shoemaker's Problem _贪心
题意:鞋匠现在有n个工作要做,每个工作要x天,没延迟一天需要付款y,鞋匠每天只能做一个工作,问使得鞋匠最少赔款的工作顺序. 思路:工作和工作之间排序,如果a.value*b.day>b.valu ...
- 01_传说中的车(Fabled Rooks UVa 11134 贪心问题)
问题来源:刘汝佳<算法竞赛入门经典--训练指南> P81: 问题描述:你的任务是在n*n(1<=n<=5000)的棋盘上放n辆车,使得任意两辆车不相互攻击,且第i辆车在一个给定 ...
- UVA 11389(贪心问题)
UVA 11389 Time Limit:1000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Description II ...
- uva 10154 贪心+dp
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- uva 10026 Shoemaker's Problem
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- UVa 11389 (贪心) The Bus Driver Problem
题意: 有司机,下午路线,晚上路线各n个.给每个司机恰好分配一个下午路线和晚上路线. 给出行驶每条路线的时间,如果司机开车时间超过d,则要付加班费d×r. 问如何分配路线才能使加班费最少. 分析: 感 ...
- UVa 1467 (贪心+暴力) Installations
题意: 一共有n项服务,每项服务有安装的时间s和截止时间d.对于每项任务,如果有一项超出截止时间,惩罚值为所超出时间的长度.问如何安装才能使惩罚值最大的两个任务的惩罚值之和最小. 分析: 如果是求总惩 ...
- uva 10026 Problem C: Edit Step Ladders
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
随机推荐
- shell 查看去掉windons中的换行符
查看 cat -v 1.sh 替换 sed -i 's/\r//g' 1.sh
- 固态硬盘SSD,机械硬盘HDD,4K速度对比。
HDD - SSD -
- 禁用触发器的N种方法
一.禁用和启用单个触发器 禁用: ALTER TABLE trig_example DISABLE TRIGGER trig1 GO 恢复: ALTER TABLE trig_example EN ...
- C++进阶--类的继承
//############################################################################ /* * 公有,保护,私有继承 */ cl ...
- Netty Tutorial Part 1: Introduction to Netty [z]
Netty Tutorial, Part 1: Introduction to Netty Update: Part 1.5 Has Been Published: Netty Tutorial P ...
- uoj#209. 【UER #6】票数统计
http://uoj.ac/problem/209 当x!=y时,这个限制条件是确定的,可以枚举总通过数,用组合数计算,当x==y时,这个限制条件表示前x个全部通过或后x个全部通过,只有最大的x有用, ...
- spring4.3新注解之:@RequestMapping变种(@GetMapping,@PostMapping,@PutMapping,@DeleteMapping,@PatchMapping)
Spring 4.3 中引进了下面的注解 @RequestMapping 在方法层级的变种,来帮助简化常用 HTTP 方法的映射,并更好地表达被注解的方法的语义.比如,@GetMapping可以读作 ...
- 解析Javascript事件冒泡机制(转) 本文转自:http://blog.csdn.net/luanlouis/article/details/23927347
本文转自:http://blog.csdn.net/luanlouis/article/details/23927347 1. 事件 在浏览器客户端应用平台,基本生都是以事件驱动的,即某个事件发生,然 ...
- error while loading shared libraries: xxx.so
出现error while loading shared libraries: xxx.so错误,一是操作系统里确实没有包含该共享库(lib*.so.*文件)或者共享库版本不对,二是虽然存在,但是程序 ...
- centos6.5部署OpenStack单节点
环境 最小化安装的centos6.5 设置如下: 一.修改基本配置 1.修改主机名为controller [root@localhost ~]# hostname controller [root@l ...