题目连接:10670 - Work Reduction

题目大意:有tol的工作量,和要求达到的工作剩余量sur,然后是公司总数,对应每个公司提供两种服务,1、完成一个工作量,2.完成当前未完成工作量的一半(注意这里是tol的一半,不是tol - sur的一半), 当剩余工作量为奇数, 对模2四舍五入。现在给出每个公司的两种服务所需费用, 要求计算出每个公司单独完成工作量所花费的最少金额(剩余工作量必须为sur,输出按照金额大小,相同按照公司名字的字典序大小。

解题思路:贪心, 对于每个公司,比较当前单位工作量的花费金额,来决定选用哪种服务。

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int N = 105; struct company {
char name[N];
int one;
int half;
int value;
}tmp[N];
char str[N];
int tol, sur; bool cmp(const company &a, const company &b) {
if (a.value < b.value) return true;
else if (a.value > b.value) return false;
else
return strcmp(a.name, b.name) < 0;
} int count(int t, int h) {
int sum = tol, cur = 0;
while (sum != sur) {
int w = (sum + 1) / 2;
if (t * w > h && sum - w >= sur) {
sum -= w;
cur += h;
}
else {
sum--;
cur += t;
}
}
return cur;
} void handle(int cur) {
int len = strlen(str), cnt = 0, sex = 0;
for (int i = 0; i < len; i++) {
if (sex == 0 && str[i] != ':')
tmp[cur].name[cnt++] = str[i];
else if (str[i] == ':') {
tmp[cur].name[cnt++] = '\0';
sex++;
}
else if (sex == 1 && str[i] != ',')
tmp[cur].one = tmp[cur].one * 10 + str[i] - '0';
else if (str[i] == ',')
sex++;
else
tmp[cur].half = tmp[cur].half * 10 + str[i] - '0';
}
tmp[cur].value = count(tmp[cur].one, tmp[cur].half);
} int main() {
int cas, t = 1, n;
scanf("%d", &cas);
while (cas--) {
memset(tmp, 0, sizeof(tmp));
scanf("%d%d%d%*c", &tol, &sur, &n);
for (int i = 0; i < n; i++) {
gets(str);
handle(i);
} sort(tmp, tmp + n, cmp); printf("Case %d\n", t++);
for (int i = 0; i < n; i++)
printf("%s %d\n", tmp[i].name, tmp[i].value);
}
return 0;
}

uva 10670 Work Reduction(贪心)的更多相关文章

  1. 10670 Work Reduction (贪心 + 被题意坑了- -)y

    Problem C: Work Reduction Paperwork is beginning to pile up on your desk, and tensions at the workpl ...

  2. UVa 10670 - Work Reduction

    题目大意:对n份文件进行处理使其减少到m份,有l个机构可供选择.每个机构提供两种方案:每减少一份收费a元,或者减少到文件数量的一半收费b元.根据各个机构收取费用进行排序. 很直接的题目,直接进行模拟就 ...

  3. uva 1615 高速公路(贪心,区间问题)

    uva 1615 高速公路(贪心,区间问题) 给定平面上n个点和一个值D,要求在x轴上选出尽量少的点,使得对于给定的每个点,都有一个选出的点离它的欧几里得距离不超过D.(n<=1e5) 对于每个 ...

  4. UVA 10714 Ants 蚂蚁 贪心+模拟 水题

    题意:蚂蚁在木棍上爬,速度1cm/s,给出木棍长度和每只蚂蚁的位置,问蚂蚁全部下木棍的最长时间和最短时间. 模拟一下,发现其实灰常水的贪心... 不能直接求最大和最小的= =.只要求出每只蚂蚁都走长路 ...

  5. UVa 11729 - Commando War(贪心)

    "Waiting for orders we held in the wood, word from the front never came By evening the sound of ...

  6. UVA 10718 Bit Mask 贪心+位运算

    题意:给出一个数N,下限L上限U,在[L,U]里面找一个整数,使得N|M最大,且让M最小. 很明显用贪心,用位运算搞了半天,样例过了后还是WA,没考虑清楚... 然后网上翻到了一个人家位运算一句话解决 ...

  7. UVA 11039-Building designing【贪心+绝对值排序】

    UVA11039-Building designing Time limit: 3.000 seconds An architect wants to design a very high build ...

  8. UVA 12130 - Summits(BFS+贪心)

    UVA 12130 - Summits 题目链接 题意:给定一个h * w的图,每一个位置有一个值.如今要求出这个图上的峰顶有多少个.峰顶是这样定义的.有一个d值,假设一个位置是峰顶.那么它不能走到不 ...

  9. UVA - 11134 Fabled Rooks[贪心 问题分解]

    UVA - 11134 Fabled Rooks We would like to place n rooks, 1 ≤ n ≤ 5000, on a n × n board subject to t ...

随机推荐

  1. StringUtils类中 isEmpty() 与 isBlank()的区别

    org.apache.commons.lang.StringUtils类提供了String的常用操作,最为常用的判空有如下两种isEmpty(String str)和isBlank(String st ...

  2. oracle_创建表空间、用户、授权等步骤

    --创建表空间(在system用户下才可以创建) //connect system/manager as sysdba:切换到system用户 create tablespace tz datafil ...

  3. Solution for sending Whatsapp via sqlite "INSERT INTO"

    I use something similar but thought I'd mention this 'bug' that can happen:when you INSERT '%wa_mess ...

  4. Android 手机 ADB FastBoot 命令基本用法

    adb用法: 准备: 1.在电脑上安装相应的USB驱动,在各分区置顶帖子有下载链接 2.手机进入设置->开发人员选项->勾选USB调试 adb devices 查看是否有设备 adb sh ...

  5. systemtap 用户态调试2

    [root@localhost ~]# cat user.stpprobe process(@1).function(@2){print_ubacktrace();exit();} session 1 ...

  6. LINUX block I/O --systemtap

    http://hushi55.github.io/2015/10/16/Block-Input-Output/ http://myaut.github.io/dtrace-stap-book/kern ...

  7. C#中List<T>是怎么存放元素的

    Jeffrey Zhao在"你的字典里有多少元素?"一文中,提到了他在面试时问过的一个问题:List<T>是怎么存放元素?不幸的是,自己也回答不出来,只知道怎么用,却不 ...

  8. C语言 const, static, static const 的区别

    基本定义: const  就是只读的意思,只在声明中使用;static 一般有2个作用,规定作用域和存储方式. 对于局部变量, static规定其为静态存储方式, 每次调用的初始值为上一次调用的值,调 ...

  9. 建议:一般地,建议使用xcode 4.3开发app 而不是使用xcode4.5

    建议:一般地,建议使用xcode 4.3开发app 而不是使用xcode4.5 因为,xcode4.5 默认的sdk 是ios6.0,这里面有很多新特性.比如,Autolayout 等,这个特性再io ...

  10. python文本 字符串开头或者结尾匹配

    python文本 字符串开头或者结尾匹配 场景: 字符串开头或者结尾匹配,一般是使用在匹配文件类型或者url 一般使用startwith或者endwith >>> a='http:/ ...