-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cake Cutting
58 lines (45 loc) Β· 985 Bytes
/
Cake Cutting
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
#include <bits/stdc++.h>
using namespace std;
void solve(){
int m,n;
cin>>m>>n;
vector<array<int,2>>a(n);
for(int i=0;i<n;i++) cin>>a[i][0], a[i][0]--;
for(int i=0;i<n;i++) cin>>a[i][1], a[i][1]--;
vector<int>cnt(m);
int overlap=0;
for(auto [s,e]:a){
int ee= e;
if(s>e)ee+=m;
if(ee-s>=(m+1)/2) {
overlap++;
continue;
}
if(s<=e){
cnt[s]++;
cnt[e]--;
}
else{
cnt[0]++;
cnt[e]--;
cnt[s]++;
}
}
for(int i=1;i<m;i++){
cnt[i]+= cnt[i-1];
}
int ans=0;
for(int i=0;i<m;i++){
ans= max( ans,cnt[i]+ cnt[( i+(m+1)/2)%m]);
ans= max( ans, cnt[i]+ cnt[( i+m/2) %m ]);
}
cout<<ans+overlap << endl;
}
int main() {
// your code goes here
int t;
cin>>t;
while(t--){
solve();
}
}