POJ-1716 Integer Intervals (differential constraint system)

Source: Internet
Author: User

POJ-1716 Integer Intervals (differential constraint system)

Given N intervals, you need to find the number of M, which satisfies the fact that there are at least two different numbers in each interval.

Solution: I still do not know much about the difference constraint system, and I am not very good at mathematics.
Drawing on others' ideas, I feel a little bit DP thinking
Set d [I] to [0, I-1]. The number of d [I] In this interval meets the requirements.
Given a range [a, B], d [B + 1]-d [a]> = 2 (B + 1 because B is also included in the range)
Convert it to d [a]-d [B + 1] <=-2, which is the first sub-Statement
Then, in the interval [I, I + 1 ],
D [I + 1]-d [I]> = 0 to d [I]-d [I + 1] <= 0
D [I + 1]-d [I] <= 1
Three sub-charts
Use Max (rightmost boundary + 1) as the source point for SPFA
The final answer is d [Max]-d [Min].

#include 
  
   #include 
   
    #include 
    
     using namespace std;    #define N 10010#define M 30010#define INF 0x3f3f3f3fstruct Edge{    int dist, to, next;}E[M];int head[N], d[N], n, tot;bool vis[N];void AddEdge(int u, int v, int dist) {    E[tot].to = v;    E[tot].dist = dist;    E[tot].next = head[u];    head[u] = tot++;}void SPFA(int s) {    for (int i = 0; i <= s; i++) {        d[i] = INF;        vis[i] = 0;    }    queue
     
       q;    q.push(s);    d[s] = 0;    while (!q.empty()) {        int u = q.front();        q.pop();        vis[u] = false;        for (int i = head[u]; i != -1; i = E[i].next) {            int v = E[i].to;            if (d[v] > d[u] + E[i].dist) {                d[v] = d[u] + E[i].dist;                if (!vis[v]) {                    vis[v] = true;                    q.push(v);                }            }        }    }}void solve() {    memset(head, -1, sizeof(head));    tot = 0;    int Min = INF, Max = -INF;    int u, v;    for (int i = 0; i < n; i++) {        scanf(%d%d, &u, &v);        Min = min(Min, u);        Max = max(Max, v + 1);        AddEdge(v + 1, u, -2);    }    for (int i = 0; i < Max; i++) {        AddEdge(i, i + 1, 1);        AddEdge(i + 1, i, 0);    }    SPFA(Max);    printf(%d, d[Max] - d[Min]);}int main() {    while (scanf(%d, &n) != EOF) {        solve();    }    return 0;}
     
    
   
  

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.