Photos of USACO17JAN Balanced Photo Balance

Posted by TTT on Thu, 06 Jun 2019 00:48:56 +0200

Topic Description

Farmer John is arranging his  cows in a line to take a photo (). The height of the th cow in sequence is , and the heights of all cows are distinct.

As with all photographs of his cows, FJ wants this one to come out looking as nice as possible. He decides that cow looks "unbalanced" if  and  differ by more than factor of 2, where  and  are the number of cows taller than  on her left and right, respectively. That is,  is unbalanced if the larger of  and  is strictly more than twice the smaller of these two numbers. FJ is hoping that not too many of his cows are unbalanced.

Please help FJ compute the total number of unbalanced cows.

FJ is arranging for his N cows to stand in a row to take pictures. (1<=N<=100,000) The height of the first cow in the sequence is h[i], and all cows in the sequence have different heights.

Like all his cow photographs, FJ wants this to look as good as possible. He believed that if the number of L[i] and R[i] were more than two times different, the first cow would be unbalanced. (L[i] and R[i] represent the number of cows on the left and right sides of the first cow higher than hers, respectively). If the number of the larger in L[i] and R[i] is more than double that of the smaller, the cow is also unbalanced. FJ doesn't want him to have too many cow imbalances.

Help FJ calculate the unbalanced cow population.

Input and output format

Input format:

The first line of input contains . The next  lines contain , each a nonnegative integer at most 1,000,000,000.

The first line is an integer N. The next N rows consist of H[1] to H[n], each of which is a non-negative integer (no more than 1,000,000,000).

Output format:

Please output a count of the number of cows that are unbalanced.

Please export an unbalanced number of cows.

Input and Output Samples

Input sample #1:
7
34
6
23
0
5
99
2
Output sample #1:
3

                                                                                                                                                                    










This is essentially to ask you to reverse the order, but foolishly I went up and chose to fight violence... (So I'm really a scum)

Inverse order pair is just a tree array, discretize it, and then traverse it.

Sixty points of violence:

#include <iostream>  
#include <cstdio>  
#include <algorithm>  
using namespace std;  
inline int read(void){  
    int x=0,f=1;  
    char ch=getchar();  
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}  
    while(isdigit(ch)){x=x*10+ch-'0';ch=getchar();}  
    return x*f;  
}  
const int maxn=100009;  
typedef long long LL;  
struct hehe{  
    int s; LL h;  
}H[maxn];  
inline bool cmp(const hehe&a,const hehe&b){  
    return a.h<b.h;  
}  
int main(){  
//  freopen("photo.in","r",stdin);  
//  freopen("photo.out","w",stdout);  
    int n=read(),ans=0;  
    for(int i=1;i<=n;i++){  
        H[i].h=read(); H[i].s=i;  
    }  
    sort(H+1,H+n+1,cmp);  
    for(int i=1;i<=n;i++){  
        int l=0,r=0;  
        for(int j=i+1;j<=n;j++){  
            if(H[i].s>H[j].s) l++;  
            if(H[i].s<H[j].s) r++;  
        }  
//      cout<<l<<' '<<r<<endl;  
        if((l==0&&r!=0)||(l!=0&&r==0)){ans++;continue;}  
        else if(l==0&&r==0) continue;  
        else{  
            int xx=max(l,r),yy=min(l,r);  
            if(xx/yy>2) ans++;  
            else if((xx/yy)==2&&yy*2!=xx) ans++;  
        }  
    }  
    cout<<ans<<endl;  
    return 0;  
}   
Inverse order pairs of tree arrays:

//He did not use n^2.
#include<cstdio>  
#include<algorithm>  
#include<iostream>  
#include<queue>  
using namespace std;  
const int maxn=100009;  
int n,a[maxn],b[maxn];  
int l[maxn],r[maxn],f[maxn];  
inline int lowbit(int x){  
    return x&-x;  
}  
inline void add(int x){  
    for(;x<=n;x+=lowbit(x))  
        f[x]++;  
}  
inline int sum(int x){  
    int ans=0;  
    for(;x;x-=lowbit(x))  
        ans+=f[x];  
    return ans;  
}  
int main(){  
    freopen("photo.in","r",stdin);  
    freopen("photo.out","w",stdout);  
    scanf("%d",&n);  
    for(int i=1;i<=n;i++)  
        scanf("%d",&a[i]),b[i]=a[i];  
    sort(b+1,b+n+1);  
    for(int i=1;i<=n;i++)  
        a[i]=lower_bound(b+1,b+n+1,a[i])-b;  
    for(int i=1;i<=n;i++)  
        l[i]=i-1-sum(a[i]),add(a[i]);//Looking for an inverse pair.
    memset(f,0,sizeof(f));  
    for(int i=n;i;i--)  
        r[i]=n-i-sum(a[i]),add(a[i]);//In reverse order.
    int ans=0;  
    for(int i=1;i<=n;i++){  
        if(l[i]==0&&r[i]==0) continue;  
        if(!l[i]||!r[i]) ans++;  
        else{  
            int xx=max(l[i],r[i]),yy=min(l[i],r[i]);  
            if(xx/yy>2) ans++;  
            else if(xx/yy==2&&yy*2!=xx) ans++;  
        }  
    }  
    printf("%d",ans);  
    return 0;  
}  
This warns us: Violence is good, but don't be greedy.