Stripies POJ - 1862 Greed

To the effect:
a sequence
xi
requires two operations to be performed, and two x , and Calculate 2 x and and put back

Analysis:
The greedy strategy is either forward or reverse, simply prove the three relationships:

x 1 < x 2 < x 3

2 2 x 1 x 2 x 3 > 2 2 x 1 x 3 x 2

Easy to get forward processing to get maximum, reverse order minimum

Implementation:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<cmath>
using namespace std;
#define pb push_back
#define inf (1e9+10)
#define mem(s,t) memset(s,t,sizeof s)
typedef pair<int,int> pii;
typedef long long ll;
const int MAXN =1e4+10;
int main() {
    int n;
    cin>>n;
    double a[MAXN];
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    sort(a+1,a+n+1);
    for(int i=n-1;i;i--){
        a[i]=2.0*sqrt(1.0*a[i]*a[i+1]);
    }
    printf("%.3f\n",a[1]);
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325408011&siteId=291194637