BZOJ3540: [Usaco2014 Open]Fair Photography

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

Silver By liyizhen2

题解:

碰上一道好题。想法和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

 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 }

代码:hzwer

 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 }

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

挖坑。

时间: 2024-12-11 15:24:05

BZOJ3540: [Usaco2014 Open]Fair Photography的相关文章

bzoj 3540: [Usaco2014 Open]Fair Photography

3540: [Usaco2014 Open]Fair Photography 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 eithe

[BZOJ3535][Usaco2014 Open]Fair Photography

试题描述 FJ'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 (an integer in the range 1..8). No two c

USACO 2014 US Open Fair Photography /// 技巧

题目大意: 给定n头奶牛 给定n头奶头所在位置和品种 品种只有G H两种 求一段区间的长度 要求区间内包含的品种满足各品种的数量相同 将一个品种的值设为1 另一个设为-1 假设 i<j 而 1~i的奶牛前缀和 与 1~j的奶牛前缀和 相等 说明 i+1~j 的奶牛总和为0 即两种奶牛的数量相同 #include <bits/stdc++.h> using namespace std; #define LL long long #define INF 0x3f3f3f3f #define

Photography theory: a beginner&#39;s guide(telegraph.co.uk)

By Diane Smyth, Tim Clark, Rachel Segal Hamilton and Lewis Bush 11:00AM BST 09 Jun 2014 Have you read the Bible cover to cover? Probably not, but it's also fair to assume you know the basic plot, the central characters and a few choice quotes. This i

bzoj1664[Usaco2006 Open]County Fair Events 参加节日庆祝*

bzoj1664[Usaco2006 Open]County Fair Events 参加节日庆祝 题意: 有N个节日,每个节日有个开始时间,及持续时间.牛想尽可能多的参加节日,问最多可以参加多少.注意牛的转移速度是极快的,不花时间,且节日必须完整参加.N≤10000,开始时刻和持续时间≤100000. 题解: dp.设f[i]表示i时刻到最后时刻最多可以参加多少节日.则f[i]=max(f[i+1],f[range[j].r+1],j为时刻i开始的节日). 代码: 1 #include <cs

3891: [Usaco2014 Dec]Piggy Back

3891: [Usaco2014 Dec]Piggy Back Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 116  Solved: 92[Submit][Status][Discuss] Description Bessie and her sister Elsie graze in different fields during the day, and in the evening they both want to walk back

PatentTips - Fair scalable reader-writer mutual exclusion

BACKGROUND The present invention relates generally to multithreaded programming and, more specifically, to mutual exclusion of readers and writers in a multithreaded programming environment. Mutual exclusion is a programming technique that ensures th

[Usaco2014 Mar]Sabotage

[Usaco2014 Mar]Sabotage 题目 Farmer John"s arch-nemesis, Farmer Paul, has decided to sabotage Farmer John"s milking equipment! The milking equipment consists of a row of N (3 <= N <= 100,000) milking machines, where the ith machine produces

BZOJ 3446: [Usaco2014 Feb]Cow Decathlon( 状压dp )

水状压dp. dp(x, s) = max{ dp( x - 1, s - {h} ) } + 奖励(假如拿到的) (h∈s). 时间复杂度O(n * 2^n) ---------------------------------------------------------------------------------- #include<bits/stdc++.h> #define rep(i, n) for(int i = 0; i < n; ++i) #define clr(x