-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathcp.cpp
More file actions
160 lines (102 loc) · 2.69 KB
/
cp.cpp
File metadata and controls
160 lines (102 loc) · 2.69 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include<bits/stdc++.h>
using namespace std;
struct node{
long long val , cur;
int idx;
}a[100005];
bool operator <(node a , node b){
if(a.val == b.val)
return a.idx < b.idx;
return a.val > b.val;
}
long long sTree[400005];
long long sTree1[400005];
void buildTree(int root , int start, int end){
if(start == end){
sTree[root] = a[start].val;
sTree1[root] = a[start].cur;
return;
}
int mid = (start+end)/2;
buildTree(2*root+1 , start , mid);
buildTree(2*root+2 , mid+1 , end);
sTree[root] = max(sTree[2*root+1] , sTree[2*root+2]);
sTree1[root] = min(sTree1[2*root+1] , sTree1[2*root+2]);
}
void printTree(int n)
{
cout << "\nTREE: \n";
for(int i = 0; i < 2*n; i++)
cout << sTree[i] << " ";
cout << endl;
for(int i = 0; i < 2*n; i++)
cout << sTree1[i] << " ";
}
long long query(int root , int start , int end , int i , int j){
if(i > end || j < start)
return 0;
if(i <= start && j >= end)
return sTree[root];
int mid = (start+end)/2;
return max(query(2*root+1,start,mid,i,j) , query(2*root+2,mid+1,end,i,j));
}
long long query1(int root , int start , int end , int i , int j){
if(i > end || j < start)
return LLONG_MAX;
if(i <= start && j >= end)
return sTree1[root];
int mid = (start+end)/2;
return min(query1(2*root+1,start,mid,i,j) , query1(2*root+2,mid+1,end,i,j));
}
int minCut(int n){
buildTree(0,0,n-1);
sort(a,a+n);
int c = 0 , i = 0;
while(i < n){
if(a[i].val == a[i].cur){
i++;
continue;
}
int j = i+1;
int cc = 1;
while(j < n && a[j].val == a[i].val ){
long long bg = query(0 , 0, n -1 , a[j-1].idx , a[j].idx);
long long sm = query1(0 , 0, n -1 , a[j-1].idx , a[j].idx);
cout << bg << " " << sm << endl;
if(bg > a[i].val || sm < a[i].val){
if(cc > 0)
c++;
cc = 0;
}
if(a[j].val != a[j].cur)
cc++;
j++;
}
if(cc > 0)
c++;
i = j;
}
return c;
}
int main(){
int t , n;
cin >> t;
while(t--){
cin >> n;
bool f = false;
for(int i = 0 ; i < n ; i++)
cin >> a[i].cur;
for(int i = 0 ; i < n ; i++){
cin >> a[i].val;
if(a[i].cur < a[i].val)
f = true;
a[i].idx = i;
}
if(f){
cout << -1 << endl;
continue;
}
cout << "minCut " << minCut(n) << endl;
}
return 0;
}