zoukankan      html  css  js  c++  java
  • [9018_1592]USACO 2014 Open Silver Fairphoto

    题目描述

    Farmer John's N cows (1 <= N <= 100,000) are standing at various positions along a long one-dimensional fence.  The ith cow is standing at position x_i (an integer in the range 0...1,000,000,000) and has breed b_i (either 'G' for Guernsey or 'H' for Holstein).  No two cows occupy the same position.

    FJ wants to take a photo of a contiguous interval of cows for the county fair, but we wants all of his breeds to be fairly represented in the photo.Therefore, he wants to ensure that, for whatever breeds are present in the photo, there is an equal number of each breed (for example, a photo with all Holsteins is ok, a photo with 27 lsteins and 27 Guernseys is ok, but a photo with 10 Holsteins and 9 Guernseys is not ok). 

    Help FJ take his fair photo by finding the maximum size of a photo that satisfies FJ's constraints.  The size of a photo is the difference between the maximum and
    minimum positions of the cows in the photo.  It is possible that FJ could end up taking a photo of just a single cow, in which case this photo would have size zero.

    PROBLEM NAME: fairphoto

    FJ的N只牛(1 <= N <= 100,000)站在一排长长的栅栏前的不同位置。第i只牛站在位置xi(0...,1,000,000,000),且它的品种是bi(或者是G,或者是H)。任意的两只牛不会占着同一个位置。

    FJ想为连续区间的牛照一张相片,使得照片上两品种的牛的数目是公平的。例如照片上所有的牛品种都是H,一照片上有27个G品种,27个H品种都是可以的,但是如果一照片上有10个H品种,9个G品种就不行。

    请帮助FJ照一张公平的照片且照片尺寸最大。照片尺寸为照片中最大位置与最小位置差。如果最终照一张只包含一只牛的照片,那么这张照片的尺寸为0.

    输入

    * Line 1: The integer N.

    * Lines 2..1+N: Line i+1 contains x_i and b_i.

    输入的第1行为整数N

    第2...i+1行,每行包含整数xi与bi

    输出

    * Line 1: A single integer indicating the maximum size of a fair   photo.

    输出仅有一个整数,表示可以照一张公平的照片的最大尺寸。

    样例输入

    6
    4 G
    10 H
    7 G
    16 G
    1 G
    3 H

    样例输出

    7

    提示

    INPUT DETAILS:

    There are six cows with breeds (from left to right) G, H, G, G, H, G. 

    OUTPUT DETAILS:

    The largest fair photo Farmer John can take is of the middle 4 cows,containing 2 Holsteins and 2 Guernseys.

    我们把G看成-1,H看成1,求前缀和

    -1,0,-1,-2,-1,-2

    很明显,如果[x,y]满足题目要求,那么b[x-1]=b[y](b前缀和数组)

    我们存下每个前缀和值最早的出现位置,然后遍历整个前缀和数组,对于每一个值,查找它的最小位置,算出ans

    注意:0的最小位置应该是0,为了避免数组越界(前缀和为负数),我们存起来的时候要+n

    代码是我好久以前写的,所以可能有点丑(其实是模拟赛做到跟这差不多的突然想加博客)

    #include<iostream>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    struct cow{
        int w;char c;
    }ccow[100001];
    bool cmp(cow a,cow b)
    {
        return a.w<b.w;
    }
    int hg[100001];
    int big[300001];
    int sta[300001];
    int main()
    {
        memset(big,-1,sizeof(big));memset(sta,-1,sizeof(sta));
        int n,MAX=-1;
        cin>>n;
        for(int i=1;i<=n;i++)cin>>ccow[i].w>>ccow[i].c;
        sort(ccow+1,ccow+n+1,cmp);
        hg[0]=0;
        sta[n]=0;char dgh=ccow[1].c;
        int len=0;
        for(int i=2;i<=n;i++)
        {
            if(ccow[i].c==dgh)len+=(ccow[i].w-ccow[i-1].w);
            else {MAX=max(MAX,len);len=0;dgh=ccow[i].c;}
        }
        MAX=max(MAX,len);
        for(int i=1;i<=n;i++)
        {
        hg[i]=hg[i-1];
            if(ccow[i].c=='H')hg[i]++;
            else hg[i]--;
            if(sta[hg[i]+n]==-1)sta[hg[i]+n]=i;
            else big[hg[i]+n]=i;
        }
        for(int i=0;i<=2*n;i++)
        {
            if(big[i]!=-1)MAX=max(MAX,ccow[big[i]].w-ccow[sta[i]+1].w);
        }
        cout<<MAX;
     } 
  • 相关阅读:
    SCILAB简介[z]
    UG OPEN API编程基础 2约定及编程初步
    Office 2003与Office 2010不能共存的解决方案
    UG OPEN API 编程基础 3用户界面接口
    NewtonRaphson method
    UG OPEN API编程基础 13MenuScript应用
    UG OPEN API编程基础 14API、UIStyler及MenuScript联合开发
    UG OPEN API编程基础 4部件文件的相关操作
    UG OPEN API编程基础 1概述
    16 UG Open的MFC应用
  • 原文地址:https://www.cnblogs.com/lher/p/7512442.html
Copyright © 2011-2022 走看看