UVA 538 - Balancing Bank Accounts(贪心)
UVA 538 - Balancing Bank Accounts
题意:给定一些人的欠钱关系,要求在n-1次内还清钱,问方案
思路:贪心,处理出每一个人最后钱的状态,然后直接每一个人都和最后一个人操作就可以
代码:
- #include <cstdio>
- #include <cstring>
- #include <iostream>
- #include <string>
- #include <map>
- using namespace std;
- const int N = 25;
- int n, m, have[N];
- map<string, int> hash;
- string name[N];
- int main() {
- int cas = 0;
- while (~scanf("%d%d", &n, &m) && n || m) {
- hash.clear();
- memset(have, 0, sizeof(have));
- for (int i = 1; i <= n; i++) {
- cin >> name[i];
- hash[name[i]] = i;
- }
- string a, b; int val;
- while (m--) {
- cin >> a >> b >> val;
- int u = hash[a], v = hash[b];
- have[u] += val;
- have[v] -= val;
- }
- printf("Case #%d\n", ++cas);
- for (int i = 1; i < n; i++) {
- if (have[i] < 0)
- cout << name[i] << " " << name[n] << " " << -have[i] << endl;
- else if (have[i] > 0)
- cout << name[n] << " " << name[i] << " " << have[i] << endl;
- have[n] -= have[i];
- }
- printf("\n");
- }
- return 0;
- }
UVA 538 - Balancing Bank Accounts(贪心)的更多相关文章
- 【NOIP合并果子】uva 10954 add all【贪心】——yhx
Yup!! The problem name reects your task; just add a set of numbers. But you may feel yourselvesconde ...
- uva 11134 fabled rooks (贪心)——yhx
We would like to place n rooks, 1 n 5000, on a n nboard subject to the following restrictions• The i ...
- UVa 11134 (区间上的贪心) Fabled Rooks
这道题真是WA得我心力交瘁,好讨厌的感觉啊! 简直木有写题解的心情了 题意: n×n的棋盘里,放置n个车,使得任意两车不同行且不同列,且第i个车必须放在给定的第i个矩形范围内.输出一种方案,即每个车的 ...
- UVA 12382 Grid of Lamps 贪心
题目链接: C - Grid of Lamps Time Limit:1000MSMemory Limit: 0KB 问题描述 We have a grid of lamps. Some of the ...
- uva 993 Product of digits (贪心 + 分解因子)
Product of digits For a given non-negative integer number N , find the minimal natural Q such tha ...
- UVA 11729 - Commando War(贪心 相邻交换法)
Commando War There is a war and it doesn't look very promising for your country. Now it's time to ac ...
- uva 714 - Copying Books(贪心 最大值最小化 二分)
题目描写叙述开头一大堆屁话,我还细致看了半天..事实上就最后2句管用.意思就是给出n本书然后要分成k份,每份总页数的最大值要最小.问你分配方案,假设最小值同样情况下有多种分配方案,输出前面份数小的,就 ...
- UVA 10382 - Watering Grass【贪心+区间覆盖问题+高精度】
UVa 10382 - Watering Grass n sprinklers are installed in a horizontal strip of grass l meters long a ...
- UVA - 11636 Hello World! (贪心)
思路:复制次数最少并且可以部分复制,那么贪心地让当前尽量多的复制,如果最后一次复制会超过n,那就部分复制.即满足并且x尽量小. AC代码 #include <stdio.h> const ...
随机推荐
- Cocos2d-x串算出Size方法
项目需要,根据所输入的字符串,的需要计算串帐户Size. 包代码如下面.只需要传递一个字符串,您可以返回Size: Size ChartDemoScene::calculateFontSize(con ...
- java.lang.IllegalAccessError: class javax.activation.SecuritySupport12 cannot access its superclass
最近加入新的项目组,eclipse + tomcat7 + spring +ibatis + restful 遇到了这样的问题, 说是不能访问父类,我一开始以为是版本的原因,但是久经更改,错误依然,实 ...
- 正则匹配去掉字符串中的html标签
1.得到超链接中的链接地址: string matchString = @"<a[^>]+href=\s*(?:'(?<href>[^']+)'|"&quo ...
- hdu2089(数位dp)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2089 题意:求区间[a,b]内不含有62或4的数的个数. 分析:数位dp,dp[pos][0]表示到第 ...
- Java-UrlRewrite中文api文档
安装 1. 下载jar包, 并加入到WEB-INF/lib下 2. 在WEB-INF/web.xml中增加下面的配置 <filter> <filter-name>UrlRewr ...
- pygame写的弹力球
这是pygame写的弹力球 运行效果: ======================================================== 代码部分: ================= ...
- 理解Git的工作流程(转)
英文原文:Understanding the Git Workflow 如果你不理解Git的设计动机,那你就会处处碰壁.知道足够多的命令和参数后,你就会强行让Git按你想的来工作,而不是按Git自己的 ...
- 熬之滴水穿石:Spring--精简的J2EE(6)
48--曾用过的View 在Spring MVC架构中View实际上是有多种选择的.JSP是首选的view,实际上在J2E ...
- Dreamer 3.0 支持json、xml、文件上传
自己写的框架,功能类似Struts2.x 下载地址:http://pan.baidu.com/share/link?shareid=3273223286&uk=470382596 新增功能: ...
- Uva562(dp)
给我们n个硬币 每个硬币都有它的面值,要我我们分为两堆硬币,使得硬币的差值最小 我们可以dp计算出所有的差值,然后从小到大枚举差值,如果差值存在,就输出 dp[i][j] 表示对于前i件物品能达到差值 ...