UVA 757 Gone Fishing

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=9&problem=698&mosmsg=Submission+received+with+ID+19496827

利用枚举和贪心来计算。枚举在每个湖stop fishing的情况,在每种情况下,贪心算法得出能钓得最多鱼的数目。因为题目要求:If multiple plans exist, choose the one that spends as long as possible at lake 1, even if no fish are expected to be caught in some intervals. If there is still a tie, choose the one that spends as long as possible at lake 2, and so on. Insert a blank line between cases. 所以在贪心算法中,如果有多个湖可钓的鱼的数目相同,选最先遇到的那个湖。不要觉得这样就可以了。当一种情况结束之后,如果可以得到的鱼的总数有相同的,还要继续比较两种情况下,从第一个湖开始,选择在lake 1停留之间长的情况。代码如下:

  1 //============================================================================
  2 // Name        : test.cpp
  3 // Author      :
  4 // Version     :
  5 // Copyright   : Your copyright notice
  6 // Description : Hello World in C++, Ansi-style
  7 //============================================================================
  8
  9 #include <iostream>
 10 #include <math.h>
 11 #include <stdio.h>
 12 #include <cstdio>
 13 #include <algorithm>
 14 #include <string.h>
 15 #include <cstring>
 16 #include <queue>
 17 #include <vector>
 18 #include <functional>
 19 #include <cmath>
 20 #define SCF(a) scanf("%d", &a)
 21 #define IN(a) cin>>a
 22 #define FOR(i, a, b) for(int i=a;i<b;i++)
 23 typedef long long Int;
 24 using namespace std;
 25
 26 int main()
 27 {
 28     int n, h; //n lakes, h hours = h*12 "5 minutes"
 29     int fish[30]; //initial fish number
 30     int decrease[30]; //decrese unit
 31     int time[30]; //walk time
 32     int tcase = 0;
 33     int stay[30]; //time that stay at lake i
 34     int take[30]; //time to take for people walk from lake 0 to lake i;
 35     while (SCF(n) && n)
 36     {
 37         if (tcase >= 1)
 38             printf("\n");
 39         SCF(h);
 40         FOR(i, 0, n)
 41             SCF(fish[i]);
 42         FOR(i, 0, n)
 43             SCF(decrease[i]);
 44         FOR(i, 0, n - 1)
 45             SCF(time[i]);
 46         FOR(i, 0, n)
 47             stay[i] = 0;
 48
 49         take[0] = 0;
 50         FOR(i, 1, n)
 51             take[i] = take[i - 1] + time[i-1];
 52
 53         int maxFish = 0;
 54         int left[30]; //record available fish for each lake
 55         int cfish = 0; //total fish that can get for current strategy
 56         int cstay[30]; //record the time stay at each lake
 57         int ctime = h * 12;
 58
 59         FOR(i, 0, n) //fishing destination is lake i
 60         {
 61             ctime = h * 12;
 62             cfish = 0;
 63
 64             ctime -= take[i];
 65             FOR(j, 0, n)
 66             {
 67                 left[j] = fish[j];
 68                 cstay[j] = 0;
 69             }
 70
 71             while (ctime > 0)
 72             {
 73                 int cmax = 0;
 74                 FOR(j, 0, i+1)
 75                 {
 76                     if (left[j] > left[cmax])
 77                         cmax = j;
 78                 }
 79                 cfish += left[cmax];
 80                 left[cmax] = max(0, left[cmax] - decrease[cmax]);
 81                 cstay[cmax] += 5;
 82                 ctime -= 1;
 83             }
 84
 85             if (cfish > maxFish)
 86             {
 87                 maxFish = cfish;
 88                 FOR(j, 0, n)
 89                     stay[j] = cstay[j];
 90             }
 91             else if (cfish == maxFish)
 92             {
 93                 FOR(j, 0, n)
 94                 {
 95                     if (cstay[j] > stay[j])
 96                     {
 97                         FOR(z, j, n)
 98                             stay[z] = cstay[z];
 99                         break;
100                     }
101                     else if (cstay[j] < stay[j])
102                         break;
103                 }
104             }
105         }
106
107         printf("%d", stay[0]);
108         FOR(i, 1, n)
109             printf(", %d", stay[i]);
110         printf("\n");
111         printf("Number of fish expected: %d\n", maxFish);
112         tcase++;
113     }
114     return 0;
115 }

刚开始没有写91行else if的情况,以为在贪心算法中就已经考虑优先选择靠前的湖就可以了,结果一直wrong answer。加上之后才AC。

时间: 2024-11-12 06:48:33

UVA 757 Gone Fishing的相关文章

uva 757 Gone Fishing (贪心)

uva 757 Gone Fishing John is going on a fishing trip. He has h hours available ( ), and there are n lakes in the area ( ) all reachable along a single, one-way road. John starts at lake 1, but he can finish at any lake he wants. He can only travel fr

UVA题目分类

题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics 10300 - Ecological Premium 458 - The Decoder 494 - Kindergarten Counting Game 414 - Machined Surfaces 490 - Rotating Sentences 445 - Marvelous Mazes

优质题表(机密版)

转载请注明出处:http://www.cnblogs.com/dashuzhilin/p/4556803.html 思维题: poj 1528 poj 1597 poj 2538 poj 2608 poj 2612 poj 2361 poj 2339 poj 2664 uva 10894 uva 10921   uva 10922   uva 10929 uva 10931   uva 10800   uva 10878 uva 10976   uva 10323   uva 201 poj 2

UVA 562 Dividing coins --01背包的变形

01背包的变形. 先算出硬币面值的总和,然后此题变成求背包容量为V=sum/2时,能装的最多的硬币,然后将剩余的面值和它相减取一个绝对值就是最小的差值. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; #define N 50007 int c[102],d

UVA 10341 Solve It

Problem F Solve It Input: standard input Output: standard output Time Limit: 1 second Memory Limit: 32 MB Solve the equation: p*e-x + q*sin(x) + r*cos(x) + s*tan(x) + t*x2 + u = 0 where 0 <= x <= 1. Input Input consists of multiple test cases and te

UVA 11014 - Make a Crystal(容斥原理)

UVA 11014 - Make a Crystal 题目链接 题意:给定一个NxNxN的正方体,求出最多能选几个整数点.使得随意两点PQ不会使PQO共线. 思路:利用容斥原理,设f(k)为点(x, y, z)三点都为k的倍数的点的个数(要扣掉一个原点O).那么全部点就是f(1),之后要去除掉共线的,就是扣掉f(2), f(3), f(5)..f(n).n为素数.由于这些素数中包括了合数的情况,而且这些点必定与f(1)除去这些点以外的点共线,所以扣掉.可是扣掉后会扣掉一些反复的.比方f(6)在f

[UVa] Palindromes(401)

UVA - 401 Palindromes Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description A regular palindrome is a string of numbers or letters that is the same forward as backward. For example, the string "ABCDED

uva 401.Palindromes

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=342 题目意思:给出一段字符串(大写字母+数字组成).判断是否为回文串 or 镜像串 or 回文镜像串 or 什么都不是.每个字母的镜像表格如下 Character Reverse Character Reverse Character Reverse A A M M Y Y B

[2016-02-19][UVA][129][Krypton Factor]

UVA - 129 Krypton Factor Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Submit Status Description You have been employed by the organisers of a Super Krypton Factor Contest in which contestants have very high mental and physica