您的位置:首页 > 其它

UVA 11991 - Easy Problem from Rujia Liu?【stl】

2015-11-17 15:57 232 查看
原文网址:
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3142
顺序给出n个数字和m次查询,查询时输入 k,v 输出从左到右第k个v 的下标,下标从1 到n。

训练指南的一道题,也是感觉不好处理,有点麻烦,然后发现代码这么简洁,也是醉了...

第一个的感觉就是,数据结构的用法真灵活,stl 只是一种外在的表现,更重要的是如何加工和处理各种数据,以达到使用的方便性,况且这些还是只是最基础的数据结构...努力学习吧!

#include<stdio.h>
#include<vector>
#include<map>
using namespace std;
int main()
{
int n,m;
//freopen("shuju.txt","r",stdin);
while(~scanf("%d%d",&n,&m))
{
map<int,vector<int> > map;
for(int i=1;i<=n;++i)
{
int x;
scanf("%d",&x);
if(!map.count(x))//如果这个数未出现过
{
map[x]=vector<int>();//分配一个数组
}
map[x].push_back(i);//把下标号存进这个数组里
}
while(m--)
{
int x,y;
scanf("%d%d",&x,&y);
if(!map.count(y)||map[y].size()<x)
//这个数不存在或者容器为空的话
{
printf("0\n");//注意看题目要求,输出 0
}
else
{
printf("%d\n",map[y][x-1]);
}
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: