【ZOJ3953 The 17th Zhejiang University Programming Contest F】【贪心 or 费用流】Intervals 删最少线段使得每点最多2次覆盖

来源:互联网 发布:天津中年同志软件 编辑:程序博客网 时间:2024/05/05 04:24

Intervals

Time Limit: 1 Second      Memory Limit: 65536 KB      Special Judge

Chiaki has n intervals and the i-th of them is [liri]. She wants to delete some intervals so that there does not exist three intervals ab and c such that a intersects with bb intersects with c and c intersects with a.

Chiaki is interested in the minimum number of intervals which need to be deleted.

Note that interval a intersects with interval b if there exists a real number x such that la ≤ x ≤ ra and lb ≤ x ≤ rb.

Input

There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:

The first line contains an integer n (1 ≤ n ≤ 50000) -- the number of intervals.

Each of the following n lines contains two integers li and ri (1 ≤ li < ri ≤ 109) denoting the i-th interval. Note that for every 1 ≤ i < j ≤ nli ≠ lj or ri ≠ rj.

It is guaranteed that the sum of all n does not exceed 500000.

Output

For each test case, output an integer m denoting the minimum number of deletions. Then in the next line, output m integers in increasing order denoting the index of the intervals to be deleted. If m equals to 0, you should output an empty line in the second line.

Sample Input

1112 54 73 96 111 1210 158 1713 1816 2014 2119 22

Sample Output

43 5 7 10

Author: LIN, Xi
Source: The 17th Zhejiang University Programming Contest Sponsored by TuSimple

#include<stdio.h>#include<iostream>#include<string.h>#include<string>#include<ctype.h>#include<math.h>#include<set>#include<map>#include<vector>#include<queue>#include<bitset>#include<algorithm>#include<time.h>using namespace std;void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }#define MS(x, y) memset(x, y, sizeof(x))#define ls o<<1#define rs o<<1|1typedef long long LL;typedef unsigned long long UL;typedef unsigned int UI;template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b > a)a = b; }template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b < a)a = b; }const int N = 100005, M = N * 8, Z = 1e9 + 7, inf = 0x3f3f3f3f;template <class T1, class T2>inline void gadd(T1 &a, T2 b) { a = (a + b) % Z; }int casenum, casei;int n, g;int l[N], r[N];int w[N];vector< pair<int, int> >a[N];bool del[N];struct A{int r, o;bool operator < (const A & b)const{if(r != b.r)return r < b.r;return o < b.o;}};set< A >sot;namespace FLOW{int ST, ED;int first[N], id;int w[M], cap[M], cost[M], nxt[M];//边包括了(抵达点,容量,单位流量成本,下条边)的信息int f[N];//f[x]表示在残量网络下,从源点到达x的最小距离int pe[N];//pe[x]记录流向x的前驱边bool e[N];//e[x]是判定点x是否在SPFA队列中的辅助数组void ins(int x, int y, int cap_, int cost_){w[++id] = y;cap[id] = cap_;cost[id] = cost_;nxt[id] = first[x];first[x] = id;w[++id] = x;cap[id] = 0;cost[id] = -cost_;nxt[id] = first[y];first[y] = id;}queue<int>q;void inq(int x, int cost_, int pe_){if (cost_ >= f[x])return;//单位流量费用更大,没有更新意义f[x] = cost_;//单位流量费用小的条件下做更新pe[x] = pe_;//然后记录上一条边if (e[x])return; e[x] = 1;//SPFA的入队标记以防止重复入队做冗余更新q.push(x);}bool spfa(){//MS(f, 63); MS(e, 0);memset(f, 63, ED + 2 << 2);memset(e, 0, ED + 2);cap[0] = inf;inq(ST, 0, 0);while (!q.empty()){int x = q.front(); q.pop(); e[x] = 0;for (int z = first[x]; z; z = nxt[z]){if (cap[z])inq(w[z], f[x] + cost[z], z);}}return f[ED] < inf;}//MCMF利用dfs方式做回溯的高效写法——bool vis[N];int dfs(int x, int all){if (x == ST)return all;int use = 0;vis[x] = 1;for (int z = first[x]; z; z = nxt[z])if (cap[z ^ 1]){int y = w[z];if (!vis[y] && f[y] + cost[z ^ 1] == f[x]){int tmp = dfs(y, min(cap[z ^ 1], all - use));cap[z ^ 1] -= tmp;cap[z] += tmp;use += tmp;if (use == all)break;}}return use;}int MCMF(){int maxflow = 0;int mincost = 0;while (spfa()){//高效写法int flow;while (MS(vis, 0), flow = dfs(ED, inf)){maxflow += flow;mincost += f[ED] * flow;}}return mincost;}void solve(){ST = 0; ED = g + 1;memset(first, 0, ED + 2 << 2); id = 1;for (int i = 0; i <= g; ++i){ins(i, i + 1, 2, 0);}int o = id;for (int i = 1; i <= n; ++i){ins(l[i], r[i] + 1, 1, -1);}printf("%d\n", n - -MCMF());for (int i = 1; i <= n; ++i){if (cap[o + i * 2] == 0)printf("%d ", i);}puts("");}}int main(){scanf("%d", &casenum);for (casei = 1; casei <= casenum; ++casei){scanf("%d", &n); g = 0;for (int i = 1; i <= n; ++i){scanf("%d%d", &l[i], &r[i]);w[++g] = l[i];w[++g] = r[i];del[i] = 0;}sort(w + 1, w + g + 1);g = unique(w + 1, w + g + 1) - w - 1;for (int i = 1; i <= g; ++i)a[i].clear();for (int i = 1; i <= n; ++i){l[i] = lower_bound(w + 1, w + g + 1, l[i]) - w;r[i] = lower_bound(w + 1, w + g + 1, r[i]) - w;a[l[i]].push_back({ r[i], i });}//把注释去掉可以实现网络流解法//FLOW::solve();//continue;sot.clear();int ans = 0;for (int i = 1; i <= g; ++i){while (sot.size() && sot.begin()->r < i)sot.erase(sot.begin());for (auto it : a[i]){sot.insert({ it.first, it.second });}while (sot.size() >= 3){int o = (--sot.end())->o;del[o] = 1;sot.erase(--sot.end());++ans;}}printf("%d\n", ans);for (int i = 1, cnt = 0; i <= n; ++i)if (del[i]){printf("%d", i);if (++cnt != ans)putchar(' ');}puts("");}return 0;}/*【trick&&吐槽】这是这场比赛自己拿到的第二个一血,自己确实比较喜欢做贪心题啦:D~~~【题意】http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3953让你删掉最少的区间数,使得任意一点最多只被2个线段覆盖【分析】显然,问题是,我们假设把线段按照左端点排序为A,B,C,我们使得A与C不相交就好啦。按照这个逻辑,我们对于任意一个点,如果其被覆盖的次数达到三次,我们只要删掉右端点最右的一条线段即可。其一来不干扰之前的最优解,而来使得后续的最优解尽可能优,达成了全局最优的目标。于是——<1>离散化<2>扫描<3>STL贪心维护就可以AC啦!【时间复杂度&&优化】这道题还有另外一种做法,我们按照K覆盖的原理,采用最大费用可行流的方法。*/


0 0