P3105 [USACO14OPEN] fair photography (Correct answer is messing around, I was half a) (+ dichotomous answer summary)

 

 Simplification usual meaning of the questions:

Given a 01 range, can be changed to a 0, Q is equal to the number of the longest length section 01 therein.

It is easy to think of the amount of prefixes and put into something w 1, h inflicted -1, then seek prefix and then fucks on the line.

But it has been less direct but I would fucks think half.

It is easy to easy to think: The answer is monotonic, that is:

The answer is definitely not monotonous increase of

How to understand it?

Is this: there must be an interval length, making it -1 is not the greatest, + 1 does not exist, this is what we're looking for something

And check it very clear ideas:

Enumeration left point, then (assuming the length of the interval) The two separated mid to find the longest segment, which is then determined whether the number of white non-negative even number of cow:

If the change, then white cow, white -1, +1 flower, the number of such cows will spend more than white cow 2

If there is a range meet the above criteria, then try to expand the range (half in l = mid), it does not meet the narrow range until the search to answer.

have to be aware of is:

If you found to reach the final rx-lx-half of the length of the interval, the need for direct break out, because the answer here is not legitimate.

Complexity of a single check is O (n), because only the endpoints lr traversed again.

Complexity is bipartite O (logn)

So overall complexity is O (n logn)

Code no big difficulty:

#include<bits/stdc++.h>
using namespace std;
const int maxn=2e6+10;
int n;
struct node
{
    int x,co;
}a[maxn];
int sum[maxn];
int ans;
int f[maxn];
bool check(int x)
{
    int r=0;
    for(int l=1;l<=n;l++)
    {
        while(a[r].x-a[l].x<x&&r<n)r++;
        if(a[r].x-a[l].x<x)break;
        if((sum[r]-sum[l-1])%2==0&&sum[r]-sum[l-1]>=0)return 1;
    }
    return 0;
}
bool cmp(node a,node b)
{
    return a.x<b.x;
}
int main ()
{
    //freopen("testdata.in","r",stdin);
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        int x;
        char f;
        cin>>a[i].x>>f;
        a [i] .co = f == ' W ' ? 1 - 1 ;
    }
    sort(a+1,a+n+1,cmp);
    for(int i=1;i<=n;i++)
    sum[i]=sum[i-1]+a[i].co;
    int l=0,r=2147483647;
    while(l<r-1)
    {
        int mid=l+r>>1;
        if(check(mid)==0)
        r=mid;
        else
        l=mid;
    }
    //while(check(l))l++;
    printf("%d",l);
    return 0;
}

Here to talk about half the answer:

In general, half of the answer commonly used in:

  1. Maximum and minimum / maximum values ​​to find a minimum of things
  2. There monotony of looking for answers

And I met almost half in three (mainly check type):

  1. Jump Stone Type (violent judgment)
  2. The title (slightly lower conversion)
  3. Portal (need to push equation)

But the general feeling is almost a stone and jump to find conditions, squeeze out a one-dimensional complexity of O (n), making it into a log.

The dichotomy is very common, very good, want to be like dp as often thought.

Here bipartite board (while inside)

Dichotomous answer (positive integer):

while(l<r-1)
    {
        int mid=l+r>>1;
        if(check(mid)==0)
        r=mid;
        else
        l=mid;
    }
    the while (the Check (L)) L ++; (because the output of the left point, but only if last updated r, then the answer is not necessarily correct, after all, a positive integer error is quite large)
View Code

Real domain dichotomy:

while((r-l)>0.000000001)
{
    double mid=(l+r)/2;
    if(check(mid)==0)
        l=mid;
        else
            r=mid;
}只要精度不出锅应该都没问题
View Code

(完)

 

Guess you like

Origin www.cnblogs.com/ajmddzp/p/11768982.html