码迷,mamicode.com
首页 > 其他好文 > 详细

BZOJ3540: [Usaco2014 Open]Fair Photography

时间:2014-10-05 14:16:48      阅读:319      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   io   os   ar   for   

3540: [Usaco2014 Open]Fair Photography

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 72  Solved: 29
[Submit][Status]

Description

FJ‘s N cows (2 <= 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 is either a plain white cow or a spotted cow. No two cows occupy the same position, and there is at least one white cow. FJ wants to take a photo of a contiguous interval of cows for the county fair, but in fairness to his different cows, he wants to ensure there are equal numbers of white and spotted cows in the photo. FJ wants to determine the maximum size of such a fair photo, where the size of a photo is the difference between the maximum and minimum positions of the cows in the photo. To give himself an even better chance of taking a larger photo, FJ has with him a bucket of paint that he can use to paint spots on an arbitrary subset of his white cows of his choosing, effectively turning them into spotted cows. Please determine the largest size of a fair photo FJ can take, given that FJ has the option of painting some of his white cows (of course, he does not need to paint any of the white cows if he decides this is better).

 

 

 

 

 

在X的非负轴上有N个不在同一位置上的数,0或1.至少有1个0.
可以先任意把0染成1.
区间长度定义为,[L,R]中最右和最左的数的差的绝对值.
求一个最长区间,满足区间中所有数0和1的个数相同.
输出这个最长区间的长度.

 

Input

 * Line 1: The integer N.

* Lines 2..1+N: Line i+1 contains x_i and either W (for a white cow) or S (for a spotted cow). 

Output

* Line 1: The maximum size of a fair photo FJ can take, after possibly painting some of his white cows to make them spotted.

Sample Input

5
8 W
11 S
3 W
10 W
5 S

INPUT DETAILS: There are 5 cows. One of them is a white cow at position 8, and so on.

Sample Output

7
OUTPUT DETAILS: FJ takes a photo of the cows from positions 3 to positions 10. There are 4 cows in this range -- 3 white and 1 spotted -- so he needs to paint one of the white cows to make it spotted.

HINT

 

Source

题解:
碰上一道好题。想法和hzwer的一样:

这个是经典题吧。。。把0看成-1

如果不考虑修改那么用last[x]记录前缀和为x的第一个位置

然后扫一遍

加上修改就是。。。

设当前位置pos,前缀和为x,则pos-last[x],pos-last[x+2]...都能更新答案

则在这之前

for(int i=2*n;i>=0;i--)
last[i]=min(last[i+2],last[i]);

我的写法和hzwer有点不一样,不知道哪里写萎了。。。

代码:mine

bubuko.com,布布扣
 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<algorithm>
 6 #include<iostream>
 7 #include<vector>
 8 #include<map>
 9 #include<set>
10 #include<queue>
11 #include<string>
12 #define inf 1000000000
13 #define maxn 250000+5
14 #define maxm 500+100
15 #define eps 1e-10
16 #define ll long long
17 #define pa pair<int,int>
18 #define for0(i,n) for(int i=0;i<=(n);i++)
19 #define for1(i,n) for(int i=1;i<=(n);i++)
20 #define for2(i,x,y) for(int i=(x);i<=(y);i++)
21 #define for3(i,x,y) for(int i=(x);i>=(y);i--)
22 #define mod 1000000007
23 using namespace std;
24 inline int read()
25 {
26     int x=0,f=1;char ch=getchar();
27     while(ch<0||ch>9){if(ch==-)f=-1;ch=getchar();}
28     while(ch>=0&&ch<=9){x=10*x+ch-0;ch=getchar();}
29     return x*f;
30 }
31 struct rec{int x,y;}a[maxn];
32 int n,s[maxn],f[2][maxn];
33 inline bool cmp(rec a,rec b)
34 {
35     return a.x<b.x;
36 }
37 int main()
38 {
39     freopen("input.txt","r",stdin);
40     freopen("output.txt","w",stdout);
41     n=read();
42     for1(i,n)
43     {
44         a[i].x=read();
45         char ch= ;
46         while(ch!=S&&ch!=W)ch=getchar();
47         a[i].y=ch==W?1:-1;
48     }
49     sort(a+1,a+n+1,cmp);
50     memset(f,127,sizeof(f));
51     f[0][n]=0;
52     for1(i,n)
53      {
54       s[i]=s[i-1]+a[i].y;
55       f[i&1][s[i]+n]=min(f[i&1][s[i]+n],i);
56      } 
57     for0(i,1)
58      for1(j,n+n)
59       {
60       f[i][j]=min(f[i][j-1],f[i][j]);
61       }
62     int ans=0;
63     for1(i,n)
64      {
65       ans=max(ans,a[i].x-a[f[i&1][s[i]+n]+1].x);
66      }
67     printf("%d\n",ans);
68     return 0;
69 }
View Code

代码:hzwer

bubuko.com,布布扣
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cstdlib>
 5 #include<algorithm>
 6 #include<cmath>
 7 #include<queue>
 8 #include<set>
 9 #include<map>
10 #define pa pair<int,int>
11 #define inf 1000000000
12 #define ll long long
13 using namespace std;
14 inline int read()
15 {
16     int x=0,f=1;char ch=getchar();
17     while(ch<0||ch>9){if(ch==-)f=-1;ch=getchar();}
18     while(ch>=0&&ch<=9){x=x*10+ch-0;ch=getchar();}
19     return x*f;
20 }
21 int n,now;
22 int last[200005];
23 struct data{int pos,v;}a[100005];
24 inline bool operator<(data a,data b)
25 {
26     return a.pos<b.pos;
27 }
28 int main()
29 {
30     n=read();
31     for(int i=1;i<=n;i++)
32     {
33         char ch[2];
34         a[i].pos=read();
35         scanf("%s",ch);
36         if(ch[0]==W)a[i].v=-1;
37         else a[i].v=1;
38     }
39     sort(a+1,a+n+1);
40     memset(last,127,sizeof(last));
41     int sum=n;
42     last[sum]=a[1].pos;
43     for(int i=1;i<n;i++)
44     {
45         sum+=a[i].v;
46         last[sum]=min(last[sum],a[i+1].pos);
47     }
48     for(int i=2*n;i>=0;i--)
49         last[i]=min(last[i+2],last[i]);
50     int ans=0;sum=n;
51     for(int i=1;i<=n;i++)
52     {
53         sum+=a[i].v;
54         ans=max(ans,a[i].pos-last[sum]);
55     }
56     printf("%d",ans);
57     return 0;
58 }
View Code

之所以可以像hzwer这样写是因为位置的奇偶性不同,前缀和的奇偶性肯定不同。

挖坑。

BZOJ3540: [Usaco2014 Open]Fair Photography

标签:des   style   blog   http   color   io   os   ar   for   

原文地址:http://www.cnblogs.com/zyfzyf/p/4006874.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!