zoukankan      html  css  js  c++  java
  • POJ 2481Cows(树状数组 + 好题)

    Cows
    Time Limit: 3000MS   Memory Limit: 65536K
    Total Submissions: 15222   Accepted: 5070

    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

    3
    1 2
    0 3
    3 4
    0
    

    Sample Output

    1 0 0
    

    Hint

    Huge input and output,scanf and printf is recommended.
     
    题意:线段起点s,终点e,问每一个线段包含在几个线段里面,样例中线段[1,2]包含在[0,3]里面所以第一个输出是1,
    思路:按照e按照从大到小排序,e相同按照s从小到大排序。因为只有e从大到小排序后,s就可以从小到大更新了,
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <map>
     5 #include <algorithm>
     6 #include <string.h>
     7 using namespace std;
     8 const int MAX = 100000 + 10;
     9 struct node
    10 {
    11     int s,e;
    12     int index;
    13 };
    14 node a[MAX];
    15 int c[MAX],cnt[MAX];
    16 int cmp(node x,node y)
    17 {
    18     if(x.e == y.e)
    19         return x.s < y.s;
    20     return x.e > y.e;
    21 }
    22 int lowbit(int k)
    23 {
    24     return k & (-k);
    25 }
    26 void add (int k,int num)
    27 {
    28     for(int i = k; i < MAX; i += lowbit(i))
    29         c[i] += num;
    30 }
    31 int sum(int k)
    32 {
    33     int s = 0;
    34     for(int i = k; i > 0; i -= lowbit(i))
    35         s += c[i];
    36     return s;
    37 }
    38 int main()
    39 {
    40     int n;
    41     while(scanf("%d", &n) != EOF && n)
    42     {
    43         for(int i = 0; i < n; i++)
    44         {
    45             scanf("%d%d", &a[i].s, &a[i].e);
    46             a[i].index = i;
    47         }
    48         sort(a,a + n,cmp);
    49         memset(c,0,sizeof(c));
    50         memset(cnt,0,sizeof(cnt));
    51         cnt[a[0].index] = 0;
    52         add (a[0].s + 1, 1);
    53         for(int i = 1; i < n; i++)
    54         {
    55             if(a[i].s == a[i - 1].s && a[i].e == a[i - 1].e)
    56                 cnt[a[i].index] = cnt[a[i - 1].index];
    57             else
    58                 cnt[a[i].index] = sum(a[i].s + 1);
    59             add(a[i].s + 1,1);
    60         }
    61         printf("%d",cnt[0]);
    62         for(int i = 1; i < n; i++)
    63             printf(" %d",cnt[i]);
    64         printf("
    ");
    65     }
    66 
    67     return 0;
    68 }
    View Code
  • 相关阅读:
    VS 2015 GIT操作使用说明
    态度以及业余付出决定程序生涯
    Magicodes.WeiChat——使用OAuth 2.0获取微信用户信息
    Magicodes.WeiChat——ASP.NET Scaffolding生成增删改查、分页、搜索、删除确认、批量操作、批量删除等业务代码
    Magicodes.WeiChat——利用纷纭打造云日志频道
    Magicodes.WeiChat——自定义knockoutjs template、component实现微信自定义菜单
    产品管理之敏捷之路(一)——携手同行,走自己的敏捷之路
    Magicodes.NET框架之路——V0.0.0.5 Beta版发布
    Spring mvc中@RequestMapping 6个基本用法小结
    spring.net aop 讲解
  • 原文地址:https://www.cnblogs.com/zhaopAC/p/4977738.html
Copyright © 2011-2022 走看看