UVa11292
来源:互联网 发布:photoshop mac中文 编辑:程序博客网 时间:2024/05/24 07:29
题意:有一个恶龙有n个头,n个头在不同的高度,m个武士,武士只能砍到比它矮的头,而雇佣武士的费用就是武士的高度值,要求以最少的费用来杀到这条恶龙。
思路:在砍一个头时,总是选取比这个头高度大但是在武士身高中最小的。
代码 如下:
#include <iostream>#include <fstream>#include <vector>#include <algorithm>using namespace std;class Solution{public:void tragonOfLoowater(vector<int> dragons, vector<int> knights){sort(dragons.begin(), dragons.end());sort(knights.begin(), knights.end());int n = dragons.size(), m = knights.size();int cur = 0;int cost = 0;for (int i = 0; i < m; i++){if (knights[i] >= dragons[cur]){cur++;cost += knights[i];if (cur >= n) break;}}if (cur < n){cout << "Loowater is doomed!" << endl;}else{cout << cost << endl;}}};Solution solver;int main(){#ifndef ONLINE_JUDGEifstream fin("f:\\OJ\\uva_in.txt");streambuf *old = cin.rdbuf(fin.rdbuf());#endifint n, m;while (cin >> n >> m){if (n == 0 && m == 0) break;vector<int> dragons, knights;for (int i = 0; i < n; i++){int v;cin >> v;dragons.push_back(v);}for (int i = 0; i < m; i++){int v;cin >> v;knights.push_back(v);}solver.tragonOfLoowater(dragons, knights);}#ifndef ONLINE_JUDGEcin.rdbuf(old);#endifreturn 0;}
阅读全文
0 0
- UVA11292
- UVa11292
- UVa11292
- uva11292
- uva11292
- UVa11292
- Uva11292
- UVA11292
- UVa11292
- UVA11292
- UVa11292
- UVa11292
- UVA11292杀怪
- uva11292--贪心
- uva11292 Dragon of Loowater
- UVA11292:Dragon of Loowater
- UVa11292 Dragon of Loowater
- uva11292 - Dragon of Loowater
- 《第一行代码 Android》学习笔记1
- Tensorflow的变量管理
- 2012 NOI 第一试 随机数生成器
- MongoDB简介
- Unity 资源热更之AssetBundle
- UVa11292
- tar打包,及打包压缩
- gcc内嵌汇编详解
- 用python 读取txt文件并保存为array
- 树莓派远程USB摄像头
- 【C++】单链表面试题
- c语言: 回文数,5位数判断
- 无定位值的absolute定位
- iOS开发—使用GCD实现多线程开发—调度队列组