刚入门学算法,有道题(虽然好像只能算思维题)卡了好久找不出bug:CodeForces Round 923 (div3) D:https://codeforces.com/problemset/problem/1927/D
用的cpp,没像题解一样用二分,直接模拟了;cf上WA 了test2;
cf测试点数据太大了看不到wa了哪一个(不是超时应该也不是数组越界),找别人的ac代码用sublime text跑了一千多个test都找不到问题()麻烦帮忙hack இ௰இ Orz
#include <iostream>
#include <vector>
#include <map>
#include <utility>
using namespace std;
int main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int t;
cin >> t;
while (t--){
int n,q,l,r;
cin >> n;
vector<int>v(n+5); // 原数组
map<int,pair<int,int>>m; // 自身的下标,<左边第一个不等于自身的值的下标,右边第一个不等于自身的值的下标>
for (int i = 1; i <= n; i++) cin >> v[i];
// 预处理
// 先处理每个数据左边第一个和它不相同的值的下标(先从左到右遍历)
int lastlastindex = 1,lastlastvalue = v[1];
int lastindex = 1,lastvalue = v[1];
for (int i = 2; i <= n; i++){
if (v[i] != lastvalue){
m[i].first = lastindex;
lastlastindex = lastindex;
lastlastvalue = lastvalue;
lastindex = i;
lastvalue = v[i];
}
else{
if (lastvalue != lastlastvalue){
m[i].first = lastlastindex;
}
else m[i].first = m[lastindex].first;
}
}
// 再处理每个数据右边第一个和它不相同的值的下标(再从右到左遍历)
lastlastindex = n;
lastlastvalue = v[n];
lastindex = n;
lastvalue = v[n];
for (int i = n-1; i >= 1; i--){
if (v[i] != lastvalue){
m[i].second = lastindex;
lastlastindex = lastindex;
lastlastvalue = lastvalue;
lastindex = i;
lastvalue = v[i];
}
else{
if (lastvalue != lastlastvalue){
m[i].second = lastlastindex;
}
else m[i].second = m[lastindex].second;
}
}
// cout << "debug: \n";
// for (auto x:m) cout << x.first << ",l: " << x.second.first << ",r: " << x.second.second << endl;
cin >> q;
while (q--){
cin >> l >> r;
if (m[l].second != 0 && m[l].second <= r) cout << l << " " << m[l].second << "\n";
else if (m[r].first != 0 && m[r].first >= l) cout << m[r].first << " " << r << "\n";
else cout << -1 << " " << -1 << "\n";
}
cout << "\n";
}
return 0;
}