首页 > 其他分享 >Codeforces Round #225 (Div. 2) C. Milking cows Greedy

Codeforces Round #225 (Div. 2) C. Milking cows Greedy

时间:2023-04-24 22:38:14浏览次数:51  
标签:cow int Codeforces Greedy Round cows include milk define


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;
}


标签:cow,int,Codeforces,Greedy,Round,cows,include,milk,define
From: https://blog.51cto.com/u_15657999/6221930

相关文章

  • Codeforces Round #285 (Div. 2) C. Misha and Forest
    Let’sdefineaforestasanon-directedacyclicgraph(alsowithoutloopsandparalleledges).OnedayMishaplayedwiththeforestconsistingofnvertices.Foreachvertexvfrom0ton - 1hewrotedowntwointegers,degreevandsv,werethefirstinte......
  • Codeforces Round #450 (Div. 2) D. Unusual Sequences 数学
    Countthenumberofdistinctsequencesa1, a2, …, an(1 ≤ ai)consistingofpositiveintegerssuchthatgcd(a1, a2, …, an) = xand.Asthisnumbercouldbelarge,printtheanswermodulo109 + 7.gcdheremeansthegreatestcommondivisor.Input......
  • Codeforces Round #272 (Div. 2) D. Dreamoon and Sets 数学
    Dreamoonlikestoplaywithsets,integersand.isdefinedasthelargestpositiveintegerthatdividesbothaandb.LetSbeasetofexactlyfourdistinctintegersgreaterthan0.DefineStobeofrankkifandonlyifforallpairsofdistinctelemen......
  • Codeforces Round #308 (Div. 2) E. Vanya and Brackets
    Vanyaisdoinghismathshomework.Hehasanexpressionofform,wherex1, x2, …, xnaredigitsfrom1to9,andsignrepresentseitheraplus‘+’orthemultiplicationsign‘*’.Vanyaneedstoaddonepairofbracketsinthisexpressionsothattoma......
  • Educational Codeforces Round 147
    A题意思路有前导零结果直接为0,出现在第一位的?贡献为9,其他地方的?贡献为10。代码#include<bits/stdc++.h>usingnamespacestd;usingll=longlong;chars[10];intmain(){intT;scanf("%d",&T);while(T--){scanf("%s",s);......
  • Educational Codeforces Round 147 (Rated for Div. 2)
    题目链接B核心思路真的需要加强了,看到这个最大值和最小值我们其实也需要往对应的最大值和最小值的相关操作去想,不如前缀最小值/前缀最大值或者是后缀最小值/后缀最大值。这里一个比较直接的想法就是想找到不同的地方,然后看不可以扩展。那么怎么看是否可以扩展呢,如果是左边的话......
  • Educational Codeforces Round 127 (Rated for Div. 2)
    题目链接D核心思路首先挖掘下操作的性质吧:x>1&&x+3<10:1xx+1x+2x+310我们可以发现这样子好像对于我们的结果是没有影响的,答案还是9.所以这个性质就挖掘出来了啊,只要我们把一段连续的插入到对应的区间里面就好了。也就是只要这个区间的左端点小于插入连续的数的最小值,......
  • Codeforces Beta Round 96 (Div. 1) -- C. Logo Turtle (dp,记忆化搜索)
    记忆化搜索就是暴力,多一步优化,走过的路别走了。说实话,可能是数据水了,居然能过。constintN=510;strings;intn,ans;boolst[501][501][2][50];voiddfs(intx,intidx,intdir,intk){ if(st[x][idx][dir][k])return; st[x][idx][dir][k]=1;//走过的路不走......
  • Educational Codeforces Round 147 (A-D)
    A.Matching橘子熊:这题太简单了我不想写题面Description给定给一个带问号的字符串,求有多少种可能的数字Input多次询问,一次一个字符串Output对于每次询问,输出可能的数字的总数数据范围与约定2e5次询问,单词询问不超过5个字符思路主要思路签到题大部分情况下,一个......
  • Educational Codeforces Round 39 (Rated for Div. 2) -- D. Timetable (DP)
    写得很折磨人,每次dp都写个一个多小时,写出来明明觉得不难.题目大意:可以进行K次操作,把删除1,进行k次操作后每行第一个1和最后一个1的位置相减的绝对值加1得到的结果最小。做法:每次肯定是要从左删或者从右边删,然后顺着这个思路,先把每行的进行小于等于k次操作时,每行最小......