CF1253E Antenna Coverage(DP)

This question proved the correctness of difficulty.

Order \ (F_i \) represents \ ([1, i] \ ) minimum cost is completely covered. The answer is \ (f_m \) .

First found that adding an interval \ ([0,0] \) does not affect the answer. So \ (f_i \) of the initial value can be set to \ (i \) . (This is very important, not the is not right!)

Transfer, if \ (I \) has been completely covered some initial interval, then from the \ (f_ {i-1} \) to transfer.

Then enumerate each interval, if the right point in the interval \ (I \) left, this interval is calculated just enough to cover the expansion (I \) \ left end point after. I.e. from \ (f _ {\ max ( 0, l [j] - (ir [j]))} + ir [j] \) is transferred by.

Each section is a note of what will be fried to the expanded state, the expansion starts from the initial interval.

Time complexity \ (O (nm) \) .

He began to justify.

Consider first prove only covered the left side of the interval, no need to consider the right.

In fact, the section covering the right also been considered, but when transferred to skip these points (in this section are after expansion). So do not control.

Then prove began to expand from an initial range is the optimal solution.

If you need to continue to expand on the basis of the expansion of the range, indicating that the expansion to the point \ (i \) must be in the last expansion to the point \ (j \) the right to expand into \ (i \) after the left point interval must have skipped \ (J \) . And we are the last to use the \ (i \) state (because of the need to continue to expand), so the middle of this first expansion is not necessary.

So this can not happen.

The next expansion proved just enough to cover \ (i \) is the optimal solution, which is the optimal solution does not need to expand to cover more than \ (i \) a little bit.

If you need more expansion, it must be because you can cover more points to the left, to the left so that the interval is shorter (or else to cover more than \ (i \) location \ (f_i \) is completely unnecessary).

However, the addition of the interval \ ([0,0] \) (yes, its role in this), there must be \ (+ F_ {I}. 1 \ Le F_i +. 1 \) (because the cover to \ (I \) interval can be extended a further grid).

So the point after the transfer of the right to skip interval should be more the better. That is no need to extend to the \ (i \) on the right.

So this is right up.


Code

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
const int maxn=100010;
#define MP make_pair
#define PB push_back
#define lson o<<1,l,mid
#define rson o<<1|1,mid+1,r
#define FOR(i,a,b) for(int i=(a);i<=(b);i++)
#define ROF(i,a,b) for(int i=(a);i>=(b);i--)
#define MEM(x,v) memset(x,v,sizeof(x))
inline ll read(){
    char ch=getchar();ll x=0,f=0;
    while(ch<'0' || ch>'9') f|=ch=='-',ch=getchar();
    while(ch>='0' && ch<='9') x=x*10+ch-'0',ch=getchar();
    return f?-x:x;
}
int n,m,x[maxn],s[maxn],f[maxn]; 
int main(){
    n=read();m=read();
    FOR(i,1,n) x[i]=read(),s[i]=read();
    f[0]=0;
    FOR(i,1,m){
        f[i]=i;
        bool flag=false;
        FOR(j,1,n) if(x[j]+s[j]>=i && x[j]-s[j]<=i) flag=true;
        if(flag) f[i]=f[i-1];
        FOR(j,1,n) if(x[j]+s[j]<i) f[i]=min(f[i],f[max(0,2*x[j]-i-1)]+i-(x[j]+s[j]));
    }
    printf("%d\n",f[m]);
}

Guess you like

Origin www.cnblogs.com/1000Suns/p/11919065.html