CF-Making Towers
正确代码:
#include<bits/stdc++.h>
using namespace std;
#define x first
#define y second
const int N = 2e5 + 10;
typedef long long LL;
typedef pair<int, int> PII;
// PII a[N];
int cnt[N];
int main()
{
int t;
cin >> t;
while(t --)
{
int n;
cin >> n;
vector<int>a[N];
for(int i = 1; i <= n; i ++){
int tp;
cin >> tp;
a[tp].push_back(i);
}
for(int i = 1; i <= n; i ++)
{
if(a[i].size() == 0){
cout << 0 <<" ";
continue;
}
else {
int ans = 1;
for(int j = 0; j < a[i].size() - 1; j ++)
{
if((a[i][j + 1] - a[i][j] - 1) % 2 == 0 || a[i][j + 1] - a[i][j] == 1)ans ++;
}
cout << ans << " ";
}
}
cout << endl;
}
}
错误代码:
#include<bits/stdc++.h>
using namespace std;
#define x first
#define y second
const int N = 2e5 + 10;
typedef long long LL;
typedef pair<int, int> PII;
// PII a[N];
int cnt[N];
int main()
{
int t;
cin >> t;
while(t --)
{
int n;
cin >> n;
vector<int>a[N];
for(int i = 1; i <= n; i ++){
int tp;
cin >> tp;
a[tp].push_back(i);
}
for(int i = 1; i <= n; i ++)
{
if(a[i].size() == 0){
cout << 0 <<" ";
continue;
}
else {
int ans = 1;
for(int j = 0; j < a[i].size() - 1; j ++)
{
if((a[i][j + 1] - a[i][j] - 1) % 2 == 0 || a[i][j + 1] - a[i][j] == 1)ans ++;
}
cout << ans << " ";
}
}
cout << endl;
}
}
定义vector数组比清空vector数组所需要的时间长?