zoukankan      html  css  js  c++  java
  • STL UVA 11991 Easy Problem from Rujia Liu?

    题目传送门

    题意:训练指南P187

    分析:用vector存id下标,可以用map,也可以离散化用数组存(发现不用离散化也可以)

    map

    #include <bits/stdc++.h>
    using namespace std;
    
    map<int, vector<int> >mp;
    
    int main(void)	{
    	int n, m;
    	while (scanf ("%d%d", &n, &m) == 2)	{
    		mp.clear ();
    		for (int a, i=1; i<=n; ++i)	{
    			scanf ("%d", &a);
    			if (!mp.count (a))	mp[a] = vector<int> ();
    			mp[a].push_back (i);
    		}
    		while (m--)	{
    			int k, v;	scanf ("%d%d", &k, &v);
    			if (!mp.count (v) || mp[v].size () < k)	puts ("0");
    			else	printf ("%d
    ", mp[v][k-1]);
    		}
    	}
    
    	return 0;
    }
    

    离散化

    #include <bits/stdc++.h>
    using namespace std;
    
    const int N = 1e5 + 5;
    vector<int> vec[N];
    int a[N], A[N];
    
    int main(void)	{
    	int n, m;
    	while (scanf ("%d%d", &n, &m) == 2)	{
    		for (int i=1; i<=n; ++i)	vec[i].clear ();
    		for (int i=1; i<=n; ++i)	{
    			scanf ("%d", &a[i]);	A[i] = a[i];
    		}
    		sort (A+1, A+1+n);
    		int nn = unique (A+1, A+1+n) - A - 1;
    		for (int i=1; i<=n; ++i)	{
    			a[i] = lower_bound (A+1, A+1+nn, a[i]) - A;
    		}
    		for (int i=1; i<=n; ++i)	{
    			vec[a[i]].push_back (i);
    		}
    		for (int k, v, i=1; i<=m; ++i)	{
    			scanf ("%d%d", &k, &v);
    			v = lower_bound (A+1, A+1+nn, v) - A;
    			if (vec[v].size () < k)	puts ("0");
    			else	printf ("%d
    ", vec[v][k-1]);
    		}
    	}
    
    	return 0;
    }
    

      

    编译人生,运行世界!
  • 相关阅读:
    Caesar cipher
    遗传算法之背包问题
    Transport scheme NOT recognized: [stomp]
    error running git
    Canvas 旋转的图片
    canvas时钟
    火箭起飞
    让图标转起来
    Tomcat启动脚本
    Task中的异常处理
  • 原文地址:https://www.cnblogs.com/Running-Time/p/5027060.html
Copyright © 2011-2022 走看看