poj 1201 interval 差分约束/贪心+线段树区间更新
2015-02-01 15:01
357 查看
Intervals
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
Sample Output
Source
Southwestern Europe 2002
差分约束系统的题。。。wa了很多次因为没注意清楚约束条件和隐藏条件
题目要求的是区间 [ai,bi]中至少有ci个数,一开始直接转化为 Sbi-Sai >= ci 个数,Sn代表前n个数中包含多少个,但其实Sbi-Sai包含的是(ai,bi],所以应该用区间Sbi-S(ai-1)。
但是ai有可能为0,所以统一都+1区间变为S(bi+1)-Sai>=ci
根据这个问题,加进去之间的点还必须满足一些隐藏条件:
1.对于a<=b, Sa <= Sb。
2.同时一个区间[a,b]内最多也只能包含b-a+1个, 所以还有 Sb+1-Sa >= b-a+1 (这里区间是上面说的+1后)
确定三个条件后,建立图论模型求最长路径 SaI+ci <= Sbi,从ai到bi连接一条权为ci的边
还有另外一种做法就是贪心+线段树,按每个区间的末端点从小到大排序。从第一个区间开始,每个区间检查有多少个数,小于c个,则从区间末端一直往前增加直到满足c个为止,这是O(n^2)。用线段树优化就可以达到O(nlogn),在进行区间更新时,优先选择先更新靠右的区间,同时返回增加了多少个数,然后再继续更新左边的区间,具体写在注释里了。
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 22337 | Accepted: 8423 |
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
Southwestern Europe 2002
差分约束系统的题。。。wa了很多次因为没注意清楚约束条件和隐藏条件
题目要求的是区间 [ai,bi]中至少有ci个数,一开始直接转化为 Sbi-Sai >= ci 个数,Sn代表前n个数中包含多少个,但其实Sbi-Sai包含的是(ai,bi],所以应该用区间Sbi-S(ai-1)。
但是ai有可能为0,所以统一都+1区间变为S(bi+1)-Sai>=ci
根据这个问题,加进去之间的点还必须满足一些隐藏条件:
1.对于a<=b, Sa <= Sb。
2.同时一个区间[a,b]内最多也只能包含b-a+1个, 所以还有 Sb+1-Sa >= b-a+1 (这里区间是上面说的+1后)
确定三个条件后,建立图论模型求最长路径 SaI+ci <= Sbi,从ai到bi连接一条权为ci的边
#include <iostream> #include <cstring> #include <cstdio> #include <queue> using namespace std; #define maxn 50005 #define inf 0x3f3f3f3f struct edge { edge(){} edge(int v_, int w_, int n) { v=v_; w=w_; next=n;} int v; int w; int next; }; edge g[4*maxn]; int first[maxn], top; void add(int u, int v, int w) { g[top] = edge(v, w, first[u]); first[u] = top++; } int d[maxn]; int in[maxn]; void spfa(int s) //因为图是连通的,所以可以用spfa { memset(d, -inf, sizeof(d)); memset(in, 0, sizeof(in)); queue<int> que; que.push(s); d[s] = 0; while(!que.empty()){ int u = que.front(); que.pop(); in[u] = 0; for(int i = first[u]; i != -1; i = g[i].next){ int v = g[i].v, w= g[i].w; if(d[u]+w > d[v]){ //求最长路径 d[v] = d[u]+w; if(!in[v]){ in[v] = 1; que.push(v); } } } } } int h[maxn]; //记录用到的点 int arr[maxn]; //用到的点从小到大存储 int main() { int n; while(~scanf("%d", &n)){ memset(h, 0, sizeof(h)); memset(first, -1, sizeof(first)); int s, e; int a, b, w; for(int i = 0; i < n; i++){ scanf("%d%d%d", &a, &b, &w); add(a,b+1,w); h[a]=h[b+1]=1; } int cnt = 0; for(int i = 0; i < maxn; i++) if(h[i]) arr[cnt++] = i; //arr中存储点,从小到大 for(int i = 1; i < cnt; i++){ add(arr[i-1], arr[i], 0); //相邻两个点满足Sa+0 <= Sb,从a到b一条边边权为0 add(arr[i], arr[i-1], -(arr[i]-arr[i-1])); //满足最多不超过b-a+1, Sb+1-Sa<= b-a+1( Sb-(b-a+1)<=Sa) } //从b到a连接边权-(b-a+1) spfa(arr[0]); printf("%d\n", d[arr[cnt-1]]); } return 0; }
还有另外一种做法就是贪心+线段树,按每个区间的末端点从小到大排序。从第一个区间开始,每个区间检查有多少个数,小于c个,则从区间末端一直往前增加直到满足c个为止,这是O(n^2)。用线段树优化就可以达到O(nlogn),在进行区间更新时,优先选择先更新靠右的区间,同时返回增加了多少个数,然后再继续更新左边的区间,具体写在注释里了。
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> using namespace std; #define maxn 50005 int n; struct node //线段树节点 { node() {} node(int ll, int rr) { l = ll; r = rr;} int l, r; int sum; //sum表示该节点区间上一共有多少个数了 int inc; }; node tr[4*maxn]; void build(int rt, int l, int r) { tr[rt] = node(l, r); tr[rt].inc = tr[rt].sum = 0; if(l == r) return; int m = l+(r-l)/2; build(2*rt, l, m); build(2*rt+1, m+1, r); } int query(int rt, int ll, int rr) { int l = tr[rt].l, r = tr[rt].r; int &sum = tr[rt].sum; int &inc = tr[rt].inc; int lc = 2*rt, rc = 2*rt+1; if(l == ll && r == rr) return sum; else{ if(inc){ tr[lc].inc = 1; tr[rc].inc = 1; tr[lc].sum = tr[lc].r-tr[lc].l+1; tr[rc].sum = tr[rc].r-tr[rc].l+1; inc = 0; } } int m = l+(r-l)/2; if(rr <= m) return query(lc, ll, rr); else if(ll > m) return query(rc, ll, rr); else return query(lc, ll, m)+query(rc, m+1, rr); } int seg_modify(int rt, int ll, int rr, int v) //v表示需要增加的个数 { int l = tr[rt].l, r = tr[rt].r; int &sum = tr[rt].sum; int &inc = tr[rt].inc; int lc = 2*rt, rc = 2*rt+1; if(sum == r-l+1) return 0; int ret; if(l == ll && r == rr && sum+v>=r-l+1){ //如果需要刚好对应节点区间,且增加的个数使得该区间满了,则直接更新该节点整个区间 ret = (r-l+1)-sum; //返回增加的个数 sum = (r-l+1); inc = 1; return ret; } else{ if(inc){ tr[lc].inc = 1; tr[rc].inc = 1; tr[lc].sum = tr[lc].r-tr[lc].l+1; tr[rc].sum = tr[rc].r-tr[rc].l+1; inc = 0; } } int m = l+(r-l)/2; if(rr <= m) seg_modify(lc, ll, rr, v); else if(ll > m) seg_modify(rc, ll, rr, v); else{ int left = v-seg_modify(rc, m+1, rr, v); //优先更新靠右边的区间,然后计算还要增加的个数 if(left) seg_modify(lc, ll, m, left); } ret = tr[lc].sum+tr[rc].sum - tr[rt].sum; tr[rt].sum += ret; return ret; } struct range //保存输入区间的节点 { int l, r; int c; bool operator <(const range& t) const { return r < t.r; } }; range a[maxn]; int main() { while(~scanf("%d", &n)){ for(int i = 0; i < n; i++) scanf("%d%d%d", &a[i].l, &a[i].r, &a[i].c); sort(a, a+n); int ub = a[n-1].r; build(1, 0, ub); for(int i = 0; i < n; i++){ int cnt = query(1, a[i].l, a[i].r); if(cnt < a[i].c) seg_modify(1, a[i].l, a[i].r, a[i].c-cnt); } printf("%d\n", query(1, 0, ub)); } return 0; }
相关文章推荐
- POJ 1201 Intervals (区间差分约束)
- [差分约束]poj 1201 Interval
- POJ 1201 Intervals [差分约束]
- POJ 3468 :A Simple Problem with Integers——区间更新线段树经典题目
- POJ 1201 Intervals (差分约束,SPFA)
- POJ 1201 Intervals (差分约束)
- Poj 3468 A Simple Problem with Integers (线段树 区间更新 区间求和)
- poj 3225【线段树--区间更新,XOR,区间询问】
- POJ 1195 Mobile phones 线段树 二维线段树 单点更新 区间求和
- poj 3225 线段树区间更新,区间询问 Help with Intervals
- 线段树 : 区间更新 poj 3468 示例
- POJ-1436 线段树 区间更新
- poj 1201 Intervals(第一道差分约束题)
- POJ 3264 Balanced Lineup 线段树 单点更新 求区间最值
- poj 1201 Intervals 差分约束 + spfa
- poj 1201 Intervals( spfa + 差分约束)
- POJ-3468(线段树_区间更新)
- POJ 1201 Intervals(差分约束)
- 线段树 : 区间更新 poj 3468 示例
- POJ 1201 Intervals (差分约束)