-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path022-noip-05-p3-fire.cpp
More file actions
87 lines (86 loc) · 1.94 KB
/
Copy path022-noip-05-p3-fire.cpp
File metadata and controls
87 lines (86 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
* NOIP '05 P3: Fire [noip05p3]
* Problem: https://dmoj.ca/problem/noip05p3
* Verdict: ACCEPTED (15.0 pts) Solved: 2023-01-06
* Language: C++20
* Runtime: 0.131709255 s Memory: 4940.0 KB
* Source: https://dmoj.ca/src/5190467
*/
/*
ID: BidoTeima
LANG: C++11
TASK:
*/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void moo(string filename);
void ACPLS(string str = "")
{
if(str=="NOF")return;
if(str.size())
moo(str);
else{
#ifndef ONLINE_JUDGE
freopen("output.txt", "w", stdout);
freopen("input.txt", "r", stdin);
#endif
}
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
void moo(string fileName){
freopen((fileName+".in").c_str(),"r",stdin);
freopen((fileName+".out").c_str(),"w",stdout);
}
#define tc \
int tttttt/*,subtask*/; \
cin >> tttttt/* >> subtask*/; \
while (tttttt--)/*end
*/
int f(int n, vector<int>l,vector<int>r){
vector<int>v;
v.push_back(0);
while((int)v.size() != n){
if(v.size() > 1){
if(v[v.size() - 2] == r[v.back()]){
swap(r[v.back()],l[v.back()]);
}
}
v.push_back(r[v.back()]);
}
bool ok = 1;
for(int i = 0; i < n; i++){
vector<int>mates = {v[(i + n - 1) % n],v[(i + 1) % n]};
vector<int>wanted = {l[v[i]],r[v[i]]};
sort(mates.begin(),mates.end());
sort(wanted.begin(),wanted.end());
if(mates!=wanted){
ok = 0;
break;
}
}
if(!ok){
return 1e9;
}
int freq[n]={0};
for(int i = 0; i < n; i++){
freq[(i - v[i] + n) % n]++;
}
return n - *max_element(freq,freq+n);
}
int main()
{
ACPLS("");
int n;
cin>>n;
vector<int>l(n),r(n);
for(int i = 0; i < n; i++){
cin>>l[i]>>r[i];
--l[i],--r[i];
}
int ans = f(n,l,r);
swap(l[0],r[0]);
cout<<(min(ans,f(n,l,r))==1e9?-1:min(ans,f(n,l,r)));
}