POJ 2481 Cows
题目链接:http://poj.org/problem?id=2481
解题思路:可以先尝试HDU 1541 Stars 。这道题相当于Stars的变形,只不过需要稍微思考。按E从大到小排序,E相同S从小到大对S E数组排序,使用一个一维BIT,初始为0,之后对于每一个[S, E]:
ans[i] = sum(S[i]) - 已经出现过的与当前[S, E]完全一样的区间个数
add(S[i], 1)
最后记得按原来顺序输出。
代码:
1 const int inf = 0x3f3f3f3f; 2 const int maxn = 1e5 + 5; 3 4 int bit[maxn], S[maxn], E[maxn], f[maxn]; 5 int n, maxp = 0, anss[maxn]; 6 7 int add(int i, int x){ 8 while(i <= maxp){ 9 bit[i] += x; 10 i += (i & -i); 11 } 12 return 0; 13 } 14 int sum(int i){ 15 int ans = 0; 16 while(i > 0){ 17 ans += bit[i]; 18 i -= (i & -i); 19 } 20 return ans; 21 } 22 int cmp(int i, int j){ 23 if(E[i] >= E[j]){ 24 if(E[i] == E[j]) return S[i] < S[j]; 25 else return true; 26 } 27 return false; 28 } 29 30 int main(){ 31 while(scanf("%d", &n) && n != 0){ 32 memset(anss, 0, sizeof(anss)); 33 memset(bit, 0, sizeof(bit)); 34 for(int i = 1; i <= n; i++) { 35 scanf("%d %d", &S[i], &E[i]); 36 S[i]++; 37 E[i]++; 38 maxp = max(maxp, E[i]); 39 f[i] = i; 40 } 41 sort(f + 1, f + n + 1, cmp); 42 int cnt = 0; 43 add(S[f[1]], 1); 44 anss[f[1]] = 0; 45 for(int i = 2; i <= n; i++){ 46 int ti = f[i], lai = f[i - 1]; 47 if(S[ti] == S[lai] && E[ti] == E[lai]) cnt++; 48 else cnt = 0; 49 int tmans = sum(S[ti]); 50 tmans -= cnt; 51 anss[ti] = tmans; 52 add(S[ti], 1); 53 } 54 for(int i = 1; i <= n; i++){ 55 printf("%d", anss[i]); 56 if(i != n) printf(" "); 57 else printf("\n"); 58 } 59 } 60 }
题目:
Cows
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 19614 | Accepted: 6670 |
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!
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.
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