codeforces 384C Milking cows(脑洞+思维)

Iahub helps his grandfather at the farm. Today he must milk the cows. There are ncows 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 nintegers a1a2, ..., 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 cincout streams or the %I64d specifier.

Example
Input
4
0 0 1 0
Output
1
Input
5
1 0 1 0 1
Output
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.

 

 【题解】 一排奶牛,有的向左,有的向右,现在要挤奶,这些奶牛都比较胆小,如果当前被挤奶的奶牛被其他牛看见,那么其他牛会减产1,假设奶牛的奶是无限的(送给我吧,我抱着它睡,QWQ)。

 就找一下规律模拟一下,见注释。

 【AC代码】

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
__int64 a[200005],l[200005],r[200005];//值可能很大
int main()
{
    int m;
    __int64 ans;
    while(~scanf("%d",&m))
    {
        ans=0;
        memset(l,0,sizeof(l));
        memset(r,0,sizeof(r));
        int lt=0,rt=0;
        for(int i=1;i<=m;i++){
            scanf("%d",&a[i]);
            if(a[i]==0)lt++;//统计朝左朝右的数量
            else  rt++;
        }
        if(lt>rt)//朝左的多
        {
            for(int i=m;i>0;--i)
                if(a[i]==1){
                l[i-1]=l[i];
                ans+=l[i];}
                else l[i-1]=l[i]+1;//统计每个朝右的牛左边朝右的牛的数量
            printf("%I64d\n",ans);
        }
        else
        {
            for(int i=1;i<=m;++i)//统计每个朝左的牛右边朝左的牛的数量
            {
                if(a[i]==0)
                {
                    r[i+1]=r[i];
                    ans+=r[i];
                }
                else r[i+1]=r[i]+1;
            }
            printf("%I64d\n",ans);
        }
    }
    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值