Iahub helps his grandfather at the farm. Today he must milk the cows. There are n cows sitting in a row, numbered from 1 to n from left to right. Each cow is either facing to the left or facing to the right. When Iahub milks a cow, all the cows that see the current cow get scared and lose one unit of the quantity of milk that they can give. A cow facing left sees all the cows with lower indices than her index, and a cow facing right sees all the cows with higher indices than her index. A cow that got scared once can get scared again (and lose one more unit of milk). A cow that has been milked once cannot get scared and lose any more milk. You can assume that a cow never loses all the milk she can give (a cow gives an infinitely amount of milk).
Iahub can decide the order in which he milks the cows. But he must milk each cow exactly once. Iahub wants to lose as little milk as possible. Print the minimum amount of milk that is lost.
Input
The first line contains an integer n (1 ≤ n ≤ 200000). The second line contains n integers a1, a2, …, an, where ai is 0 if the cow number i is facing left, and 1 if it is facing right.
Output
Print a single integer, the minimum amount of lost milk.
Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
inputCopy
4
0 0 1 0
outputCopy
1
inputCopy
5
1 0 1 0 1
outputCopy
3
Note
In the first sample Iahub milks the cows in the following order: cow 3, cow 4, cow 2, cow 1. When he milks cow 3, cow 4 loses 1 unit of milk. After that, no more milk is lost.
题意:挤牛奶,但是那些可以看见被挤牛奶的奶牛的奶牛会损失一个单位的牛奶,且可以多次损失知道它们自己也被挤了牛奶,求最小损失量;
考虑贪心:很容易发现:当最后的排列为00000……00011111…11111时,是不损失牛奶的,从左向右遍历一遍,标号为1 的就加上其右边为0 奶牛的数量,直到遍历完全;注意:时限1000ms,所以用前缀和维护一下就好了,总复杂度 O( n ).
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<string>
#include<bitset>
#include<ctime>
typedef long long ll;
using namespace std;
typedef unsigned long long int ull;
#define maxn 200005
#define ms(x) memset(x,0,sizeof(x))
#define Inf 0x7fffffff
#define inf 0x3f3f3f3f
const long long int mod = 1e9 + 7;
#define pi acos(-1.0)
#define pii pair<int,int>
#define pll pair<ll,ll>
ll quickpow(ll a, ll b) {
ll ans = 1;
while (b > 0) {
if (b % 2)ans = ans * a;
b = b / 2;
a = a * a;
}
return ans;
}
int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a%b);
}
int a[maxn];
ll sum[maxn];
int main()
{
ios::sync_with_stdio(false);
int n;
cin >> n;
int i, j;
for (i = 1; i <= n; i++)cin >> a[i];
for (i = 1; i <= n; i++) {
if (a[i] == 1)sum[i] = sum[i - 1];
else {
sum[i] = sum[i - 1] + 1ll;
}
}
ll cnt = 0;
for (i = 1; i <= n; i++) {
if (a[i] == 1) {
cnt += (sum[n] - sum[i]);
}
}
cout << cnt << endl;
return 0;
}