QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#393335#6545. Connect the DotsNahidameowWA 0ms3880kbC++201.8kb2024-04-18 15:23:392024-04-18 15:23:40

Judging History

你现在查看的是最新测评结果

  • [2024-04-18 15:23:40]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3880kb
  • [2024-04-18 15:23:39]
  • 提交

answer

#include<bits/stdc++.h>
#define pd push_back
#define all(A) A.begin(),A.end()
#define lower_bound lb
#define ve std::vector
typedef long long ll;
typedef long long ll;
typedef __int128 Int;
typedef unsigned long long ul;
typedef long double LD;
bool FileIfstream(std::string name){
	std::ifstream f(name.c_str());
	return f.good();
}
namespace Math{
	ll QP(ll x,ll y,ll mod){ll ans=1;for(;y;y>>=1,x=x*x%mod)if(y&1)ans=ans*x%mod;return ans;}
	ll inv(ll x,ll mod){return QP(x,mod-2,mod);}
}
const int N=2e5+10;
const int mod=998244353;
void solve(){
	//don't forget to open long long
	int n,m;std::cin>>n>>m;
	ve<int>v(n+1);
	ve<std::array<int,2>>ans;
	for(int i=1;i<=n;i++)std::cin>>v[i];
	for(int i=1;i<n;i++)
		if(v[i]!=v[i+1])ans.pd({i,i+1});
	ve<bool>del(n+1);
	auto special=[&](int x)->void{
		int L=x,R=x;
		for(int i=x-1;i>=1;i--)
			if(!del[i]){if(L^x)ans.pd({i,x});L=i;}
		for(int i=x+1;i<=n;i++)
			if(!del[i]){if(R^x)ans.pd({x,R});R=i;}
		if(v[L]!=v[R]&&L!=x&&R!=x)ans.pd({L,R});
	};
	ve<int>st;
	ve<int>cnt(m+1);
	for(int i=1;i<=n;i++)
		cnt[v[i]]++;
	for(int i=1;i<=n;i++){
		while(st.size()>=2&&v[st[st.size()-2]]!=v[i]){
			if(cnt[v[i]]==1){
				special(i);
				st.clear();
				goto flg;
			}
			ans.pd({st[st.size()-2],i});
			del[st.back()]=true;
			--cnt[v[st.back()]];
			st.pop_back();
		}
		st.pd(i);
	}
	for(int i=4;i<=st.size();i++)
		ans.pd({st[0],st[i-1]});
	flg:;
	std::cout<<ans.size()<<'\n';
	for(auto &p:ans)
		std::cout<<p[0]<<' '<<p[1]<<'\n';
}
int main(){
#ifndef ONLINE_JUDGE
	if(!FileIfstream("IO.in")){
		freopen("IO.in","w",stdout);
		return 0;
	}
	freopen("IO.in","r",stdin);
	freopen("IO.out","w",stdout);
#endif
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	std::cout.tie(0);
	int T=1;
	std::cin>>T;
	while(T--)solve();

	return 0;
}




详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 3880kb

input:

3
4 2
1 1 2 2
4 2
1 2 1 2
3 3
1 2 3

output:

3
2 3
1 3
1 4
4
1 2
2 3
3 4
1 4
3
1 2
2 3
1 3

result:

ok all 3 test passed

Test #2:

score: 0
Accepted
time: 0ms
memory: 3516kb

input:

1
2 2
1 2

output:

1
1 2

result:

ok all 1 test passed

Test #3:

score: -100
Wrong Answer
time: 0ms
memory: 3656kb

input:

10
5 2
2 2 2 1 2
5 2
2 1 2 1 2
5 2
1 2 2 2 1
5 2
2 1 2 1 1
5 2
1 1 1 2 1
5 2
1 2 2 1 2
5 2
2 1 1 2 2
5 2
2 2 2 1 1
5 2
1 1 2 1 2
5 2
1 2 2 2 1

output:

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

result:

wrong answer output = 6, answer = 5.