poj2182-Lost Cow

题目链接 http://vjudge.net/problem/POJ-2182

解题思路

用最朴素的想法,从尾到头扫描,再开两个循环找空位,时间复杂度O(n3),容易超时。

正确的做法是建立线段树。维护每个区间的长度。长度足够排下就往左子树走,否则往右子树。

直到叶子节点,此时叶子节点的区间(点)为此奶牛的编号。

代码

#include<stdio.h>
#define MAX_SIZE 10010
struct Node {
    int l, r;
    int len;
}tree[4*MAX_SIZE];
int small[MAX_SIZE], q[MAX_SIZE];
void BuildTree(int root, int left, int right)
{
    if(left == right) {
        tree[root].l = tree[root].r = right;
        tree[root].len = 1;
        return ;
    }
    tree[root].l = left; tree[root].r = right;
    tree[root].len = right - left + 1;
    int mid = (left + right) / 2;
    BuildTree(root*2, left, mid);
    BuildTree(root*2+1, mid+1, right);
}
int query(int root, int s)
{
    tree[root].len--;
    if(tree[root].l == tree[root].r) return tree[root].l;
    if(tree[root*2].len >= s) return query(root*2, s);
    else return query(root*2+1, s - tree[root*2].len);
}
int main()
{
    int n;
    scanf("%d", &n);
    for(int i=2; i<=n; i++) scanf("%d", &small[i]);
    small[1] = 0;
    BuildTree(1, 1, n);
    for(int i=n; i>=1; i--) q[i] = query(1, small[i]+1);
    for(int i=1; i<=n; i++) printf("%d\n", q[i]);
    return 0;
}
    
时间: 2024-12-09 07:43:28

poj2182-Lost Cow的相关文章

poj3278Catch That Cow

Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 88361   Accepted: 27679 Description Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,00

The Cow Lineup_找规律

Description Farmer John's N cows (1 <= N <= 100,000) are lined up in a row.Each cow is labeled with a number in the range 1...K (1 <= K <=10,000) identifying her breed. For example, a line of 14 cows might have these breeds: 1 5 3 2 5 1 3 4 4

poj 1985 Cow Marathon 【树的直径】

题目:poj 1985 Cow Marathon 题意:给出一个树,让你求树的直径. 分析: 树的直径:树上两点之间的最大距离. 我们从任意一点出发,BFS一个最远距离,然后从这个点出发,在BFS一个最远距离,就是树的直径. AC代码: /* POJ:1985 Cow Marathon 2014/10/12/21:18 Yougth*/ #include <cstdio> #include <iostream> #include <algorithm> #include

POJ 2018 Best Cow Fences

斜率优化DP...<浅谈数形结合思想在信息学竞赛中的应用 安徽省芜湖一中 周源>例题... Best Cow Fences Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 9311   Accepted: 2986 Description Farmer John's farm consists of a long row of N (1 <= N <= 100,000)fields. Each field c

[USACO11FEB] Cow Line

https://www.luogu.org/problem/show?pid=3014 题目描述 The N (1 <= N <= 20) cows conveniently numbered 1...N are playing yet another one of their crazy games with Farmer John. The cows will arrange themselves in a line and ask Farmer John what their line

luogu P2863 [USACO06JAN]牛的舞会The Cow Prom

https://www.luogu.org/problem/show?pid=2863#sub 题目描述 The N (2 <= N <= 10,000) cows are so excited: it's prom night! They are dressed in their finest gowns, complete with corsages and new shoes. They know that tonight they will each try to perform th

Catch That Cow(广搜)

个人心得:其实有关搜素或者地图啥的都可以用广搜,但要注意标志物不然会变得很复杂,想这题,忘记了标志,结果内存超时: 将每个动作扔入队列,但要注意如何更简便,更节省时间,空间 Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and t

TOJ 2703: Cow Digit Game

2703: Cow Digit Game Time Limit(Common/Java):1000MS/10000MS     Memory Limit:65536KByte Total Submit: 1            Accepted:1 Description Bessie is playing a number game against Farmer John, and she wants you to help her achieve victory. Game i start

Cow Acrobats POJ - 3045

Farmer John's N (1 <= N <= 50,000) cows (numbered 1..N) are planning to run away and join the circus. Their hoofed feet prevent them from tightrope walking and swinging from the trapeze (and their last attempt at firing a cow out of a cannon met wit

bfs hdu 2717 Catch That Cow

Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 14553    Accepted Submission(s): 4422 Problem Description Farmer John has been informed of the location of a fugitive cow and wants