日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

NYOJ 30 Gone Fishing(贪心)

發(fā)布時(shí)間:2025/3/16 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 NYOJ 30 Gone Fishing(贪心) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Gone Fishing

時(shí)間限制:3000?ms ?|? 內(nèi)存限制:65535?KB 難度:5 描述
John is going on a fishing trip. He has h hours available (1 <= h <= 16), and there are n lakes in the area (2 <= n <= 25) 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 from one lake to the next one, but he does not have to stop at any lake unless he wishes to. For each i = 1,...,n - 1, the number of 5-minute intervals it takes to travel from lake i to lake i + 1 is denoted ti (0 < ti <=192). For example, t3 = 4 means that it takes 20 minutes to travel from lake 3 to lake 4. To help plan his fishing trip, John has gathered some information about the lakes. For each lake i, the number of fish expected to be caught in the initial 5 minutes, denoted fi( fi >= 0 ), is known. Each 5 minutes of fishing decreases the number of fish expected to be caught in the next 5-minute interval by a constant rate of di (di >= 0). If the number of fish expected to be caught in an interval is less than or equal to di , there will be no more fish left in the lake in the next interval. To simplify the planning, John assumes that no one else will be fishing at the lakes to affect the number of fish he expects to catch.?
Write a program to help John plan his fishing trip to maximize the number of fish expected to be caught. The number of minutes spent at each lake must be a multiple of 5.?
輸入
You will be given a number of cases in the input. Each case starts with a line containing n. This is followed by a line containing h. Next, there is a line of n integers specifying fi (1 <= i <=n), then a line of n integers di (1 <=i <=n), and finally, a line of n - 1 integers ti (1 <=i <=n - 1). Input is terminated by a case in which n = 0.?
輸出
For each test case, print the number of minutes spent at each lake, separated by commas, for the plan achieving the maximum number of fish expected to be caught (you should print the entire plan on one line even if it exceeds 80 characters). This is followed by a line containing the number of fish expected.?
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.?
樣例輸入
2 1 10 1 2 5 2 4 4 10 15 20 17 0 3 4 3 1 2 3 4 4 10 15 50 30 0 3 4 3 1 2 3 0
樣例輸出
45, 5 Number of fish expected: 31 240, 0, 0, 0 Number of fish expected: 480 115, 10, 50, 35 Number of fish expected: 724

題意:John有h個(gè)小時(shí)的釣魚時(shí)間,一共有n個(gè)湖泊,它們?cè)谝粭l直線上。從第1個(gè)湖開始,可以在任意一個(gè)湖停下,每到達(dá)一個(gè)湖,這個(gè)湖開始有f[i]條魚,釣一次魚需要5分鐘,5分鐘后這個(gè)湖的魚的數(shù)量為上次數(shù)量減去d[i],并且從第i個(gè)湖到第i+1個(gè)湖需要ti*5的時(shí)間。如果某個(gè)時(shí)間段期望釣到的魚數(shù)小于或者等于di,那么下一個(gè)時(shí)間段湖中將不再有魚剩下。為了簡(jiǎn)化計(jì)劃,John假設(shè)沒有其他釣魚人影響到他的釣魚數(shù)目。目標(biāo)是讓他能釣到的魚最多。在每個(gè)湖他所花的時(shí)間必須是5分鐘的倍數(shù)。

分析:為了敘述方便,把釣5分鐘魚稱為釣一次魚。首先枚舉John需要走過(guò)的湖泊數(shù)X,也就是假設(shè)他從湖泊1走到湖泊X,在湖泊X停止釣魚,則路上花去的時(shí)間記為time[X]。在這個(gè)前提下,可以認(rèn)為John能從一個(gè)湖“瞬間轉(zhuǎn)移”到另一個(gè)湖,那么在任意一個(gè)時(shí)刻都可以從湖泊1到湖泊X中任選一個(gè)湖泊來(lái)釣魚。因此,采取貪心策略,每次選一個(gè)魚最多的湖泊來(lái)釣魚。對(duì)于每個(gè)湖泊而言,由于在任何時(shí)候魚的數(shù)量之和John在該湖釣魚的次數(shù)有關(guān),而和釣魚的總次數(shù)無(wú)關(guān),所以這個(gè)策略是最優(yōu)的。假設(shè)一共允許釣k次魚,那么每次在n個(gè)湖泊中選擇魚最多的一個(gè)湖泊釣,選擇每次釣魚地點(diǎn)的時(shí)間復(fù)雜度為O(n),總的時(shí)間復(fù)雜度為O(kn^2)。如果用優(yōu)先隊(duì)列來(lái)做,總的時(shí)間復(fù)雜度可以降為O(knlogn)。

