uva 501 Black Box

点击打开链接uva 501

思路: vector模拟
分析:
1 题目的规模是n和m最大为30000,那么明显复杂度要为O(nlogn)级别才能过
2 由于枚举u数组需要n,所以剩下的是logn的时间,明显只有二分可以做到。所以利用vector来模拟,插入的时候利用二分找到位置即可

代码:

#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

const int MAXN = 30010;

int n , m;
int num[MAXN];

void solve(){
    vector<int>v;
    vector<int>::iterator it;
    v.clear();
    int x , pos = 0;
    int index = 0;
    while(m--){
         scanf("%d" , &x);
         while(v.size() != x){
              it = lower_bound(v.begin() , v.end() , num[pos]);
              v.insert(it , num[pos++]);
         }
         printf("%d\n" , v[index++]);
    }
}

int main(){
    int Case;
    bool first = true;
    scanf("%d" , &Case);
    while(Case--){
        scanf("%d%d" , &n , &m);
        if(first)
            first = false;
        else
            puts("");
        for(int i = 0 ; i < n ; i++)
            scanf("%d" , &num[i]);
        solve();
    }
    return 0;
}
时间: 2024-09-16 08:17:24

uva 501 Black Box的相关文章

《程序设计解题策略》——1.4 利用改进型的二叉查找树优化动态集合的操作

1.4 利用改进型的二叉查找树优化动态集合的操作 我们知道,二叉查找树(binary search tree)能够支持多种动态集合操作,因此在程序设计竞赛中,二叉查找树起着非常重要的作用,它可以用来表示有序集合.建立索引或优先队列等.作用于二叉查找树上的基本操作时间是与树的高度成正比的:对于一棵含n个节点的二叉查找树,如果呈完全二叉树结构,则这些操作的最坏情况运行时间为O(log2n):但如果呈线性链结构,则这些操作的最坏情况运行时间退化为O(n).针对二叉查找树这种不平衡.不稳定的弊病,人们做

算法题:UVa 591 Box of Bricks (模拟)

591 - Box of Bricks Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php? option=com_onlinejudge&Itemid=8&category=467&page=show_problem&problem=53 2 Little Bob likes playing with his box of bricks. He puts the bricks one upon an

UVa 103:Stacking Boxes

[题目链接] http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=114&page=show_problem&problem=39 [原题] Stacking Boxes Background Some concepts in Mathematics and Computer Science are simple in one or two dimensions but

UVa 10012:How Big Is It? 圆排列问题

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=108&page=show_problem&problem=953 类型: 回溯 原题: Ian's going to California, and he has to pack his things, including his collection of circles. Given a set of

UVa 11417 GCD (欧拉φ函数)

11417 - GCD Time limit: 2.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=2412 Given the value of N, you will have to find the value of G. The definition of G is given

UVa 10602

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1543 类型:贪心 原题: Company Macrohard has released it's new version of editor Nottoobad, which can understand a few voice commands.

UVa 10392 Factoring Large Numbers:素因子分解

10392 - Factoring Large Numbers Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=100&page=show_problem&problem=1333 One of the central ideas behind much cryptography is that factoring

UVa 10182 Bee Maja:规律&amp;amp;O(1)算法

10182 - Bee Maja Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1123 Maja is a bee. She lives in a bee hive with thousands of other bees. This bee hive c

算法题之UVA 763

Fibinary Numbers The standard interpretation of the binary number 1010 is 8 + 2 = 10. An alternate way to view the sequence ``1010'' is to use Fibonacci numbers as bases instead of powers of two. For this problem, the terms of the Fibonacci sequence