gym 102082G BZOJ4240 贪心+树状数组

4240: 有趣的家庭菜园

Time Limit: 10 Sec  Memory Limit: 256 MB
Submit: 756  Solved: 349
[Submit][Status][Discuss]

Description

对家庭菜园有兴趣的JOI君每年在自家的田地中种植一种叫做IOI草的植物。JOI君的田地沿东西方向被划分为N个区域,由西到东标号为1~N。IOI草一共有N株,每个区域种植着一株。在第i个区域种植的IOI草,在春天的时候高度会生长至hi,此后便不再生长。
为了观察春天的样子而出行的JOI君注意到了IOI草的配置与预定的不太一样。IOI草是一种非常依靠阳光的植物,如果某个区域的IOI草的东侧和西侧都有比它高的IOI草存在,那么这株IOI草就会在夏天之前枯萎。换句话说,为了不让任何一株IOI草枯萎,需要满足以下条件:
对于任意2<=i<=N-1,以下两个条件至少满足一个:
1. 对于任意1<=j<=i-1,hj<=hi
2. 对于任意i+1<=j<=N,hk<=hi
IOI草是非常昂贵的,为了不让IOI草枯萎,JOI君需要调换IOI草的顺序。IOI草非常非常的高大且纤细的植物,因此JOI君每次只能交换相邻两株IOI草。也就是说,JOI君每次需要选择一个整数i(1<=i<=N-1),然后交换第i株IOI草和第i+1株IOI草。随着夏天临近,IOI草枯萎的可能性越来越大,因此JOI君想知道让所有IOI草都不会枯萎的最少操作次数。
现在给出田地的区域数,以及每株IOI草的高度,请你求出让所有IOI草的不会枯萎的最少操作次数。

Input

第一行一个正整数N,代表田地被分为了N个区域。
接下来N行,第i行(1<=i<=N)一个整数hi,表示第i株植物在春天时的高度

Output

输出一行一个整数,表示最少需要的操作次数

Sample Input

6
2
8
4
5
3
6

Sample Output

3

HINT

最终的高度序列为2 4 5 8 6 3,共需要操作三次。
3<=N<=3*10^5
1<=hi<=10^9
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<time.h>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize(2)
using namespace std;
#define maxn 300005
#define inf 0x7fffffff
//#define INF 1e18
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
#define mclr(x,a) memset((x),a,sizeof(x))
typedef long long  ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-5
typedef pair<int, int> pii;
#define pi acos(-1.0)
//const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii;

inline int rd() {
	int x = 0;
	char c = getchar();
	bool f = false;
	while (!isdigit(c)) {
		if (c == '-') f = true;
		c = getchar();
	}
	while (isdigit(c)) {
		x = (x << 1) + (x << 3) + (c ^ 48);
		c = getchar();
	}
	return f ? -x : x;
}


ll gcd(ll a, ll b) {
	return b == 0 ? a : gcd(b, a%b);
}
int sqr(int x) { return x * x; }



/*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
	if (!b) {
		x = 1; y = 0; return a;
	}
	ans = exgcd(b, a%b, x, y);
	ll t = x; x = y; y = t - a / b * y;
	return ans;
}
*/

ll ans;
int n, l, r;
struct node {
	int v, p;
}t[maxn];
int tr[maxn];
bool cmp(node a, node b) {
	return a.v > b.v;
}
void add(int x) {
	while (x <= n) {
		tr[x]++; x += x & -x;
	}

}

int query(int x) {
	int rs = 0;
	while (x > 0) {
		rs += tr[x]; x -= x & -x;
	}
	return rs;
}

int main()
{
	//	ios::sync_with_stdio(0);
	n = rd();
	for (int i = 1; i <= n; i++)t[i].v = rd(), t[i].p = i;
	sort(t + 1, t + 1 + n, cmp);
	for (int i = 1; i <= n; i++) {
		int tp = i; while (t[tp].v == t[tp + 1].v&&tp < n)tp++;
		for (int j = i; j <= tp; j++)ans += 1ll * min(query(t[j].p), i - 1 - query(t[j].p));
		for (int j = i; j <= tp; j++)add(t[j].p);
		i = tp;
	}
	cout << (ll)ans << endl;
	return 0;
}

猜你喜欢

转载自www.cnblogs.com/zxyqzy/p/10375968.html
今日推荐