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 ...
随机推荐
- Hive Metastore ObjectStore PersistenceManager自动关闭bug解析
最近在测试HCatalog,由于Hcatalog本身就是一个独立JAR包,虽然它也可以运行service,但是其实这个service就是metastore thrift server,我们在写基于Hc ...
- 实现ListView A~Z快速索引
ListView A~Z快速索引这种效果在通信录和城市列表中经常看到,方便用户查找,是一种增加用户体验的好方法. 实现步骤: 1.自定义一个名叫SlideBar 的View. 2.在布局文件中加入这个 ...
- 【Bug Fix】Error : Can't create table 'moshop_1.#sql-534_185' (errno: 150)
运行alter操作, alter table xx_shop_info add index FK9050F5D83304CDDC (shop_area), add constraint FK9050F ...
- Remote Desktop Organizer – 管理组织远程桌面 - 小众软件
http://www.appinn.com/remote-desktop-organizer/
- android中file的使用实例
File是android的4种存储方式的一种.File就是文件的意思一个文件,你无非是想进行读写操作.所以这就用到两个流.一个数输入流,一个是输出流.FileOutstream,和FileInputS ...
- 每天一个JavaScript实例-递归实现反转数组字符串
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- 使用AndroidFrameworks开发和应用隐藏类 or Android使用自定义framework开发与应用
Android眼下代表系统的开源手机操作系统已经更新到4.0.3版本号.由于其开源特性.使得操作系统本身所具有的最大的灵活性,但同时也引起的版本号的多样性,市场上出现的是手机厂商或ROM.可是怎样开发 ...
- dell N5010
Inspiron N5010Microsoft Windows 10 企业版 (64位) (英特尔)Intel(R) Core(TM) i3 CPU M 370 @ 2.40GHz(24 ...
- Maven, Ivy, Grape, Gradle, Buildr, SBT, Leiningen, ant
Maven, Ivy, Grape, Gradle, Buildr, SBT, Leiningen, ant
- HTML学习笔记之中的一个(input文件选择框的封装)
方式一:直接透明隐藏 .file_button_container,.file_button_container input {background: transparent url(./img/BT ...