Minimum number of people lie (dp)

https://ac.nowcoder.com/acm/contest/1168/H

Meaning of the questions: n students, Deng Zhicong want to know these students' test case, a so called these students called to the office to ask them, but some students did not speak the truth, the i-th student, said: "There ai personal scores than me high, bi personal scores lower than I am. "Deng Zhicong want to know at least a few students do not have to tell the truth, can you help him? (May have the same score)

Solution: Reverse thinking seek a maximum number of people did not lie, with the interval represents the position in which the students, the interval represents the number of the section.

dp does not solve the overlap period can be the sum of the maximum.

//#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <string>
#include <stdio.h>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <string.h>
#include <vector>
#define ME(x , y) memset(x , y , sizeof(x))
#define SF(n) scanf("%d" , &n)
#define rep(i , n) for(int i = 0 ; i < n ; i ++)
#define INF  0x3f3f3f3f
#define mod 20191117
#define PI acos(-1)
using namespace std;
typedef long long ll ;

map<pair<int , int > , int>m;
vector<int>ans[100100];
int dp[100100];

int main ()
{
    int n ;
    scanf("%d" , &n);
    for(int i = 0 ; i < n ; i++)
    {
        int x , y ;
        scanf("%d%d" , &x , &y);
        int l = x + 1; // left high
        int r = n - y; // low in the right
        if(l > r) continue;
        if(m[make_pair(l , r)] < r - l + 1)
            // value representation interval; m [make_pair (l, r)] ++
        if(m[make_pair(l , r)] == 1) ans[r].push_back(l);
    }
    for(int i = 1 ; i <= n ; i++)
    {
        dp [i] = dp [i -1]; // i to the right border.
        for (auto j: ans [i]) left margin // ans [i] in the second dimension element
        {
            dp[i] = max(dp[i] , dp[j-1] + m[make_pair(j , i)]);
        }
    }
    cout << n - dp[n] << endl ;
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/nonames/p/11932919.html