描述

游戏积分的排行榜出来了,小z想看看得某个积分的人是谁。但是由于人数很多,他自己找很浪费时间,所以他想请你帮忙写一个程序,能快速的帮他找到他想要找的人
 
输入
多组测试数据,第一行有一个数T,表示有T组测试数据(T<=50)
第二行有两个数n和m(1<=n,m<=10000),n表示有n个人,m表示有m次查询
接下来n行,输入每个人的名字(长度小于10)和积分num(0<=num<=10^8),
接下来m行,每个数表示要查询的积分(每次查询一定有结果)
输出
输出对应积分的人的名字(如果有多个人的积分相同,则输出最前面的那个)
样例输入
1
5 3
zhangsan 2
qianxiao 5
liuqiang 2
wangwu 1
lisi 3
2
1
3
样例输出
zhangsan
wangwu
lisi
来源
原创
上传者
ACM_马振阳
刚开始想的(但是效率太慢)
 #include <stdio.h>
#include <math.h>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <string>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std;
int num[];
int main()
{
int t,n,m;
string name[];
scanf("%d",&t);
while(t--){
scanf("%d %d",&n,&m);
for(int i=;i<n;i++){
cin>>name[i]>>num[i];
}
for(int j=;j<m;j++){
int p;
scanf("%d",&p);
for(int i=;i<n;i++){
if(p==num[i]){
cout<<name[i]<<endl;
break;
}
}
}
}
return ;
}
要理解二分的过程,左和右...........
 #include <stdio.h>
#include <math.h>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <string>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std;
struct node{
char name[];
int num;
}a[];
bool cmp(node x,node y){
return x.num<y.num;
}
int cf(int l,int r,int k){
while(l<r){
int mid=(l+r)/;
if(a[mid].num>k)
r=mid;
else if(a[mid].num<k)
l=mid+l;
else{
if(a[mid].num==a[mid-].num)
r=mid;
else
return mid;
}
}
}
int main()
{
int n,m,t,i,j,k;
scanf("%d",&t);
while(t--){
scanf("%d %d",&n,&m);
for(i=;i<n;i++){
scanf("%s %d",a[i].name,&a[i].num);
}
stable_sort(a,a+n,cmp);
for(j=;j<m;j++){
scanf("%d",&k);
int p=cf(,n,k);
cout<<a[p].name<<endl;
}
}
return ;
}

NYOJ 904 search(stable_sort函数的应用)的更多相关文章

  1. GCD nyoj 1007 (欧拉函数+欧几里得)

    GCD  nyoj 1007 (欧拉函数+欧几里得) GCD 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 The greatest common divisor ...

  2. nyoj 904 hashmap

    这个题目是个水题目,现在我只管做出来,效率不考虑了. 题目链接: http://acm.nyist.net/JudgeOnline/problem.php?pid=904 我用hashmap 很爽,很 ...

  3. 全排列函数 nyoj 366(next_permutation()函数)

    C++ STL中提供了std::next_permutation与std::prev_permutation可以获取数字或者是字符的全排列,其中std::next_permutation提供升序.st ...

  4. 函数 sort,unique,stable_sort,count_if,谓词

    bool isShorter(const string &s1,const string &s2) { return s1.size() < s2.size(); } bool ...

  5. [LeetCode] Add and Search Word - Data structure design 添加和查找单词-数据结构设计

    Design a data structure that supports the following two operations: void addWord(word) bool search(w ...

  6. PHP的字符串函数

      1.字符串的连接 字符串可以使用字符串连接符(.)来连接 常见的用途是建立大块的HTML字串代码,赋值号 (=) 连接符 (.) 可以被简写合并为 (.=) 符号 2.在字符串中使用变量 这个功能 ...

  7. php数组函数,字符串,linux命令

    1>> Linux常用命令一. 文件目录操作命令1. ls命令    命令格式:ls [选项] [目录名]    命令功能:列出目标目录中所有的子目录和文件.2. 命令格式:cd [目录名 ...

  8. js常用函数、书写可读性的js、js变量声明...

    1.Array类型函数 array.concat(item...) 函数功能:关联数组,实现数组相加功能,但并不影响原先数组,concat返回新数组. array.join(separator) 函数 ...

  9. PHP 常用字符串函数整理

    PHP语言中的字符串函数也是一个比较易懂的知识.今天我们就为大家总结了将近12种PHP字符串函数,希望对又需要的朋友有所帮助,增加读者朋友的PHP知识库. 1.查找字符位置函数 strpos($str ...

随机推荐

  1. C++primer读书笔记9-转换和类类型

    有时指定自己的类类型来表示某些类型的数据,如SmallInt,然后在为了便于计算将指定一个转换算,类类型,在某些情况下,自己主动转换为指定的类型 <1>转换操作符 operator typ ...

  2. Android:简单的弹幕效果达到

    首先,效果图.分类似至360检测到的骚扰电话页面: 布局非常easy,上面是一个RelativeLayout,以下一个Button. 功能: (1)弹幕生成后自己主动从右側往左側滚动(Translat ...

  3. JavaHTTP下载视频

    控制层类: package com.grab.video.controller; import java.io.BufferedOutputStream; import java.io.Buffere ...

  4. Duanxx的Design abroad: C++矩阵运算库Eigen 概要

    一.概要 这两天想起来要做神经网络的作业了,要求用C++完毕神经网络的算法. 摆在面前的第一个问题就是,神经网络算法中大量用到了矩阵运算.可是C++不像matlab那样对矩阵运算有非常好的支持.本来准 ...

  5. Android - 和其他APP交互 - 把用户带到其他app

    Android的重要功能之一就是app可以根据要执行的操作让用户启动另外一个app.例如,app有一个商业地址然后想要在地图上显示,并不需要在app中加一个显示地图的activity,可以直接用Int ...

  6. 【Android进阶】android:configChanges属性总结

    android中的组件Activity在manifest.xml文件中可以指定参数android:ConfigChanges,用于捕获手机状态的改变. 在Activity中添加了android:con ...

  7. Scilab 的画图函数(1)

    Scilab 的画图函数 plot 函数 最主要的是 plot 函数,与 matlab 中的plot 函数类似. xdata = linspace(1,10,50); ydata = sin(xdat ...

  8. android:强大的图像下载和缓存库Picasso

    1.Picasso一个简短的引论 Picasso它是Square该公司生产的一个强大的图像下载并缓存画廊.官方网站:http://square.github.io/picasso/ 仅仅须要一句代码就 ...

  9. 【BZOJ1791】【IOI2008】【基环树】island(status第一速度)

      1791: [Ioi2008]Island 岛屿  Time Limit: 20 Sec  Memory Limit: 162 MB Submit: 908  Solved: 159 [Su ...

  10. IOS-QQ登陆之苹果程序流程

    1.新建项目,通过main函数循环执行代码,直到应用被关闭. 2.点击项目,建立storyboard文件,并在info文件夹中指定第一个storyboard文件 3.建立Controller文件. 组 ...