贪心 做题

P1223 排队接水

题目
题目描述
有 n 个人在一个水龙头前排队接水,假如每个人接水的时间为Ti,请编程找出这 n 个人排队的一种顺序,使得 n个人的平均等待时间最小。

输入格式
第一行为一个整数 n。
第二行 n 个整数,第 i 个整数 Ti
表示第 i个人的等待时间 Ti。
输出格式
输出文件有两行,第一行为一种平均时间最短的排队顺序;第二行为这种排列方案下的平均等待时间(输出结果精确到小数点后两位)。

输入输出样例
输入
10
56 12 1 99 1000 234 33 55 99 812
输出
3 2 7 8 1 4 9 6 10 5
291.90
说明/提示
10^6n≤1000,ti≤10的6次方
,不保证ti不重复。
当 ti重复时,按照输入顺序即可(sort 是可以的)
注意:排队打水的那个人就不算等待时间了

#include <bits/stdc++.h>

using namespace std;

int main(){
	int n,a[1010];
	long long sum=0;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		a[i]=a[i]*1001+i;//一种很妙的写法,即记录了排队时间,又记录了排队顺序 
	} 
	sort(a+1,a+n+1);
	for(int i=1;i<=n;i++){
		printf("%d ",a[i]%1001);
	}
	cout<<endl;
	for(int i=1;i<n;i++){
		sum+=a[i]/1001*(n-i);
	}
	printf("%.2lf\n",1.0*sum/n);
    return 0;
}

P1803 凌乱的yyy / 线段覆盖

题目
题目描述
现在各大 oj 上有 n 个比赛,每个比赛的开始、结束的时间点是知道的。

yyy 认为,参加越多的比赛,noip 就能考的越好(假的)。

所以,他想知道他最多能参加几个比赛。

由于 yyy 是蒟蒻,如果要参加一个比赛必须善始善终,而且不能同时参加 2个及以上的比赛。

输入格式
第一行是一个整数 n ,接下来 n 行每行是 2 个整数ai,bi(ai<bi),表示比赛开始、结束的时间。

输出格式
一个整数最多参加的比赛数目。

输入输出样例
输入
3
0 2
2 4
1 3
输出
2
1≤n≤10^6 0≤ai<bi ≤10^6
思路:主要是判断比赛的结束时间,因为最快结束才能最快开始下一场比赛,所以排顺序的时候是排比赛结束的顺序

#include <bits/stdc++.h>

using namespace std;
struct contect{
	int s,e;
}a[1000010];
bool cmp(contect x,contect y){
	return x.e <y.e ;
}
int main(){
	int n;
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>a[i].s >>a[i].e ;
	}
	sort(a,a+n,cmp);
	int cnt=a[0].e ,ans=1;
	for(int i=1;i<n;i++){
		if(a[i].s >=cnt){
			cnt=a[i].e ;
			ans++;
		}
	}
	cout<<ans<<endl;
    return 0;
}

P1090 [NOIP2004 提高组] 合并果子 / [USACO06NOV] Fence Repair G

题目
题目描述
在一个果园里,多多已经将所有的果子打了下来,而且按果子的不同种类分成了不同的堆。多多决定把所有的果子合成一堆。

每一次合并,多多可以把两堆果子合并到一起,消耗的体力等于两堆果子的重量之和。可以看出,所有的果子经过 n−1 次合并之后, 就只剩下一堆了。多多在合并果子时总共消耗的体力等于每次合并所耗体力之和。

因为还要花大力气把这些果子搬回家,所以多多在合并果子时要尽可能地节省体力。假定每个果子重量都为 1,并且已知果子的种类 数和每种果子的数目,你的任务是设计出合并的次序方案,使多多耗费的体力最少,并输出这个最小的体力耗费值。

例如有 3种果子,数目依次为 1, 2 , 9 。可以先将 1 、 2堆合并,新堆数目为 3 ,耗费体力为 3 。接着,将新堆与原先的第三堆合并,又得到新的堆,数目为 12,耗费体力为 12。所以多多总共耗费体力 =3+12=15。可以证明 15 为最小的体力耗费值。

输入格式
共两行。
第一行是一个整数(1≤n≤10000) ,表示果子的种类数。

