-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path045-range-and-partition.cpp
More file actions
108 lines (107 loc) · 2.8 KB
/
Copy path045-range-and-partition.cpp
File metadata and controls
108 lines (107 loc) · 2.8 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
* Range and Partition [1630B]
* Problem: https://codeforces.com/problemset/problem/1630/B
* Verdict: ACCEPTED Solved: 2022-08-28
* Language: C++17 (GCC 7-32)
* Runtime: 171 ms Memory: 6800 KB
* Tags: binary search, constructive algorithms, data structures, greedy, two pointers
* Author: BidoTeima
* Source: https://codeforces.com/contest/1630/submission/169980391
*/
/// isA AC
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<typename t1>
using ordered_set = tree<t1, null_type,less<t1>, rb_tree_tag,tree_order_statistics_node_update>;
using ll = long long;
void ACPLS()
{
#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--)
#define sumrange(l, r, arr) (l == 0 ? arr[r] : arr[r] - arr[l - 1])
#define all(v) v.begin(), v.end()
int main()
{
ACPLS();
tc{
int n,k;
cin>>n>>k;
int a[n];
int p[n+1]={0};
for(int&i:a){
cin>>i;
p[i]++;
}
for(int i = 2; i <= n; i++){
p[i]+=p[i-1];
}
int lo = 1, hi = n;
vector<pair<int,int>>v;
v.push_back({1,n});
for(int i = 0; i < k-1; i++){
v.push_back({i,i});
}
v.push_back({k-1,n-1});
while(lo <= hi){
int m = (lo + hi) / 2;
int x = -1, y = -1;
for(int i = m; i <= n; i++){
if(x == -1 || p[i] - p[i - m] > p[y] - p[x - 1]){
x = i - m + 1, y = i;
}
}
vector<pair<int,int>>cur;
cur.push_back({x,y});
int cnt = k - 1;
int prevr = 0, cntxy = 0, cntother = 0;
for(int i = 0; cnt && i < n - cnt; i++){
if(x <= a[i] && a[i] <= y){
cntxy++;
}
else cntother++;
if(cntxy > cntother){
cur.push_back({prevr,i});
prevr = i + 1;
cntxy = 0;
cntother = 0;
cnt--;
}
}
cntxy = 0, cntother = 0;
for(int i = prevr; i < n; i++){
if(x <= a[i] && a[i] <= y){
cntxy++;
}
else cntother++;
}
if(cntxy > cntother){
cur.push_back({prevr, n-1});
}
if(cur.size() == k + 1){
v = cur;
hi = m - 1;
}
else lo = m + 1;
}
--v[0].first,--v[0].second;
for(auto p : v)
cout<<p.first + 1<<' '<<p.second + 1<<'\n';
}
}