• poj 1201 Intervals 解题报告


     

    Time Limit: 2000MS   Memory Limit: 65536KB   64bit IO Format: %lld & %llu

     Status

    Description

    You are given n closed, integer intervals [ai, bi] and n integers c1, ..., cn. 
    Write a program that: 
    reads the number of intervals, their end points and integers c1, ..., cn from the standard input, 
    computes the minimal size of a set Z of integers which has at least ci common elements with interval [ai, bi], for each i=1,2,...,n, 
    writes the answer to the standard output. 

    Input

    The first line of the input contains an integer n (1 <= n <= 50000) -- the number of intervals. The following n lines describe the intervals. The (i+1)-th line of the input contains three integers ai, bi and ci separated by single spaces and such that 0 <= ai <= bi <= 50000 and 1 <= ci <= bi - ai+1.

    Output

    The output contains exactly one integer equal to the minimal size of set Z sharing at least ci elements with interval [ai, bi], for each i=1,2,...,n.

    Sample Input

    5
    3 7 3
    8 10 3
    6 8 1
    1 3 1
    10 11 1

    Sample Output

    6

    Source

    ——————————————————我是华丽丽的分割线——————————————————

    差分约束系统题目。

    约束条件如下:

    设S[i]为集合Z中小于等于i的元素个数,即S[i]=|{s|s属于Z,s<=i}|。则:

    (1) S[b(i)]-S[a(i-1)]>=c(i)   ------------> S[a(i-1)]-S[b(i)]<=-c(i)

    (2) S[i]-S[i-1]<=1。

    (3) S[i]-S[i-1]>=0,即S[i-1]-S[i]<=0.

    设所有区间极右端点为mx,极左端点为mn 则 ans=min(s[mx]-s[mn-1]) 即求源点s[mx]与s[mn-1]之间的最短路 此题得解。

    改进后方法如下: (1)先只用条件(1)构图,各顶点到源点的最短距离初始为0。 (2)即刻用Bellman-ford算法求各顶点到源点的最短路径。 p.s.在每次循环中,条件1判完后判断2和3.

    2的判断: s[i]<=s[i-1]+1等价于s[i]-s[mx]<=s[i-1]-s[mx]+1 设dist[i]为源点mx到i的最短路径,则s[i]-s[mx]即为dist[i],s[i-1]-s[mx]+1即为dist[i-1]+1。 即若顶点i到源点的最短路径长度大于i-1到源点的最短路径长度+1,则dist[i]=dist[i-1]+1。

    3的判断: s[i-1]<=s[i]等价于s[i-1]-s[mx]<=s[i]-s[mx] s[i]-s[mx]即为dist[i],s[i-1]-s[mx]即为dist[i-1]。 即若顶点i-1到源点的最短路径长度大于i到源点的最短路径长度,则dist[i-1]=dist[i]。

      1 #include<iostream>
      2 #include<cstdio>
      3 #include<cstring>
      4 #include<cmath>
      5 #include<algorithm>
      6 #include<queue>
      7 #include<cstdlib>
      8 #include<iomanip>
      9 #include<cassert>
     10 #include<climits>
     11 #include<vector>
     12 #include<list>
     13 #include<map>
     14 #define maxn 1000001
     15 #define F(i,j,k) for(int i=j;i<=k;i++)
     16 #define M(a,b) memset(a,b,sizeof(a))
     17 #define FF(i,j,k) for(int i=j;i>=k;i--)
     18 #define inf 0x7fffffff
     19 #define maxm 2016
     20 #define mod 1000000007
     21 //#define LOCAL
     22 using namespace std;
     23 int read(){
     24     int x=0,f=1;char ch=getchar();
     25     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
     26     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
     27     return x*f;
     28 }
     29 int n,m;
     30 int lsh=inf,rsh;
     31 struct EDGE
     32 {
     33     int from;
     34     int to;
     35     int value;
     36     int next;
     37 }e[maxn];
     38 int head[maxn];
     39 int tot;
     40 inline void addedge(int u,int v,int w)
     41 {
     42     tot++;
     43     e[tot].from=u;
     44     e[tot].to=v;
     45     e[tot].value=w;
     46     e[tot].next=head[u];
     47     head[u]=tot;
     48 }
     49 int f[maxn];
     50 queue<int> q;
     51 bool vis[maxn];
     52 int d[maxn],in[maxn];
     53 bool ford() 
     54 {
     55      int i,t;
     56      bool flag=true;
     57      while(flag){
     58          flag=false;
     59          F(i,1,n){
     60              t=d[e[i].from]+e[i].value;
     61              if(d[e[i].to]>t){
     62                  d[e[i].to]=t;
     63                  flag=true;
     64              }
     65          }
     66          F(i,lsh,rsh){
     67              t=d[i-1]+1;
     68              if(d[i]>t){
     69                  d[i]=t;
     70                  flag=true;
     71              }
     72          }
     73          FF(i,rsh,lsh){
     74              t=d[i];
     75              if(d[i-1]>t){
     76                  d[i-1]=t;
     77                  flag=true;
     78              }
     79          }
     80      }
     81      return true;
     82 }
     83 int main()
     84 {
     85     std::ios::sync_with_stdio(false);//cout<<setiosflags(ios::fixed)<<setprecision(1)<<y;
     86     #ifdef LOCAL
     87     freopen("data.in","r",stdin);
     88     freopen("data.out","w",stdout);
     89     #endif
     90     cin>>n;M(head,-1);
     91     F(i,1,n){
     92         int a,b,c;
     93         cin>>a>>b>>c;
     94         addedge(b,a-1,-c);
     95         if(lsh>a) lsh=a;
     96         if(rsh<b) rsh=b;
     97     }
     98     ford();
     99     cout<<d[rsh]-d[lsh-1]<<endl;
    100     return 0;
    101 }
    102 /*
    103 5
    104 3 7 3
    105 8 10 3
    106 6 8 1
    107 1 3 1
    108 10 11 1
    109 */
    poj 1201
  • 相关阅读:
    Java中Comparable与Comparator的区别
    LeetCode[5] 最长的回文子串
    LeetCode[3] Longest Substring Without Repeating Characters
    LeetCode 7. Reverse Integer
    统计单词出现的次数
    System.arraycopy()和Arrays.copyOf()的区别
    SyncToy
    查看端口占用及进程号
    TCP协议
    python 的日志logging模块学习
  • 原文地址:https://www.cnblogs.com/SBSOI/p/5842298.html
Copyright © 2020-2023  润新知