题目连接: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. php远程获取图片或文件信息(get_headers,fsocketopen,curl)

    <?php if(!function_exists("remote_filesize")){ /** * 获取远程或本地文件信息 * @param string $strUr ...

  2. linux驱动移植的重要数据结构

    转载:http://www.embeddedlinux.org.cn/html/jishuzixun/201304/14-2538.html 对于嵌入式 Linux 系统来说,有各种体系结构的处理器和 ...

  3. MVC单元测试,使用Repository模式、Ninject、Moq

    本篇使用Repository设计MVC项目,使用Ninject作为DI容器,借助Moq进行单元测试. 模型和EF上下文 模型很简单: public class Foo { public int Id ...

  4. 【springboot+easypoi】一行代码搞定excel导入导出

    原文:https://www.jianshu.com/p/5d67fb720ece 开发中经常会遇到excel的处理,导入导出解析等等,java中比较流行的用poi,但是每次都要写大段工具类来搞定这事 ...

  5. 为 UITextField 增加键盘偏移的模板化写法

    .h代码 #import <UIKit/UIKit.h> @interface ViewController : UIViewController<UITextFieldDelega ...

  6. python笔记32-ddt框架优化(生成html报告注释内容传变量)

    前言 至于什么是ddt这个可以参考我之前写的博客内容,使用ddt框架的时候,有个问题困扰我很久了,一直没得到解决(也有很大小伙伴问过我,没解决抱歉了!) 这个问题就是:如何使用ddt框架时,生成的ht ...

  7. Nginx和Tomcat负载均衡实现session共享

    以前的项目使用Nginx作为反向代理实现了多个Tomcat的负载均衡,为了实现多个Tomcat之间的session共享,使用了开源的Memcached-Session-Manager框架. 此框架的优 ...

  8. Tomcat集群扩展session集中管理,Memcached-session-manager使用

    研究tomcat做负载均衡的时候如何实现ha,还有就是不采用session复制的方法做集群. 想到的是将session全部存储在后端的缓存服务器中. 正好网上有这么一个工具Memcached-sess ...

  9. iOS宏(自己使用,持续更新)

    // 直接从RGB取颜色(RGB 0xFF00FF)#define UICOLOR_FROM_RGB(rgbValue) \[UIColor colorWithRed:((float)((rgbVal ...

  10. Win8 下配置Java开发环境

    背景: 大学期间学习过一段时间的JavaEE.不算很熟悉. 后来学习并在工作中很多其它是iOS开发,iOS的水平属于中上. 对技术已经有一定熟知程度. 近期为了写一些东西,须要用到Java写后台. 流 ...