uva-757-贪心
题意:有个人要去湖里钓鱼,总共有N个湖,排成一个序列,用字母P表示湖,从湖pi 到 pi+1(下一个湖)需要ti个五分钟.
并且,每个湖里可钓出来的鱼每过五分钟就减少di.如果产出的鱼小于等于di.那么下个五分钟这个湖再也不会产出鱼.
问:使得总的钓出鱼数目最大和每个湖花费的对应时间,.如果鱼数目最大存在多种解,让时间尽可能花费在前面的湖上.
解法:
枚举每一个pi,划去从p0至pi的总time(总的转移时间).
这样我们就可以在任意p0-pi之间钓鱼而不用考虑time的问题.剩下的h全部花费在钓鱼上.
每次花费的五分钟都花在产出鱼数目最大的湖上.这样得到总鱼数目总是最大的.如果所有的鱼都钓完还有时间剩余.全部加在第一个湖上面.
#include <string>
#include<iostream>
#include<map>
#include<memory.h>
#include<vector>
#include<algorithm>
#include<queue>
#include<vector>
#include<stack>
#include<math.h>
#include<iomanip>
#include<bitset>
#include"math.h"
namespace cc
{
using std::cout;
using std::endl;
using std::cin;
using std::map;
using std::vector;
using std::string;
using std::sort;
using std::priority_queue;
using std::greater;
using std::vector;
using std::swap;
using std::stack;
using std::queue;
using std::bitset; class State
{
public: int curFish;
int d;
int time;
int index;
State() {}
State(int curFish, int d, int time, int index) :
curFish(curFish), d(d), time(time), index(index)
{ }
friend bool operator < (const State& s1, const State& s2)
{
if (s1.curFish != s2.curFish)
return s1.curFish < s2.curFish;
return s1.index > s2.index;
}
}; constexpr int N = ;
int H = ;
int n; int totalFish;
int per[N] = { };
State states[N]; priority_queue < State>q; void solve()
{
int t = ;
while (cin >> n && n)
{
if (t != )
cout << endl;
++t;
cin >> H;
H = H * ;
int f = ;
for (int i = ;i < n;i++)
{
cin >> f;
State state(f, , , i);
states[i] = state;
}
for (int i = ;i < n;i++)
{
cin >> states[i].d;
}
for (int i = ;i < n;i++)
{
cin >> states[i].time;
states[i].time = states[i].time + states[i-].time;
}
totalFish = -;
memset(per, , sizeof(per));
for (int i = ;i < n;i++)
{
while (q.empty() == false)
q.pop();
for (int k = ;k <= i;k++)
q.push(states[k]);
int h = H;
int curTotalFish = ;
int times[N] = {};
h = h - states[i].time;
while (h > )
{
State s = q.top();
q.pop();
int f = s.curFish;
if (f <= )break;
--h;
s.curFish -= s.d;
curTotalFish += f;
times[s.index]++;
q.push(s);
}
if (h > )
times[] += h;
if (curTotalFish > totalFish)
{
totalFish = curTotalFish;
memcpy(per, times, sizeof(times));
}
}
cout << per[] * ;
for (int i=;i<n;i++)
cout<<", " << per[i] * ;
cout << endl;
cout << "Number of fish expected: " << totalFish << endl;
}
}
}; int main()
{ #ifndef ONLINE_JUDGE
freopen("d://1.text", "r", stdin);
#endif // !ONLINE_JUDGE
cc::solve(); return ;
}
uva-757-贪心的更多相关文章
- 01_传说中的车(Fabled Rooks UVa 11134 贪心问题)
问题来源:刘汝佳<算法竞赛入门经典--训练指南> P81: 问题描述:你的任务是在n*n(1<=n<=5000)的棋盘上放n辆车,使得任意两辆车不相互攻击,且第i辆车在一个给定 ...
- UVA 11389(贪心问题)
UVA 11389 Time Limit:1000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Description II ...
- uva 10154 贪心+dp
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- uva 757
贪心 优先队列 #include <cstdio> #include <cstdlib> #include <cmath> #include <map> ...
- UVa 11389 (贪心) The Bus Driver Problem
题意: 有司机,下午路线,晚上路线各n个.给每个司机恰好分配一个下午路线和晚上路线. 给出行驶每条路线的时间,如果司机开车时间超过d,则要付加班费d×r. 问如何分配路线才能使加班费最少. 分析: 感 ...
- UVa 1467 (贪心+暴力) Installations
题意: 一共有n项服务,每项服务有安装的时间s和截止时间d.对于每项任务,如果有一项超出截止时间,惩罚值为所超出时间的长度.问如何安装才能使惩罚值最大的两个任务的惩罚值之和最小. 分析: 如果是求总惩 ...
- UVa 757 - Gone Fishing
题目大意:John有h的时间可以去钓鱼,有n湖可钓鱼,以5分钟为时间单位,每个湖初始每个单位时间可钓f条鱼,每下一个时间单位数量减少d条.同时,John只能从1号湖往后走进行钓鱼,湖之间的距离需要t个 ...
- Party Games UVA - 1610 贪心
题目:题目链接 思路:排序后处理到第一个不同的字符,贪心一下就可以了 AC代码: #include <iostream> #include <cstdio> #include ...
- UVa 1149 (贪心) Bin Packing
首先对物品按重量从小到大排序排序. 因为每个背包最多装两个物品,所以直觉上是最轻的和最重的放一起最节省空间. 考虑最轻的物品i和最重的物品j,如果ij可以放在一个包里那就放在一起. 否则的话,j只能自 ...
- UVA 10037 贪心算法
题目链接:http://acm.hust.edu.cn/vjudge/contest/122829#problem/A 题目大意:N个人夜里过河,总共只有一盏灯,每次最多过两个人,然后需要有人将灯送回 ...
随机推荐
- c语言设计实验报告
试验项目:1.字符与ASCLL码 2.运算符与表达式的运用 3.顺序结构应用程序 4.数学函数的算法描述 5.鸡兔同笼的算法描述 6.确定坐标的算法描述 姓名:熊承启 实验地点:514实验室 1.字符 ...
- 小妖精的完美游戏教室——东方PROJECT,同人,符卡系统
//================================================================//// Copyright (C) 东方同人社// All Rig ...
- 【步步为营 Entity Framework+Reporting service开发】-(2) Code Fir
也许有人问,为什么要用EF创建爱你数据表,code first好处是什么? 使用EF创建数据库/表,只需要设计简单的C#类,再表内容变化的时候他会自动更新数据库结构,并且保留原有数据. EF很强大,支 ...
- HOMEWORD2
开发工具和语言选择 语言 :pyhton3.6 工具 我选择的是 pycharm2019.1.由于之前已经安装好了,这里就不再贴出. 自动单元测试练习 python单元测试框架Unittest.Uni ...
- Windows登录后不记得密码
(不用输入原密码的方式修改用户的密码) 1 命令行输入命令:mmc #进入到控制台 2 点击左上角的文件,选择添加/删除管理单元 3 选择本地用户和组管理单元,添加到本地计算机,完成,确定 4 添加 ...
- ARM Cortex M0 程序映像和启动流程
- node搭建简易的websocket服务
http协议单向请求,只能客户端向服务器发送消息,然而websocket一旦双方建立连接就可以双方通信,更加深层次的用法是websocket可以做基础,然后不同的客户端可以通过websocket连接可 ...
- IMDG
将内存作为首要存储介质不是什么新鲜事儿,在对主存的使用上,内存数据网格(In Memory Data Grid,IMDG)与IMDB类似,但二者在架构上完全不同.IMDG特性可以总结为以下几点: 数据 ...
- 关于FIFO异步复位的问题
关于FIFO异步复位的问题 FIFO异步复位的宽度,需要保证至少3个较慢时钟的时钟周期长度. 怎样对一个脉冲加宽呢? `timescale 1ns / 1ps //////////////////// ...
- 【申嵌视频】5-1 ubuntu下安装VMWare Tools工具
[申嵌视频]5-1 ubuntu下安装VMWare Tools工具 适合搭建mini2440, Tiny6410, smart210,Tiny4412, NanoPC-T2, NanoPC-T3, N ...