第二行包含 n个整数,用空格分隔,第 i个整数 ai(1≤ai≤20000) 是第i 种果子的数目。

输出格式
一个整数,也就是最小的体力耗费值。输入数据保证这个值小于 2 ^31。

输入输出样例
输入
3
1 2 9
输出
15
说明/提示
对于30%的数据,保证有n≤1000:

对于50%的数据,保证n≤5000;

对于全部的数据,保证有n≤10000。
这题用priority_queue可以快速写出,它可以自动升序/降序排列对此题特别友好!!!
具体请看一个写的不错的博客

#include <bits/stdc++.h>

using namespace std;
int n,a,sum=0;
int main(){
	cin>>n;
	priority_queue<int,vector<int>,greater<int> >q;
	for(int i=1;i<=n;i++){
		cin>>a;
		q.push(a);
	}
	for(int i=1;i<n;i++){
		a=q.top();q.pop();
		a+=q.top();q.pop();
		q.push(a);
		sum+=a;
	}
	cout<<sum<<endl;
    return 0;
}

P1031 [NOIP2002 提高组] 均分纸牌

题目
有N堆纸牌,编号分别为1,2,…,N。每堆上有若干张,但纸牌总数必为N的倍数。可以在任一堆上取若干张纸牌,然后移动。

移牌规则为:在编号为1堆上取的纸牌,只能移到编号为2的堆上;在编号为N的堆上取的纸牌,只能移到编号为N−1的堆上;其他堆上取的纸牌,可以移到相邻左边或右边的堆上。

现在要求找出一种移动方法,用最少的移动次数使每堆上纸牌数都一样多。

例如N=4,4堆纸牌数分别为:

①9②8③17④6

移动3次可达到目的:

从 ③ 取4张牌放到 ④ (9,8,13,10)-> 从 ③ 取3张牌放到 ②(9,11,10,10)-> 从 ② 取1张牌放到①(10,10,10,10)。

输入格式
两行

第一行为:N(N 堆纸牌,1≤N≤100)

第二行为:A1,A2 ,…,An(N堆纸牌,每堆纸牌初始数,1≤Ai≤10000)

输出格式
一行:即所有堆均达到相等时的最少移动次数。

输入输出样例
输入
4
9 8 17 6
输出
3
这题往一个方向放纸牌就好了。
不过呢我之前想了一些很特殊的情况,比如说我代码里面是从左往右放,但如果放置的时候第二个的纸牌数目不足以将前一个纸牌数目达到均值的时候该怎么办呢,比如:1,2,20,17;这个时候其实实际上就是它的反过来思考,也就是代码走的是:10,-7,20,17>10,10,3,17>10,10,10,10;但现实中不能出现负值,此时它可以逆着走,也就是:1,2,27,10>1,19,10,10>10,10,10,10同样也是3次实现,更复杂的也同样可以这么思考;
所以只需要判断要纸牌堆不为均值的情况,不用再很复杂的考虑是否出现负值的情况;

#include <bits/stdc++.h>
using namespace std;
int sum=0,cnt=0,a[105];
int main(){
	int n;
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>a[i];
		sum+=a[i];
	}
	sum=sum/n;
	for(int i=0;i<n;i++){
		if(a[i]!=sum){//注意!
			a[i+1]-=sum-a[i];
			cnt++;
		}
	}
	cout<<cnt<<endl;
    return 0;
}

P2240 【深基12.例1】部分背包问题

题目
阿里巴巴走进了装满宝藏的藏宝洞。藏宝洞里面有 N(N≤100) 堆金币,第 i 堆金币的总重量和总价值分别是 mi,vi(1≤mi,vi≤100)。阿里巴巴有一个承重量为 T(T≤1000) 的背包,但并不一定有办法将全部的金币都装进去。他想装走尽可能多价值的金币。所有金币都可以随意分割,分割完的金币重量价值比(也就是单位价格)不变。请问阿里巴巴最多可以拿走多少价值的金币?

输入格式
第一行两个整数 N,T。

接下来 N行,每行两个整数 mi,vi。

输出格式
一个实数表示答案,输出两位小数

