【CF383A】Milking cows

题目

题意翻译
题目描述 Iahub在农场帮助他的祖父,今天他必须挤牛奶。农场里有nn只牛左成一排,从左到右编号11到nn。每只牛不是面向左边就是面向右边。当Iahub挤某一只牛的牛奶时,所有看得到这当前只牛的牛都会因为受到惊吓而损失一单位量的牛奶。面向左边的牛可以看到它左边的所有牛(不包括自己),面向右边的牛可以看到它右边的所有牛(不包括自己)。被惊吓过的牛可以再次被惊吓。被挤过奶的牛不会再被惊吓。我们假定每一头牛可以生产无限量的牛奶。

Iahub可以决定挤牛奶的顺序,但他必须每头牛都要挤。Iahub想损失尽可能少的牛奶。请输出最少的牛奶损失量。

输入输出格式

输入格式 第一行包括一个整数n(1<=n<=200000)n(1<=n<=200000). 第二行包括n个整数a_1,a_2…,a_na
1
​ ,a
2
​ …,a
n
​ 。a_ia
i
​ 是00表示牛面向左边,11则表示面向右边。

输出格式 输出一个整数,即最少的牛奶损失量

在C++中,请不要用%lld分类符来读入或输出64位整型。最好用输出输出流或者%I64D分类符

说明 在第一个样例中,Iahub按照下面的顺序可以使得牛奶损失最少:3,4,2,13,4,2,1。当他挤牛33时,牛44会损失11单位牛奶。在此之后,没有牛奶损失。

Translated by @Imakf

题目描述
Iahub helps his grandfather at the farm. Today he must milk the cows. There are n n cows sitting in a row, numbered from 1 1 to n 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.

输入输出格式
输入格式:
The first line contains an integer n n (1<=n<=200000) (1<=n<=200000) . The second line contains n n integers a_{1} a
1
​ , a_{2} a
2
​ , …, a_{n} a
n
​ , where a_{i} a
i
​ is 0 0 if the cow number i i is facing left, and 1 1 if it is facing right.

输出格式:
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.

输入输出样例
输入样例#1:
4
0 0 1 0
输出样例#1:
1
输入样例#2:
5
1 0 1 0 1
输出样例#2:
3
说明
In the first sample Iahub milks the cows in the following order: cow 3 3 , cow 4 4 , cow 2 2 , cow 1 1 . When he milks cow 3 3 , cow 4 4 loses 1 1 unit of milk. After that, no more milk is lost.

思路

这道题我们可以使用贪心的解法。

由题意可知,如果我们把向左看的牛从右到左去挤奶,这样面朝左的牛就不会失去牛奶。这样,向右看的牛i丢失的牛奶量为i右边所有向左看奶牛的总和。

另一种情况,如果我们把向右看的牛从左到右去挤奶,这样面朝右的牛就不会失去牛奶。这样,向左看的牛i丢失的牛奶量为i左边所有向右看奶牛的总和。

不难证明,上述贪心算法是正确的。实际上,上述两种方案的结果是一样的,因为若第一种情况中有向左看的a惊吓了向右看的b,则第二种情况中就会有向右看的bb惊吓了向左看的aa。

所以,上述两种情况是等价的,只需算出任一种情况就行。

代码

#include<cstdio>
#include<iostream>
using namespace std;
int n,t;
long long cnt,sum;
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;++i)
    {
        scanf("%d",&t);
        if(t) ++cnt;      
        else sum+=cnt;
    }
    cout<<sum<<endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Based on the following story, continue the story by writing two paragraphs, paragraph 1 beginning with "A few weeks later, I went to the farm again. " and paragraph 2 beginning with "I was just about to leave when the hummingbird appeared."respectively with 150 words. I was invited to a cookout on an old friend's farm in western Washington. I parked my car outside the farm and walked past a milking house which had apparently not been used in many years.A noise at a window caught my attention,so I entered it. It was a hummingbird,desperately trying to escape. She was covered in spider-webs and was barely able to move her wings. She ceased her struggle the instant I picked her up. With the bird in my cupped hand, I looked around to see how she had gotten in. The broken window glass was the likely answer. I stuffed a piece of cloth into the hole and took her outside,closing the door securely behind me. When I opened my hand, the bird did not fly away; she sat looking at me with her bright eyes.I removed the sticky spider-webs that covered her head and wings. Still, she made no attempt to fly.Perhaps she had been struggling against the window too long and was too tired? Or too thirsty? As I carried her up the blackberry-lined path toward my car where I kept a water bottle, she began to move. I stopped, and she soon took wing but did not immediately fly away. Hovering,she approached within six inches of my face. For a very long moment,this tiny creature looked into my eyes, turning her head from side to side. Then she flew quickly out of sight. During the cookout, I told my hosts about the hummingbird incident. They promised to fix the window. As I was departing, my friends walked me to my car. I was standing by the car when a hummingbird flew to the center of our group and began hovering. She turned from person to person until she came to me. She again looked directly into my eyes, then let out a squeaking call and was gone. For a moment, all were speechless. Then someone said, “She must have come to say good-bye.”
02-12

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值