B. Milena and Admirer(贪心)

Problem - B - Codeforces

B. Milena and Admirer(贪心)_第1张图片

解析:

        每次贪心分解为不降并且左端尽量大即可

#include
using namespace std;
#define int long long
const int N=2e5+5;
int n,a[N]; 
void solve(){
	scanf("%lld",&n);
	for(int i=1;i<=n;i++){
		scanf("%lld",&a[i]);
	}
	int res=0;
	for(int i=n-1;i>=1;i--){
		if(a[i]>a[i+1]){
			int k;
			if(a[i]%2==0) k=a[i]/2;
			else k=a[i]/2+1;
			if(k<=a[i+1]){
				if(a[i]%2==0) a[i]=k;
				else a[i]=k-1;
				res++;
			}
			else{
				if(a[i]%a[i+1]==0){
					res+=a[i]/a[i+1]-1;
					a[i]=a[i+1];
				}
				else{
					res+=a[i]/a[i+1];
					a[i]=a[i]/(a[i]/a[i+1]+1);
				}
			}
//			cout<

你可能感兴趣的:(codeforces,c++,算法,开发语言,数据结构,c语言,贪心)