Popular driving range - greedy -P1223 water line connection

Description Title
has n individual line connected to a tap water before, if the water receiving each time is Ti, please find this programming individual queue n in a sequential, such that n individual minimum average waiting time.

Input and output format
input format:

Input common two rows, the first row n; second line respectively denote an n-th individual to individual contact of water per time T1, T2, ..., Tn, there is a space between each data.

Output formats:

There are two output lines, a first act of the queue sequence, i.e., an arrangement 1 to n; (accurate output result after two decimal places) the average waiting time in the second embodiment are arranged such behavior.

Input Output Sample
Input Sample # 1:

10
56 12 1 99 1000 234 33 55 99 812

Output Sample # 1:

32781496105
291.90
----------------
ideas: to traverse the entire array at the other person seeking person kick the accumulated total time spent on ans, Finally ans divided by n.

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
struct water{
	int num;
	int time;
	
}p[10010];
double ans;
inline bool cmp(water x,water y){
	if(x.time!=y.time) return x.time < y.time;
	return x.num < y.num;
} 

int main(){
	int n;
	scanf("%d",&n);
	for(int i = 1;i <= n;i++){
		scanf("%d",&p[i].time);
		p[i].num = i;
	}
	sort(p + 1,p + n + 1,cmp);
	for(int i = 1;i <= n;i++)
	printf("%d ",p[i].num);
	printf("\n");
	for(int i = 1; i<=n;i++)
	 ans += i*p[n-i].time;
	 ans /= n;
	 printf("%.2lf",ans);
	return 0;
}
Published 80 original articles · won praise 1 · views 1472

Guess you like

Origin blog.csdn.net/zqhf123/article/details/104370785