POJ 2182 Lost Cows.(线段树)

~~~~

数据太水,暴力无压力,不过还是用线段树写了下,表现了楼主对线段树的无限热爱之情。

~~~~

题目连接:http://poj.org/problem?id=2182

大致题意;牛牛因XXXXXX原因乱序成一排,现已知每头牛前面有多少头牛比它的编号小(第一头牛前面没有当然就不列粗来了),求每头牛的编号。

思路:从后往前扫描,遇到a,则说明它是剩余序列的第a+1头牛。

~~~~

暴力代码:(157ms)

#include<cstdio>
#include<algorithm>
#include<cstring>
#define N 8888
using namespace std;

int q[N],a[N],ans[N];
int main()
{
    int n;
    while(scanf("%d",&n)==1)
    {
        q[0]=0;a[0]=1;
        for(int i=1;i<n;i++)
        {
            scanf("%d",&q[i]);
            a[i]=i+1;
        }
        for(int i=n-1;i>=0;i--)
        {
            ans[i]=a[q[i]];
            for(int j=q[i];j<n-1;j++)
                a[j]=a[j+1];
        }
        for(int i=0;i<n;i++)
            printf("%d\n",ans[i]);
    }
    return 0;
}

~~~~

线段树代码:(32ms)

#include<cstdio>
#include<algorithm>
#include<cstring>
#define N 8888
#define lson rt<<1,s,m
#define rson rt<<1|1,m+1,e
using namespace std;

int tre[N<<2];
void build(int rt,int s,int e)
{
    tre[rt]=e-s+1;  //用tre数组存每条线段间有多少个牛
    if(s==e)
        return ;
    int m=(s+e)>>1;
    build(lson);
    build(rson);
}
int query(int val,int rt,int s,int e)
{
    tre[rt]--;  //每次query,牛数必减1,因为下一次询问总是从剩余的序列中。
    if(s==e)
        return s;   //~~
    int m=(s+e)>>1;
    if(val<=tre[rt<<1]) return query(val,lson);
    else return query(val-tre[rt<<1],rson);
}

int q[N],ans[N];
int main()
{
    int n;
    while(scanf("%d",&n)==1)
    {
        build(1,1,n);
        q[0]=0;     //~~
        for(int i=1;i<n;i++)
            scanf("%d",&q[i]);
        for(int i=n-1;i>=0;i--)
            ans[i]=query(q[i]+1,1,1,n);
        for(int i=0;i<n;i++)
            printf("%d\n",ans[i]);
    }
    return 0;
}

POJ 2182 Lost Cows.(线段树),布布扣,bubuko.com

时间: 2024-10-25 14:17:01

POJ 2182 Lost Cows.(线段树)的相关文章

poj 2182 Lost Cows(线段树经典题)

题目链接:http://poj.org/problem?id=2182 Lost Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9152   Accepted: 5879 Description N (2 <= N <= 8,000) cows have unique brands in the range 1..N. In a spectacular display of poor judgment, th

poj 2182 Lost Cows (线段树)

 Lost Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8838   Accepted: 5657 Description N (2 <= N <= 8,000) cows have unique brands in the range 1..N. In a spectacular display of poor judgment, they visited the neighborhood 'wate

POJ 2481 Cows (线段树)

Cows 题目:http://poj.org/problem?id=2481 题意:有N头牛,每只牛有一个值[S,E],如果对于牛i和牛j来说,它们的值满足下面的条件则证明牛i比牛j强壮:Si <=Sjand Ej <= Ei and Ei - Si > Ej - Sj.现在已知每一头牛的测验值,要求输出每头牛有几头牛比其强壮. 思路:将牛按照S从小到大排序,S相同按照E从大到小排序,这就保证了排在后面的牛一定不比前面的牛强壮.再按照E值(离散化后)建立一颗线段树(这里最值只有1e5,所

POJ 3264-Balanced Lineup(线段树:单点更新+区间查询)

Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 34522   Accepted: 16224 Case Time Limit: 2000MS Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One day Farmer Joh

POJ训练计划2777_Count Color(线段树/成段更新/区间染色)

解题报告 题意: 对线段染色,询问线段区间的颜色种数. 思路: 本来直接在线段树上染色,lz标记颜色.每次查询的话访问线段树,求出颜色种数.结果超时了,最坏的情况下,染色可以染到叶子节点. 换成存下区间的颜色种数,这样每次查询就不用找到叶子节点了,用按位或来处理颜色种数. #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace

POJ训练计划1177_Picture(扫描线/线段树+离散)

解题报告 题意: 求矩形周长和. 思路: 左扫上扫,扫过了. #include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <cmath> using namespace std; struct Seg { int lx,rx,ly,ry,h,v; friend bool operator < (Seg a,Seg b) { re

POJ训练计划2828_Buy Tickets(线段树/单点更新)

解题报告 题意: 插队完的顺序. 思路: 倒着处理数据,第i个人占据第j=pos[i]+1个的空位. 线段树维护区间空位信息. #include <iostream> #include <cstdio> #include <cstring> using namespace std; struct node { int x,v; } num[201000]; int sum[1000000],ans[201000]; void cbtree(int rt,int l,in

POJ 1151 Atlantis 扫描线+线段树

点击打开链接 Atlantis Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17252   Accepted: 6567 Description There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some of these texts even include maps of pa

HDU 1540 &amp;&amp; POJ 2892 Tunnel Warfare (线段树,区间合并).

~~~~ 第一次遇到线段树合并的题,又被律爷教做人.TAT. ~~~~ 线段树的题意都很好理解吧.. 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1540 http://poj.org/problem?id=2892 ~~~~ 我的代码:200ms #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #defin