[2020.10.28 SSL Popularization Simulation Tournament T5] Little b watering the flowers [Mathematics]

Insert picture description here
Insert picture description here

analysis

Sort first, and then think about it. The optimal situation should be to make this sequence of numbers monotonically increasing with the smallest difference between adjacent ones.
Simulate each day, judge the relationship between two flowers, and add up the number.

Upload code

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;

int n,a[40001],ans;

int main()
{
    
    
    cin>>n;
	for(int i=1;i<=n;i++)
	{
    
    
		cin>>a[i];
	}	
	sort(a,a+n+1); 
	for(int i=1;i<=n;i++)
	{
    
    
		if(a[i]==a[i-1]&&i!=1)
		{
    
    
			a[i]=a[i-1]+1;
			ans++;
		}
		else if(a[i]<a[i-1])
		{
    
    
			ans+=(1+a[i-1]-a[i]);
			a[i]=a[i-1]+1;
		}
	}
	cout<<ans;
	return 0;
}

Guess you like

Origin blog.csdn.net/dglyr/article/details/109351053