codeforces 384C Milking cows(脑洞+思维)
来源:互联网 发布:js解析url中的参数 编辑:程序博客网 时间:2024/06/03 21:58
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.
The first line contains an integer n (1 ≤ n ≤ 200000). The second line contains nintegers a1, a2, ..., an, where ai is 0 if the cow number i is facing left, and 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.
40 0 1 0
1
51 0 1 0 1
3
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;}
- codeforces 384C Milking cows(脑洞+思维)
- Codeforces 383A Milking cows(贪心)
- Codeforces 383 A. Milking cows(贪心)
- Codeforces Round #225 (Div. 2) C. Milking cows
- Codeforces Round #225 (Div. 2)-C. Milking cows
- Codeforces 284C Cows and Sequence【思维】
- Codeforces 383 A. Milking cows
- Codeforces 383A Milking cows
- Milking Cows(USACO)
- cf#225-C - Milking cows-贪心
- Milking Cows
- Milking Cows
- Milking Cows
- Milking Cows
- Milking Cows
- milking cows
- milking cows
- Milking Cows
- SSH框架的项目执行流程
- Codeforces Round #426 (Div. 2)-A. The Useless Toy
- [设计模式]SINGLETON(单件)_对象创建型模式
- Redis学习四(java使用redis)
- LeetCode 541 Reverse String II
- codeforces 384C Milking cows(脑洞+思维)
- 运维技术盘点
- select2 api参数中文文档
- 线程与进程,线程的可用状态、创建、启动、停止、交互
- 在一个div中有两个子元素,子元素浮动,左边div高度随右边变化
- 瑞博pm9000A+ 黑屏故障分析及处理方法
- java访问修饰符概述
- 条形码和二维码编码解码
- AtomicInteger理解和使用