题目传送
题意:
给你n个数字,问每k个段的都存在的最小数字是多少?
思路:
存一下每一种数存在的位置,找出最小k使得这个数都在每一段中存在,然后前缀和求出最小值
具体看注释
AC代码
#include <bits/stdc++.h>
inline long long read(){char c = getchar();long long x = 0,s = 1;
while(c < '0' || c > '9') {if(c == '-') s = -1;c = getchar();}
while(c >= '0' && c <= '9') {x = x*10 + c -'0';c = getchar();}
return x*s;}
using namespace std;
#define NewNode (TreeNode *)malloc(sizeof(TreeNode))
#define Mem(a,b) memset(a,b,sizeof(a))
#define lowbit(x) (x)&(-x)
const int N = 2e5 + 10;
const long long INFINF = 0x7f7f7f7f7f7f7f;
const int INF = 0x3f3f3f3f;
const double EPS = 1e-5;
const int mod = 1e9+7;
const double II = acos(-1);
const double PP = (II*1.0)/(180.00);
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> piil;
signed main()
{
std::ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
int t;
cin >> t;
while(t--)
{
int n;
cin >> n;
int arr[n+5] = {0},ans[n+5];
ans[0] = INF;
vector<int> v[n+5];
for(int i = 1;i <= n;i++)
{
cin >> arr[i];
ans[i] = INF;
if(v[arr[i]].size() == 0)
v[arr[i]].push_back(0);//方便求出第一个数之前的距离
v[arr[i]].push_back(i);
}
for(int i = 1;i <= n;i++)
if(v[arr[i]].size() != 0)
v[arr[i]].push_back(n+1);//方便求出最后一个数到最后的距离
for(int i = 1;i <= n;i++)
{
int Min = -1;
for(int j = 1;j < v[i].size();j++)
Min = max(Min,v[i][j]-v[i][j-1]);//最小的存在的k
if(Min != -1)
ans[Min] = min(ans[Min],i);
}
for(int i = 1;i <= n;i++) ans[i] = min(ans[i],ans[i-1]);//前缀和求最小
for(int i = 1;i <= n;i++) ans[i] == INF ? cout << -1 << " " : cout << ans[i] << " ";
cout << endl;
}
}