Codeforces Round #600 (Div. 2) E. Antenna Coverage

Codeforces Round #600 (Div. 2) E. Antenna Coverage(dp)

Topic Link

Meaning of the questions:

m ants, is the position of each ant \ (x_i \) , is a fraction \ (S_I \) , coverage is \ ([x_i - S_I; x_i + S_I] \) , each coin can have ant \ (S_I \) + 1'd, seeking to cover the entire \ ([1; m] \ ) the least coin

Ideas:

\ (f [pos] [0 ] \) represents \ ([1, pos] \ ) is not covered with the least cost of the coin but also, \ (F [POS] [. 1] \) represents \ ([1, pos] \) minimum spend coins covered for each \ (pos \) enumeration of ants, there are these types of the following

  • \ (pos \) in (x_i \) \ right, and the ants \ (i \) extended to \ (pos \) left when already there are ants covered the ant \ (i \) extended to \ (pos \) when \ (f [pos] [1 ] = min (f [pos] [1], f [max (2 * x [j] -pos-1,0)] [1] + max (pos-x [j ] -s [j], 0) ); \)
  • \ (POS \) in \ (x_i \) to the right, and ant \ (I \) extended to \ (POS \) left when there is no ant cover, the ant \ (I \) extended to \ (POS \) when \ (f [pos] [1 ] = min (f [pos] [1], f [max (2 * x [j] -pos-1,0)] [0] + 1 + max (pos-x [ j] -s [j], 0 )); \)
  • \ (POS \) in \ (x_i \) left, simply extended \ (x_i \) left side \ (f [pos] [1 ] = min (f [pos] [1], min (f [ max (x [j] -s [ j] -1,0)] [1], f [max (x [j] -s [j], 0)] [0])); \)

    \ (f [i] [1 ] \) and \ (f [i] [0 ] \) difference is if the left side has ants covered, can go to cover at least the \ ([i-1, i ] \) costs

    Code:

#include <bits/stdc++.h>
using namespace std;
const int N = 200100;
int f[N][2],s[N],x[N];
int main()
{
    int n,m;
    cin >> n >> m;
    for (int i = 1; i <= m; i++) f[i][0]=i-1,f[i][1]=1<<30;
    f[0][1] = 0;
    for (int i = 1; i <= n; i++)
     {
        cin >> x[i] >> s[i];
     }
    for (int i = 1; i<= m; i++)
    for (int j = 1; j <= n;j++)
     if ( i >= x[j])
     {
        f[i][1] = min(f[i][1],f[max(2*x[j]-i-1,0)][1]+max(i-x[j]-s[j],0));
        f[i][1] = min(f[i][1],f[max(2*x[j]-i-1,0)][0]+1+max(i-x[j]-s[j],0));
     }
     else 
     {
       f[i][1] = min(f[i][1],f[max(x[j]-s[j]-1,0)][1]);
       f[i][1] = min(f[i][1],f[max(x[j]-s[j],0)][0]);
     }
    cout << f[m][1] << endl;
} 

Guess you like

Origin www.cnblogs.com/7osen/p/11875441.html