码迷,mamicode.com
首页 > 其他好文 > 详细

Uva 11991 Easy Prblem from Rujia Liu ?

时间:2014-09-06 10:52:03      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   os   io   使用   ar   for   数据   

【题意讲解】

本题讲的是给定一组数据,让你输出第k个v的下标。采用一般的方法本题是可以做出来的(数据(k,v)量较小)。对于数据量较大的情况那么这种方法就不适用了(会浪费大量能够空间)。那么我可以考虑使用一种叫map容器的方法。采用动态存储的方法,不浪费

空间。

【map容器简介】

见c++map容器 简介

【本题代码】

 1 #include<iostream>
 2 #include<vector>
 3 #include<map>
 4 using namespace std;
 5 map<int ,vector<int> >a;//第一个int是代表关键字的意思,第二个vector<int>代表关键字为第一个int的不定数组 
 6 int main()
 7 {
 8      int n,m,x,y;
 9      while(scanf("%d%d",&n,&m)==2)
10      {
11          a.clear();//清除map 容器中原有的内容 
12         for(int i=0;i<n;i++)
13         {
14                 scanf("%d",&x);
15                 if(!a.count(x)) //如果关键字不存在那么count函数 返回值为0;否则为1 
16                 a[x]=vector<int>();//动态分配vector 
17                 a[x].push_back(i+1);//把数据压入数组a[x]中,此时的a[x]数组相当于 二纬数组    
18         }
19        }
20        while(m--)
21      {
22          scanf("%d%d",&x,&y);
23         if(!a.count(y) ||a[y].size()<x) printf("0\n");
24         else printf("%d\n",a[y][x-1]);
25      }
26     return 0;
27 }

 

Uva 11991 Easy Prblem from Rujia Liu ?

标签:style   blog   color   os   io   使用   ar   for   数据   

原文地址:http://www.cnblogs.com/khbcsu/p/3959166.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!