输入输出样例
输入
4 50
10 60
20 100
30 120
15 45
输出
240.00
这题就是很简单的找性价比然后从性价比最高的放进去

#include <bits/stdc++.h>

using namespace std;
int n,t;
double sum;
struct hole{
	int m,v;
	double portion;
}a[110];
bool cmp(hole x,hole y){
	return x.portion >y.portion ;
}
int main(){
	cin>>n>>t;
	for(int i=0;i<n;i++){
		cin>>a[i].m >>a[i].v ;
		a[i].portion =1.0*a[i].v/a[i].m ;
	}
	sort(a,a+n,cmp);
	for(int i=0;i<n;i++){
		if(a[i].m <=t){
			sum+=a[i].v ;
			t-=a[i].m ;
		}
		else{
			sum+=a[i].portion *t;
			break;
		}
	}
	printf("%.2lf",sum); 
    return 0;
}

1328:Radar Installation

题目
描述
Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. Each small island is a point locating in the sea side. And any radar installation, locating on the coasting, can only cover d distance, so an island in the sea can be covered by a radius installation, if the distance between them is at most d.

We use Cartesian coordinate system, defining the coasting is the x-axis. The sea side is above x-axis, and the land side below. Given the position of each island in the sea, and given the distance of the coverage of the radar installation, your task is to write a program to find the minimal number of radar installations to cover all the islands. Note that the position of an island is represented by its x-y coordinates.
在这里插入图片描述

Figure A Sample Input of Radar Installations
输入
The input consists of several test cases. The first line of each case contains two integers n (1<=n<=1000) and d, where n is the number of islands in the sea and d is the distance of coverage of the radar installation. This is followed by n lines each containing two integers representing the coordinate of the position of each island. Then a blank line follows to separate the cases.

The input is terminated by a line containing pair of zeros
输出
For each test case output one line consisting of the test case number followed by the minimal number of radar installations needed. “-1” installation means no solution for that case.
样例输入
3 2
1 2
-3 1
2 1

1 2
0 2

0 0
样例输出
Case 1: 2
Case 2: 1
题目大致翻译成中文:
假设滑行是一条无限长的直线。陆地在海岸的一边,海洋在另一边。每个小岛都是位于海边的一个点。而任何一个位于海岸线上的雷达装置,都只能覆盖d距离,所以一个半径的装置可以覆盖海上的一个岛屿,如果它们之间的距离最多是d,我们使用笛卡尔坐标系,将海岸线定义为x轴。海侧在x轴上方,陆侧在x轴下方。考虑到每个岛屿在海洋中的位置,考虑到雷达装置的覆盖距离,你的任务是编写一个程序,找到覆盖所有岛屿的雷达装置的最小数量。请注意,岛的位置由其x-y坐标表示。图1雷达装置的输入示例由几个测试用例组成。每种情况的第一行包含两个整数n(1<=n<=1000)和d,其中n是海上岛屿的数量,d是雷达装置的覆盖距离。后面是n行,每行包含两个整数,表示每个岛的位置坐标。然后用一个空行来分隔案例。输入端由一条包含一对零的线终止,对于每个测试用例输出,一条线由测试用例号和所需的最少雷达装置数组成-1“安装意味着这种情况没有解决方案。
此题应该换个角度思考:不从雷达可以接收哪些位置的岛屿,而是从每个岛屿可以被x轴上哪些区间放置的雷达接收到信号(注意放置的位置不一定为整数)
所以,
第一步:先找每个岛屿的区间(能覆盖到它的雷达能放置的位置)
在这里插入图片描述

第二步:对区间的右端点从小到大排序,如果一个岛屿的左端点大于前一个岛屿的右端点说明一个雷达无法满足,则需要再加雷达,如果区间与区间之间有重叠部分则说明它们均可被同一个雷达覆盖

#include <bits/stdc++.h>

