zoukankan      html  css  js  c++  java
  • Intervals II

    题目描述

    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.

    输入

    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.

    输出

    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.

    样例输入

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

    样例输出

    6

     题意:求符合题意的最小集合的元素个数  
    题目要求的是求的最短路,  
    则对于 不等式  f(b)-f(a)>=c,建立 一条 b 到 a 的边 权值为 c,则求的最长路 即为 最小值(集合)  
    并且有隐含条件:0<=f(a)-f(a-1)<=1  则有边权关系(a,a-1,0)以及(a-1,a,-1);  
    将源点到各点的距离初始化为INF(无穷大),其中之1为0,最终求出的最短路满足 它们与该点之间相互差值最大  
    差分约束  
     在实际的应用中,一般使用SPFA(Shortest Path Fast Algorithm)算法来实现。  
      差分约束系统中源点到每个点的距离确定  
      关于Dist[]的初始化化  
      1.如果将源点到各点的距离初始化为0,最终求出的最短路满足 它们之间相互最接近了  
      2.如果将源点到各点的距离初始化为INF(无穷大),其中之1为0,最终求出的最短路满足 它们与该点之间相互差值最大。  
      3.差分约束系统的确立要根据自己确定的约束条件,从约束点走向被约束点  
      连边一般有两种方法,第一种是连边后求最长路的方法,第二种是连边后求最短路的方法。  
      例:d[x]-d[y]>=Z  
      如果想连边后求最长路 那么将不等式变形为这种形式 d[x]>=d[y]+z y---x连一条权值为z的边  
      求最短路则变形成d[y]<=d[x]-z x---y连一条权值为-z的边。  
      如果是别的不等式,也可以根据情况变形。但是要保证的是 两个变量(x,y)的系数一定要是正的。而常量则不一定。  
    第一:  
    感觉难点在于建图  
    第二:  
    ①:对于差分不等式,a - b <= c ,建一条 b 到 a 的权值为 c 的边,求的是最短路,得到的是最大值  
    ②:对于不等式 a - b >= c ,建一条 b 到 a 的权值为 c 的边,求的是最长路,得到的是最小值  
    ③:存在负环的话是无解  
    ④:求不出最短路(dist[ ]没有得到更新)的话是任意解  
    第三:  
    一种建图方法:  
    设x[i]是第i位置(或时刻)的值(跟所求值的属性一样),那么把x[i]看成数列,前n项和为s[n],则x[i] = s[i] - s[i-1];  
    那么这样就可以最起码建立起类似这样的一个关系:0 <= s[i] - s[i-1] <= 1; 

  • 相关阅读:
    CSS资料:IE8 CSS hack
    作为前端制作师你必须知道的事情!
    TABLE的1PT边框
    IE6 png图片的支持
    开源公司IronTec将推动PHP进驻Android平台
    终于解决了FLASH 播放器的问题了
    Windows7 添加快速启动栏
    不能调试存储过程的解决方法
    给Asp.Net Forums的后台管理菜单做一个树形外衣
    Asp.net Forums与Cnforums研究文章集合
  • 原文地址:https://www.cnblogs.com/Accpted/p/11433356.html
Copyright © 2011-2022 走看看