Description
給定Q次操作:
每次操作給定一個正整數x —— 表示向集合A和集合B裏面各加入一個元素x。
集合A:允許存在相同元素
集合B:不允許存在相同元素,如果有多個相同元素只存在一個。
Input
第一行輸入一個整數t,代表有t組測試數據(t <= 10)
每組數據第一行輸出一個整數Q,代表操作次數。
保證:1 <= Q <= 100000,且所有元素均在int範圍內。
Output
對每次操作輸出兩個結果,分別代表A集合裏面的元素個數、B集合裏面的元素個數。
Sample Input
2 2 2 2 3 1000000000 1000000001 1000000001
Sample Output
1 1 2 1 1 1 2 2 3 2
該題看着比較簡單,其實如果用遍歷的方法的話就一定會超時,這裏用到了比較實用的map函數,該函數可以非常方便的記錄出現的次數,並且不會重複,下面看代碼:
#include<cstdio>
#include<cstring>
#include<map>
#include<algorithm>
using namespace std;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int Q;
int x;
scanf("%d",&Q);
map<int,int>mapp1;//定義一個map函數,mapp1[i]以及i的類型可以任意定義//
int count1 = 0;
int count2 = 0;
for(int i =1;i<=Q;i++)
{
scanf("%d",&x);
count2++;
mapp1[x]++;//如果x出現了兩次,那麼mapp[x]的值就等於二//
if(mapp1[x]==1)
count1++;
printf("%d ",count2);
printf("%d\n",count1);
}
}
return 0;
}
願你一生清澈明朗,做你願做之事,愛你願愛之人!