using namespace std;
struct node{
	double s,e;
}a[1010];
bool cmp(node x,node y){
	return  x.e <y.e ;
}
int n,d,sum,x,y;
int main(){
	int q=0;
	while(scanf("%d%d",&n,&d),n||d){
		int flag=0;
		q++;
		for(int i=0;i<n;i++){
			cin>>x>>y;
			if(y>d) flag=1;
			a[i].s=double(x)-sqrt((double)(d*d-y*y));//查找每一个岛屿的区间 
			a[i].e=double(x)+sqrt((double)(d*d-y*y));//其实就是用勾股定理找咯不难理解叭
		}
		if(flag) printf("Case %d: -1\n",q);//如果岛屿的y坐标大于了雷达最大距离则说明此岛屿无法被雷达覆盖 
		else{
			sort(a,a+n,cmp);//按区间右端点进行从小到大的排序 
			int ans=1;//一定先有一个雷达 
			double pos=a[0].e;//标记第一个雷达所能覆盖到的最右端 
			for(int i=1;i<n;i++){
				if(pos<a[i].s){//如果a[i]的左端点大于雷达所能覆盖到的最右端则需要再加一个雷达 
					ans++;//加雷达 
					pos=a[i].e ;//新加雷达所能覆盖到的最右端 
				}
			}
			printf("Case %d: %d\n",q,ans);
		}
	}
	
    return 0;
}

P1080 [NOIP2012 提高组] 国王游戏

题目
恰逢 H国国庆,国王邀请n 位大臣来玩一个有奖游戏。首先,他让每个大臣在左、右手上面分别写下一个整数,国王自己也在左、右手上各写一个整数。然后,让这 n 位大臣排成一排,国王站在队伍的最前面。排好队后,所有的大臣都会获得国王奖赏的若干金币,每位大臣获得的金币数分别是:排在该大臣前面的所有人的左手上的数的乘积除以他自己右手上的数,然后向下取整得到的结果。

国王不希望某一个大臣获得特别多的奖赏,所以他想请你帮他重新安排一下队伍的顺序,使得获得奖赏最多的大臣,所获奖赏尽可能的少。注意,国王的位置始终在队伍的最前面。

输入格式
第一行包含一个整数n,表示大臣的人数。

第二行包含两个整数 a和 b,之间用一个空格隔开,分别表示国王左手和右手上的整数。

接下来 nn行,每行包含两个整数a 和 b,之间用一个空格隔开,分别表示每个大臣左手和右手上的整数。

输出格式
一个整数,表示重新排列后的队伍中获奖赏最多的大臣所获得的金币数。

输入输出样例
输入
3
1 1
2 3
7 4
4 6
输出
2
说明/提示
【输入输出样例说明】

按1、2、3 这样排列队伍,获得奖赏最多的大臣所获得金币数为 2;

按 1、3、2这样排列队伍,获得奖赏最多的大臣所获得金币数为2;

按 2、1、3 这样排列队伍,获得奖赏最多的大臣所获得金币数为 2;

按2、3、1这样排列队伍,获得奖赏最多的大臣所获得金币数为9;

按 3、1、2这样排列队伍,获得奖赏最多的大臣所获得金币数为 2;

按3、2、1 这样排列队伍,获得奖赏最多的大臣所获得金币数为 9。

因此,奖赏最多的大臣最少获得 2个金币,答案输出 2。

【数据范围】

对于 20%的数据,有 1≤ n≤ 10,0 < a,b < 8;

对于 40%的数据,有1≤ n≤20,0 < a,b < 8;

对于 60%的数据,有 1≤ n≤100;

对于 60%的数据,保证答案不超过 10^9 ;

对于 100%的数据,有 1 ≤ n ≤1,000,0 < a,b < 10000。

NOIP 2012 提高组 第一天 第二题
这题首先我们要先知道,对于两个大臣的位置是否交换对于这两位大臣前面以及后面大臣都没有影响,那么大臣之间该如何排呢,首先我们可以假设:在这里插入图片描述
由此可以推出左右乘积小的应该排前面
首先根据题意我们可以写出这个代码:

#include <bits/stdc++.h>

using namespace std;
int n,a,b,ans,c;
struct minister{
	int l,r;
}mini[1010];
bool cmp(minister x,minister y){
	return x.l*x.r <y.l *y.r  ;
}
int main(){
	cin>>n>>a>>b;
	for(int i=0;i<n;i++){
		cin>>mini[i].l >>mini[i].r ;
	}
	sort(mini,mini+n,cmp);//通过左右手乘积排序,小的在前面大的在后面 
	c=a;
	for(int i=0;i<n;i++){
		ans=max(ans,c/mini[i].r );//比较每个大臣得到的金币数寻找最大值 
		c*=mini[i].l ;
	}
	cout<<ans<<endl;
    return 0;
}

