题解[USACO17JAN]Balanced Photo G

Posted longdouble

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了题解[USACO17JAN]Balanced Photo G相关的知识,希望对你有一定的参考价值。

题目链接:https://www.luogu.com.cn/problem/P3608

方法一

用树状数组求逆序对先后扫两遍,一次从前往后,一次从后往前,算出每头奶牛左右两边比她高的数量。

最后统计一下。

#include <bits/stdc++.h>
using namespace std;
int sum[500010], l[100010], r[100010];
int n, m, u, v, a[500010], t[500010];
int ans;

inline int read()
{
    int x = 0;
	int f = 1; char ch = getchar();
    while (ch < ‘0‘ || ch > ‘9‘) {if (ch == ‘-‘) f = -1; ch = getchar();}
    while (ch >= ‘0‘ && ch <= ‘9‘) {x = x * 10 + ch - ‘0‘; ch = getchar();}
    return x * f;
}

int lowbit(int x)
{
	return x & (-x);
}

void add(int i, int v)
{
???? while (i <= n)
	{
		sum[i] += v;
		i += lowbit(i);
	}
}

int query(int i)
{
	int ans = 0;
???? while (i)
	{
		ans += sum[i];
		i -= lowbit(i);
	}
	return ans;
}

int main()
{
	n = read();
	for (int i = 1; i <= n; i++)
		a[i] = read(), t[i] = a[i];
???? sort(t + 1, t + 1 + n);
	m = unique(t + 1, t + 1 + n) - t - 1;
	for (int i = 1; i <= n; i++)
		a[i] = lower_bound(t + 1, t + 1 + m, a[i]) - t;
???? for (int i = 1; i <= n; i++)
	{
		add(a[i], 1);
		l[i] = i - query(a[i]);
	}
	memset(sum, 0, sizeof sum);
???? for (int i = n; i >= 1; i--)
	{
		add(a[i], 1);
		r[i] = n - i + 1 - query(a[i]);
	}
???? for (int i = 1; i <= n; i++)
		if (max(l[i], r[i]) > 2 * min(l[i], r[i]))
			ans++;
	printf("%d
", ans);
	return 0;
}

以上是关于题解[USACO17JAN]Balanced Photo G的主要内容,如果未能解决你的问题,请参考以下文章

luogu P2880 [USACO07JAN]平衡的阵容Balanced Lineup 题解

洛谷P3608 [USACO17JAN]Balanced Photo平衡的照片

[USACO17JAN]Balanced Photo平衡的照片

bzoj4759 [Usaco2017 Jan]Balanced Photo

BZOJ 1699 [Usaco2007 Jan]Balanced Lineup排队 线段树

[BZOJ] 1636: [Usaco2007 Jan]Balanced Lineup