QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#474227#4925. Adjacent Pairsra0 1ms7920kbC++141.3kb2024-07-12 16:47:512024-07-12 16:47:51

Judging History

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

  • [2024-07-12 16:47:51]
  • 评测
  • 测评结果:0
  • 用时:1ms
  • 内存:7920kb
  • [2024-07-12 16:47:51]
  • 提交

answer

#include<iostream>
#include<cstdio>
#include<map>
#include<algorithm> 
#define mk make_pair
using namespace std;
const int N=3*1e5;
int t,n;
int a[N];
map<pair<int,int> , int > m;
struct aa{
	int cnt,id;
}odd[N];
aa even[N];
bool cmp(aa a,aa b){
	return a.cnt>b.cnt;
}

int main(){
	cin>>t;
	while(t--){
		int ans=99999999;
		m.clear();
		scanf("%d",&n);
		for(int i=1;i<=n;i++){
			scanf("%d",&a[i]);
		}
		int len=2;
		pair<int,int> d=mk(a[1],a[2]),e=mk(0,0);
		odd[a[1]].id=a[1];
		odd[a[1]].cnt=1;
		for(int i=3;i<=n;i+=2){
			odd[a[i]].cnt++;
			odd[a[i]].id=a[i];
			e=mk(a[i],a[i+1]);
			if(e!=d){
				m[d]+=len;
				len=2;
				d=e;
			}else
				len+=2;
		}
		m[e]+=len;
		len=2;
		d=mk(a[2],a[3]);
		e=mk(0,0);
		even[a[2]].cnt=1;
		even[a[2]].id=a[2];
		for(int i=4;i<=n;i+=2){
			even[a[i]].cnt++;
			even[a[i]].id=a[i];
			e=mk(a[i],a[i+1]);
			if(e!=d){
				m[d]+=len;
				len=2;
				d=e;
			}else len+=2;
		}
		sort(odd+1,odd+1+n,cmp);
		sort(even+1,even+1+n,cmp);
		for(int i=1;i<=n;i++){
			int c=odd[i].id;
			for(int j=1;j<=n;j++){
				int d=even[j].id;
				if(d==c)continue;
				ans=min(ans,n+m[mk(d,c)]/2-odd[i].cnt-even[j].cnt);
				if(m[mk(d,c)]==0)
				break;	
			}
		}
		cout<<ans<<endl;
	}
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 20
Accepted
time: 1ms
memory: 5744kb

input:

2
5
4 5 2 4 5
2
1 2

output:

3
0

result:

ok 2 lines

Test #2:

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

input:

1
9
1 2 1 2 3 1 2 1 2

output:

6

result:

ok single line: '6'

Test #3:

score: 0
Accepted
time: 1ms
memory: 7920kb

input:

1
7
6 5 4 1 2 6 5

output:

5

result:

ok single line: '5'

Test #4:

score: -20
Wrong Answer
time: 0ms
memory: 5664kb

input:

1
16
4 3 4 3 4 3 4 3 1 4 3 4 1 4 3 4

output:

11

result:

wrong answer 1st lines differ - expected: '10', found: '11'

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Skipped

Dependency #1:

0%

Subtask #4:

score: 0
Skipped

Dependency #1:

0%