但是提交的时候只得到60分,这是为什么呢?因为数据过大,我们要考虑到数据最大的情况,即:100001000,把它转化一下即1*104000,也就是说1后面有4000个0我们应该考虑用高精度来实现它(具体关于高精度的一些注意点以及方法可以对照这里做题技巧关于高精度计算的内容(可以点击链接去做做题目哟)
代码如下:(注意高精度除法的时候一定要除去前导0!!!不然会影响结果!!!)

#include <bits/stdc++.h>

using namespace std;
int n,a[4010],b,ans[4010],lans=0,la=1;
struct minister{
	int l,r;
}mini[1010];
bool cmp(minister x,minister y){
	return x.l*x.r <y.l *y.r  ;
}
void findmax(int ri){
	int c[4010],lc=la;//设 c/mini[i].r
	memset(c,0,sizeof(c));
	for(int i=lc-1;i>=0;i--){//c/mini[i].r 高精除以低精
		c[i]+=a[i];
		if(i)
			c[i-1]=c[i]%ri*10;
		c[i]/=ri;
	}
	while(c[lc-1]==0 && lc>1) lc--;//除去前导0  很重要!!! 
	int flag=0;
	if(lans<lc) flag=1;//比较 ans  c/mini[i].r 的大小 
	else if(lans==lc){
		for(int i=lc-1;i>=0;i--){
			if(ans[i]>c[i]) break;
			if(ans[i]<c[i]){
				flag=1;
				break;
			}
		}
	}
	if(flag){//如果flag=1说明 c/mini[i].r 比 ans大 
		lans=lc;
		memcpy(ans,c,sizeof(c));
	} 
}
int main(){
	cin>>n>>a[0]>>b;
	for(int i=0;i<n;i++){
		cin>>mini[i].l >>mini[i].r ;
	}
	sort(mini,mini+n,cmp);//通过左右手乘积排序,小的在前面大的在后面 
//	c=a;
	for(int i=0;i<n;i++){
//		ans=max(ans,c/mini[i].r );//比较每个大臣得到的金币数寻找最大值 
		findmax(mini[i].r);
//		c*=mini[i].l ;
		for(int j=0;j<la;j++){//高精*低精 
			a[j]*=mini[i].l ;
		}
		for(int j=0;j<la;j++){
			a[j+1]+=a[j]/10;
			a[j]%=10;
			if(a[la]>0) la++;
		}
	}
	for(int i=lans-1;i>=0;i--){
		cout<<ans[i];
	}
    return 0;
}

1042:Gone Fishing

题目
描述
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
大致意思为:有n个湖,h小时
从i–(i-1)湖消耗t[i]个时间片(5分钟一个时间片)
湖i第一个时间片能钓到f[i]条鱼,往后每个时间片钓到鱼比上一个时间片少d[i],优先第一个湖时间长
思路:假设最远能到达i湖,他所拥有时间减去到最远那个湖i所用时间后求他所拥有时间之内能钓到的最大鱼的数量
先对0–i个湖第一个时间片能钓到的鱼的数目进行排序,然后寻找湖j最大钓鱼的量s,再将s-d[j]放进去再次排序…重复此步骤
我的思路;我想用priority_queue进行快速排序,写下代码:

#include <bits/stdc++.h>

using namespace std;

int main(){
	int n,h;
	while(scanf("%d",&n),n){
		cin>>h;h*=12;
		int fi[30],di[30],ti[30]={0},fish[30]/*最终每个鱼塘钓鱼数目*/,ffis[30]/*每次循环时每个鱼塘钓鱼量*/;
		for(int i=0;i<n;i++) cin>>fi[i];
		for(int i=0;i<n;i++) cin>>di[i];
		for(int i=1;i<n;i++) cin>>ti[i];
		int tim=0,ans=0;
		memset(fish,0,sizeof(fish)); 
		for(int i=0;i<n;i++){//每次最多只能到达i鱼塘 
			memset(ffis,0,sizeof(ffis));
			tim+=ti[i];//路上耗时 
			int left=h-tim;//可钓鱼时间
			priority_queue<pair<int,int> >p;
			for(int j=0;j<=i;j++) p.push(make_pair(fi[j],j));//将初始钓鱼数按从大到小排序 
			int an=0;
			while(left>0){
				int s =p.top().first;//最大数目的鱼 
				int pos =p.top().second;//所在位置 (对应鱼塘) 
				p.pop();
				if(s<=0){
					ffis[0]+=left;//如果最大钓鱼量为0将剩余时间加到第一个湖上 
					break;//如果最大都是0那么就不用再找了 
				}
				ffis[pos]+=1;//对应鱼塘时间段加一 
				p.push(make_pair(s-di[pos],pos));
				an+=s;//求此方法的最大钓鱼数目 
				left--;
			}
			if(an>ans){
				ans=an;
//				memcpy(fish,ffis,sizeof(ffis));
				for(int j=0;j<n;j++){
					fish[j]=ffis[j];
				} 
			}
			while(!p.empty()) p.pop();
		}
		printf("%d",fish[0]*5);
		for(int i=1;i<n;i++) printf(", %d",fish[i]*5);
		cout<<endl;
		cout<<"Number of fish expected:"<<ans<<endl;
	} 
    return 0;
}

编译时发现最后一组数据出现问题:在这里插入图片描述
错误原因:对priority_queue<pair<int,int> >p;的认识不全
此排序先是对第一个int进行从高到低排序,若第一个int相同,然后对第二个int进行从高到低排序,但是题目优先考虑前面的湖里钓鱼,即第二个int应该从低到高排序才对,我不知道如何对priority_queue<pair<int,int> >p;进行修改,所以可以对湖的编号逆排序:

#include <bits/stdc++.h>

using namespace std;

int main(){
	int n,h;
	while(scanf("%d",&n)&&n>0){
		cin>>h;h*=12;
		int fi[30],di[30],ti[30],fish[30]/*最终每个鱼塘钓鱼数目*/,ffis[30]/*每次循环时每个鱼塘钓鱼量*/;
		for(int i=(n-1);i>=0;i--) cin>>fi[i];
		for(int i=(n-1);i>=0;i--) cin>>di[i];
		for(int i=(n-2);i>=0;i--) cin>>ti[i];
		int tim=0,ans=0;
		memset(fish,0,sizeof(fish)); 
		ti[n-1]=0;
		for(int i=(n-1);i>=0;i--){//每次最多只能到达i鱼塘 
			memset(ffis,0,sizeof(ffis));
			tim+=ti[i];//路上耗时 
			int left=h-tim;//可钓鱼时间
			priority_queue<pair<int,int> >p;
			for(int j=(n-1);j>=i;j--) p.push(make_pair(fi[j],j));//将初始钓鱼数按从大到小排序 
			int an=0;
			while(left>0){
				int s =p.top().first;//最大数目的鱼 
				int pos =p.top().second;//所在位置 (对应鱼塘) 
				p.pop();
				if(s<=0){
					ffis[n-1]+=left;//如果最大钓鱼量为0将剩余时间加到第一个湖上 
					break;//如果最大都是0那么就不用再找了 
				}
				ffis[pos]+=1;//对应鱼塘时间段加一 
				p.push(make_pair(s-di[pos],pos));
				an+=s;//求此方法的最大钓鱼数目 
				left--;
			}
			if(an>ans){
				ans=an;
				memcpy(fish,ffis,sizeof(ffis));
//				for(int j=0;j<n;j++){
//					fish[j]=ffis[j];
//				} 
			}
			while(!p.empty()) p.pop();
		}
		printf("%d",fish[n-1]*5);
		for(int i=(n-2);i>=0;i--) printf(", %d",fish[i]*5);
		cout<<endl;
		cout<<"Number of fish expected:"<<ans<<endl;
		cout<<endl;
	} 
    return 0;
}

不过答案 Presentation Error了还没找到原因找到再更辽

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值