zoj 3633 map簡單應用

Alice's present

Time Limit: 5 Seconds      Memory Limit: 65536 KB

As a doll master, Alice owns a wide range of dolls, and each of them has a number tip on it's back, the tip can be treated as a positive integer. (the number can be repeated). One day, Alice hears that her best friend Marisa's birthday is coming , so she decides to sent Marisa some dolls for present. Alice puts her dolls in a row and marks them from 1 ton. Each time Alice chooses an interval from i to j in the sequence ( includei and j ) , and then checks the number tips on dolls in the interval from right to left. If any number appears more than once , Alice will treat this interval as unsuitable. Otherwise, this interval will be treated as suitable.

This work is so boring and it will waste Alice a lot of time. So Alice asks you for help .

Input

There are multiple test cases. For each test case:

The first line contains an integer n ( 3≤ n ≤ 500,000) ,indicate the number of dolls which Alice owns.

The second line contains n positive integers , decribe the number tips on dolls. All of them are less than 2^31-1.The third line contains an intergerm ( 1 ≤ m ≤ 50,000 ),indicate how many intervals Alice will query. Then followed by m lines, each line contains two integeru, v ( 1≤ u< vn ),indicate the left endpoint and right endpoint of the interval.Process to the end of input.

Output

For each test case:

For each query,If this interval is suitable , print one line "OK".Otherwise, print one line ,the integer which appears more than once first.

Print an blank line after each case.

Sample Input

5
1 2 3 1 2
3
1 4
1 5
3 5
6
1 2 3 3 2 1
4
1 4
2 5
3 6
4 6

Sample Output

1
2
OK

3
3
3
OK

一開始想利用c來做,但是不斷超時,然後看了一下STATUS 看到了滿版的c++.......

思路很簡單,從右向左,第一個重複的數字就是答案,否則就是OK了.

可以利用 map 映射,出現重複的就輸出答案.

例如

11122334

輸入4 7

就是2233這一段數字.

從右向左: 3  3   2  2

                  1  2   1  2

很明顯答案是3

#include <iostream> 
#include <vector> 
#include<stdio.h>
#include <cstring> 
#include<map>
using namespace std; 

int f[500001],hash;
int main()
{
  int a,b,n,m,i,j;
  while(~scanf("%d",&n))
  {
    for(i=0;i<n;i++)
      scanf("%d",&f[i]);
    scanf("%d",&m);
    for(int k=0;k<m;k++)
    {
      scanf("%d%d",&a,&b);
      map<int,int>q;
      int flag=0;
      for(i=b-1;i>=a-1;i--)
      {
        q[f[i]]++;
        //第一個重複出現的數字一旦存在就跳出循環.
        if(q[f[i]]==2)
        {
          flag=1;
          break;
        }
      }
      if(!flag)
        printf("OK\n");
      else
        printf("%d\n",f[i]);

    }
    putchar(10);
  }
  return 0;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章