CodeForces - 1216B Shooting (简单贪心)

CodeForces - 1216B Shooting

题目

Recently Vasya decided to improve his pistol shooting skills. Today his coach offered him the following exercise. He placed nn cans in a row on a table. Cans are numbered from left to right from 11 to nn. Vasya has to knock down each can exactly once to finish the exercise. He is allowed to choose the order in which he will knock the cans down.

Vasya knows that the durability of the ii-th can is aiai. It means that if Vasya has already knocked xx cans down and is now about to start shooting the ii-th one, he will need (ai⋅x+1) shots to knock it down. You can assume that if Vasya starts shooting the ii-th can, he will be shooting it until he knocks it down.

Your task is to choose such an order of shooting so that the number of shots required to knock each of the nn given cans down exactly once is minimum possible.


Input

The first line of the input contains one integer n (2≤n≤1000) — the number of cans
The second line of the input contains the sequence a1,a2,…,an (1≤ai≤1000), where ai is the durability of the i-th can.


Output

In the first line print the minimum number of shots required to knock each of the nn given cans down exactly once.

In the second line print the sequence consisting of n distinct integers from 1 to n — the order of indices of cans that minimizes the number of shots required. If there are several answers, you can print any of them.


Sample Input

3
20 10 20

Sample Output

43
1 3 2 

Sample Input

4
10 10 10 10

Sample Output

64
2 1 4 3

Sample Input

6
5 4 5 4 4 5

Sample Output

69
6 1 3 5 2 4 

Sample Input

2
1 4

Sample Output

3
2 1 

题目大意:
题目大意是:给定n个罐子,第i个罐子a[i]需要射击 (a[i]*x+1) 次,问击倒n个罐子最少需要射击多少次,并输出罐子的初始编号,x是在射击这个罐子之前已经射击了几个,第一个射击时 x=0。

题目分析:

容易想到 简单贪心, 从最大 的开始 射击就可以,用一个数组存放开始的顺序,这里我初学, 没有用 map 和pair 只是单纯的用两个数组。初学来说 ,代码比较容易看懂。下面代码,有详细注释。

AC代码

#include 
int main()
{
	int n,p,ans=0,j=-1,min,max;
	scanf("%d",&n);
	int a[n],i,b[n];
	for(i=0;i<n;i++)
	scanf("%d",&a[i]);
	min=a[n-1];
	for (i=n-2;i>=0;i--)
	{
	if(a[i]<min) 	
	min=a[i];            
	}	//找到最小的那个	
	max=min;
	for (j=0;j<n;j++)
	{
		for(i=0;i<n;i++)
		{
			if(a[i]>=max)
			{
				max=a[i];//找到当前最大的那个
				p=i;//p是当前最大的那个的序号,
			}
			
		}
		
		ans+=(j*max+1);//j是射击的顺序,
		a[p]=0;//把当前最大的变为0,
		b[j]=p;// b数组存放射击的罐子的原始序号。
		max=min;//重置 max,
		
	}
	printf("%d\n",ans); 
	for (i=0;i<n;i++)
	printf("%d ",b[i]+1);
	
	return 0;	
}

我来要赞了,如果觉得解释还算详细,可以学到点什么的话,点个赞再走吧
欢迎各位路过的dalao 指点,提出问题。

你可能感兴趣的:(入门水题,贪心)