zoukankan      html  css  js  c++  java
  • uva11991 Easy Problem from Rujia Liu?

    题目链接

    分析:

    《算法竞赛入门经典——训练指南》上的一道例(水)题,map的应用,个人感觉代码中注释掉的那一句没有什么意义,就注释掉了,提交确实也对了。

     map的小知识点(总结自c++ primer):

    1. 对于map容器,如果下标所表示的键在容器中不存在,则添加元素。书中的例子:
      map<string, int> word_count;
      sting word;
      while(cin>>word)
          ++word_count[word];

      在单词第一次出现时,会在word_count中创建并插入一个以该单词为索引的新元素,同时将它的值初始化为0。

    2. 当只想要知道某元素存在,而当元素不存在时,并不想做插入运算,应当用count或find。count返回m中k的出现次数(使用方法就如本代码注释的那条一样)。
    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <queue>
    #include <stack>
    #include <map>
    
    using namespace std;
    
    const int maxn = 1000 + 10;
    
    map<int, vector<int> > a;
    
    int main(){
        int n, m, x;
        while(scanf("%d %d", &n, &m) == 2){
            a.clear();
    
            for(int i=0; i<n; i++){
                scanf("%d", &x);
                //if(!a.count(x)) a[x] = vector<int>();
                a[x].push_back(i+1);
            }
    
            int k, v;
            while(m--){
                scanf("%d%d", &k, &v);
                if(!a.count(v) || a[v].size()<k) printf("0\n");
                else printf("%d\n", a[v][k-1]);
            }
        }
    
        return  0;
    }
  • 相关阅读:
    IL查看泛型
    IL查看委托
    IL查看override
    为VS集成IL环境
    HashTable Dictionary HashMap
    C#集合类
    Resharper团队协作之TODO
    怪物彈珠Monster Strike 攻略
    [Editor]Unity Editor类常用方法
    C# Reflection BindingFlags
  • 原文地址:https://www.cnblogs.com/tanhehe/p/3050962.html
Copyright © 2011-2022 走看看