不用優(yōu)先隊(duì)列的方法:

#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; const int N = 30; int f[N], d[N], ans[N], tmp[N], time[N]; int ff[N]; int main() {int n, h, i, j, t, cas = 0;while(~scanf("%d",&n) && n){scanf("%d",&h); h *= 60;for(i = 0; i < n; i++)scanf("%d",&f[i]);for(i = 0; i < n; i++)scanf("%d",&d[i]);time[0] = 0;for(i = 1; i < n; i++){scanf("%d",&t);time[i] = time[i-1] + t*5;}memset(ans, 0, sizeof(ans)); //注意初始化int maxsum = -1;for(i = 0; i < n; i++){int sum = 0, left_time = h - time[i];for(j = 0; j <= i; j++)ff[j] = f[j]; //注意不要直接對(duì)f[i]進(jìn)行操作,不然會(huì)影響下一次的結(jié)果memset(tmp, 0, sizeof(tmp));while(left_time > 0){int mmax = 0, id = 0;for(j = 0; j <= i; j++){if(ff[j] > mmax){mmax = ff[j];id = j;}}if(mmax == 0) break;sum += mmax;tmp[id] += 5;ff[id] -= d[id];left_time -= 5;}if(left_time > 0)tmp[0] += left_time;if(sum > maxsum){maxsum = sum;for(j = 0; j <= i; j++)ans[j] = tmp[j];}}if(cas > 0)printf("\n");printf("%d",ans[0]);for(i = 1; i < n; i++)printf(", %d",ans[i]);printf("\n");printf("Number of fish expected: %d\n",maxsum);cas++;}return 0; }
使用優(yōu)先隊(duì)列:

#include<stdio.h> #include<string.h> #include<queue> #include<algorithm> using namespace std;struct node {int id;int fish;int down;int time;friend bool operator < (const node &x, const node &y){if(x.fish != y.fish)return x.fish < y.fish;return x.id > y.id;} }a[30]; priority_queue<node> q; int h, n, ans[30], tmp[30], maxsum;void greedy() {int i, j;maxsum = -1;for(i = 0; i < n; i++){while(!q.empty()) q.pop();for(j = 0; j <= i; j++)q.push(a[j]);int left_time = h - a[i].time, sum = 0;memset(tmp, 0, sizeof(tmp));while(left_time > 0){node temp = q.top();q.pop();if(temp.fish <= 0) break;sum += temp.fish;temp.fish -= temp.down;tmp[temp.id] += 5;q.push(temp);left_time -= 5;}if(left_time > 0)tmp[0] += left_time;if(sum > maxsum){maxsum = sum;for(j = 0; j < n; j++)ans[j] = tmp[j];}} }int main() {int i, t, cas = 0;while(~scanf("%d",&n) && n){scanf("%d",&h);h *= 60;for(i = 0; i < n; i++){scanf("%d",&a[i].fish);a[i].id = i;}for(i = 0; i < n; i++)scanf("%d",&a[i].down);a[0].time = 0;for(i = 1; i < n; i++){scanf("%d",&t);a[i].time = a[i-1].time + t * 5;}if(cas > 0)printf("\n");cas++;greedy();printf("%d",ans[0]);for(i = 1; i < n; i++)printf(", %d",ans[i]);printf("\n");printf("Number of fish expected: %d\n",maxsum);}return 0; }

參考資料:http://blog.csdn.net/shuangde800/article/details/7828547

總結(jié)

以上是生活随笔為你收集整理的NYOJ 30 Gone Fishing(贪心)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。