BZOJ3540: [Usaco2014 Open]Fair Photography
3540: [Usaco2014 Open]Fair Photography
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 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).
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
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
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
这个是经典题吧。。。把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这样写是因为位置的奇偶性不同,前缀和的奇偶性肯定不同。
挖坑。