POJ2481-Cows
2017-07-22 10:41
274 查看
Cows
Description
Farmer John's cows have discovered that the clover growing along the ridge of the hill (which we can think of as a one-dimensional number line) in his field is particularly good.
Farmer John has N cows (we number the cows from 1 to N). Each of Farmer John's N cows has a range of clover that she particularly likes (these ranges might overlap). The ranges are defined by a closed interval [S,E].
But some cows are strong and some are weak. Given two cows: cowi and cowj, their favourite clover range is [Si, Ei] and [Sj, Ej]. If Si <= Sj and Ej <= Ei and Ei - Si > Ej - Sj, we say that cowi is stronger than cowj.
For each cow, how many cows are stronger than her? Farmer John needs your help!
Input
The input contains multiple test cases.
For each test case, the first line is an integer N (1 <= N <= 105), which is the number of cows. Then come N lines, the i-th of which contains two integers: S and E(0 <= S < E <= 105) specifying the start end location respectively of a
range preferred by some cow. Locations are given as distance from the start of the ridge.
The end of the input contains a single 0.
Output
For each test case, output one line containing n space-separated integers, the i-th of which specifying the number of cows that are stronger than cowi.
Sample Input
Sample Output
Hint
Huge input and output,scanf and printf is recommended.
Source
POJ Contest,Author:Mathematica@ZSU
题意:有n头牛,每头牛对应一个区间[Si,Ei],如果牛j 的区间是牛i 的区间的真子集,那么就说牛i 比牛j 强壮。依次输出比第i头牛强壮的牛数目
解题思路:将所有牛的E区间按从大到小排序(如果E相同, 则S小的排在前面)的话,那当前读取到第i个牛的Si和Ei,那么之前(假设任意牛的区间不会完全相同)的牛的Sj(j <= i - 1) <= Si的这些牛就都比i号牛强壮了。可以用树状数组维护牛的S坐标来。如果两个区间完全一样, 那么就把i - 1的结果赋值给i的结果。如果i牛与i - 1牛不完全相同,就输出前面有多少牛的S值 <= S[i]。( 注意:牛的S和E坐标可能为0, 所S和E坐标都 + 1)
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 19319 | Accepted: 6544 |
Farmer John's cows have discovered that the clover growing along the ridge of the hill (which we can think of as a one-dimensional number line) in his field is particularly good.
Farmer John has N cows (we number the cows from 1 to N). Each of Farmer John's N cows has a range of clover that she particularly likes (these ranges might overlap). The ranges are defined by a closed interval [S,E].
But some cows are strong and some are weak. Given two cows: cowi and cowj, their favourite clover range is [Si, Ei] and [Sj, Ej]. If Si <= Sj and Ej <= Ei and Ei - Si > Ej - Sj, we say that cowi is stronger than cowj.
For each cow, how many cows are stronger than her? Farmer John needs your help!
Input
The input contains multiple test cases.
For each test case, the first line is an integer N (1 <= N <= 105), which is the number of cows. Then come N lines, the i-th of which contains two integers: S and E(0 <= S < E <= 105) specifying the start end location respectively of a
range preferred by some cow. Locations are given as distance from the start of the ridge.
The end of the input contains a single 0.
Output
For each test case, output one line containing n space-separated integers, the i-th of which specifying the number of cows that are stronger than cowi.
Sample Input
3 1 2 0 3 3 4 0
Sample Output
1 0 0
Hint
Huge input and output,scanf and printf is recommended.
Source
POJ Contest,Author:Mathematica@ZSU
题意:有n头牛,每头牛对应一个区间[Si,Ei],如果牛j 的区间是牛i 的区间的真子集,那么就说牛i 比牛j 强壮。依次输出比第i头牛强壮的牛数目
解题思路:将所有牛的E区间按从大到小排序(如果E相同, 则S小的排在前面)的话,那当前读取到第i个牛的Si和Ei,那么之前(假设任意牛的区间不会完全相同)的牛的Sj(j <= i - 1) <= Si的这些牛就都比i号牛强壮了。可以用树状数组维护牛的S坐标来。如果两个区间完全一样, 那么就把i - 1的结果赋值给i的结果。如果i牛与i - 1牛不完全相同,就输出前面有多少牛的S值 <= S[i]。( 注意:牛的S和E坐标可能为0, 所S和E坐标都 + 1)
#include <iostream> #include <cstdio> #include <string> #include <cstring> #include <algorithm> #include <queue> #include <vector> #include <set> #include <stack> #include <map> #include <climits> using namespace std; #define LL long long const int INF = 0x3f3f3f3f; int n, x[100005], ans[100005]; struct node { int s, e, id; }a[100005]; bool cmp(node a, node b) { if (a.e != b.e) return a.e>b.e; else return a.s<b.s; } int lowbit(int k) { return k&-k; } void add(int k) { while (k <= 100001) { x[k]++; k += lowbit(k); } } int getsum(int k) { int sum = 0; while (k) { sum += x[k]; k -= lowbit(k); } return sum; } int main() { while (~scanf("%d", &n) && n) { memset(x, 0, sizeof x); for (int i = 1; i <= n; i++) { scanf("%d%d", &a[i].s, &a[i].e); a[i].id = i; } sort(a + 1, a + 1 + n, cmp); ans[a[1].id] = 0; add(a[1].s+1); for (int i = 2; i <= n; i++) { if (a[i].e == a[i - 1].e&&a[i].s == a[i - 1].s) ans[a[i].id] = ans[a[i - 1].id]; else ans[a[i].id] = getsum(a[i].s + 1); add(a[i].s + 1); } printf("%d", ans[1]); for (int i = 2; i <= n; i++) printf(" %d", ans[i]); printf("\n"); } return 0; }
相关文章推荐
- poj2481 Cows
- poj2481 Cows(树状数组)
- poj2481 -- Cows 线段覆盖,树状数组处理
- poj2481 Cows
- POJ2481 Cows
- POJ2481 Cows(线段树 & 树状数组)
- poj2481 Cows
- poj2481 Cows (树状数组)
- POJ2481 Cows[树状数组+离散化]
- 【树状数组】Cows (POJ2481) PASCA…
- 【POJ2481】cows 线段树
- POJ2481 Cows 树状数组
- poj2481 Cows
- poj2481 Cows(树状数组)
- poj2481 Cows 树状数组
- POJ2481 Cows 树状数组的简单应用
- [树状数组]poj2481 Cows
- poj2481 Cows
- POJ2481-Cows
- POJ2481 Cows