-
Notifications
You must be signed in to change notification settings - Fork 1
/
A_And_Matching.cpp
317 lines (277 loc) · 7.08 KB
/
A_And_Matching.cpp
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define sza(x) ((int)x.size())
#define all(x) x.begin(), x.end()
#define revall(x) x.rbegin(), x.rend()
#define sortall(x) sort(all(x))
const int MAX_N = 2e5 + 5;
const int MAX_NN =250010;
const ll MOD = 1e9 + 7;
const ll INF = 1e9;
#define revall(x) x.rbegin(), x.rend()
#define ALL(x) sort(x.begin(), x.end())
#define sortall(x) sort(all(x))
#define reverseall(x) reverse(all(x))
#define SCD(t) scanf("%d",&t)
#define SCLD(t) scanf("%ld",&t)
#define SCLLD(t) scanf("%lld",&t)
#define SCC(t) scanf("%c",&t)
#define SCS(t) scanf("%s",t)
#define SCF(t) scanf("%f",&t)
#define SCLF(t) scanf("%lf",&t)
#define MEM(a, b) memset(a, (b), sizeof(a))
#define FOR(i, j, k, in) for (int i=j ; i<k ; i+=in)
#define RFOR(i, j, k, in) for (int i=j ; i>=k ; i-=in)
#define REP(i, j) FOR(i, 0, j, 1)
#define RREP(i, j) RFOR(i, j, 0, 1)
#define rall(cont) cont.end(), cont.begin()
#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define IN(A, B, C) assert( B <= A and A <= C)
#define MP make_pair
#define PB push_back
#define PF push_front
#define INF (int)1e9
#define EPS 1e-9
#define MOD 1000000007
#define PI 3.1415926535897932384626433832795
#define read(type) readInt<type>()
const double pi=acos(-1.0);
#define ll long long
typedef pair<long,long> PII;
typedef vector<long long> VI;
typedef vector<bool> VB;
typedef vector<char> VC;
typedef vector<vector<ll>> VII;
typedef vector<pair<int,bool>>VPBI;
typedef vector<pair<int,int>>VPII;
typedef vector<string> VS;
typedef vector<PII> VIII;
typedef vector<VI> VVI;
// typedef vector<string> VS;
typedef map<int,int> MPII;
typedef set<long long> SETI;
typedef multiset<int> MSETI;
typedef priority_queue<pair<int,int>> PQPII;
typedef long int int32;
typedef unsigned long int uint32;
typedef long long int int64;
typedef unsigned long long int uint64;
#define ff(a,b) for(int i=a;i<b;i++)
#define nl endl
/* clang-format off */
/* TYPES */
#define ll long long
#define pii pair<int, int>
#define pll pair<long long, long long>
#define vvi vector<vector<int>>
#define vll vector<long long>
#define vd vector<double>
#define mii map<long,long>
#define MPC map<char,int>
#define miid map<double,long>
#define si set<int>
#define sc set<char>
#define vi vector<int>
/* FUNCTIONS */
#define f1(i,s,e) for(long long int i=s;i<e;i++)
#define ff1(i,s,e) for(long long int i=s;i>=e;--i)
#define For1(i,n) or(long long int i=0;i<n+1;++i)
#define cf(i,s,e) for(long long int i=s;i<=e;i++)
#define FO(i,s,e) for(long long int i=1;i*i<=y;i++)
#define rf(i,e,s) for(long long int i=n-1;i>=0;i--)
#define pass(a) for(long long int i=n-1;i>=1;i-=2)
#define pb push_back
#define eb emplace_back
/* PRINTS */
template <class T>
void print_v(vector<T> &v) { cout << "{"; for (auto x : v) cout << x << ","; cout << "\b}"; }
/* UTILS */
ll min(ll a,int b) { if (a<b) return a; return b; }
ll min(int a,ll b) { if (a<b) return a; return b; }
ll max(ll a,int b) { if (a>b) return a; return b; }
ll max(int a,ll b) { if (a>b) return a; return b; }
ll gcd(ll a,ll b) { if (b==0) return a; return gcd(b, a%b); }
ll lcm(ll a,ll b) { return a/gcd(a,b)*b; }
string to_upper(string a) { for (int i=0;i<(int)a.size();++i) if (a[i]>='a' and a[i]<='z') a[i]-='a'-'A'; return a; }
string to_lower(string a) { for (int i=0;i<(int)a.size();++i) if (a[i]>='A' and a[i]<='Z') a[i]+='a'-'A'; return a; }
void yes() { cout<<"YES\n"; }
void no() { cout<<"NO\n"; }
/* All Required define Pre-Processors and typedef Constants */
typedef long int int32;
typedef unsigned long int uint32;
typedef long long int int64;
typedef unsigned long long int uint64;
bool isprime(ll n)
{
ll x=sqrt(n);
for(int i=2;i<=x;i++)
{if(n%i==0)return true;}
return false;
}
bool cmp(pair<int,int>x,pair<int,int>y){
return x.second<y.second;
}
void two_D_PrifixSum()
{
//prefix sum in 2 D array
int n,m;cin>>n>>m;
int a[n][m],prefix[n][m];
f1(i,0,n)
{
f1(j,0,m)
{
cin>>a[i][j];
}
}
//create a prefix array
f1(i,0,n)
{
f1(j,0,m)
{
prefix[i][j]=a[i][j];
if(i-1>=0)prefix[i][j]+=prefix[i-1][j];
if(j-1>=0)prefix[i][j]+=prefix[i][j-1];
if(i-1>=0 and j-1>=0)
{
prefix[i][j]-=prefix[i-1][j-1];
}
}
}
int q;cin>>q;
while(q--)
{
int i1,i2,j1,j2;cin>>i1>>i2>>j1>>j2;
}
}
//Scanline algorithm
void scanlineAlgoritm()
{
int n;cin>>n;
int a[n];
f1(i,0,n)cin>>a[i];
int prefixSum[n+1];
int q;cin>>q;
while(q--)
{
int l,r,x;cin>>l>>r>>x;
prefixSum[l]+=x;
prefixSum[r+1]-=x;
}
int s=0;
f1(i,0,n)
{
s+=prefixSum[i];
a[i]+=s;
}
f1(i,0,n)cout<<a[i]<<" ";
}
int findMin(vector<bool>&vis,VI distance)
{
int start=-1;
int n=vis.size();
f1(i,0,n)
{
if(start==-1 or distance[start]>distance[i])
start=i;
}
return start;
}
int dist(VPII vis,int n)
{
vector<bool>ar(n,false);
VI temp(n,INF);
ar[1]=0;
int val=findMin(ar,temp);
if(val==-1)return 0;
ar[val]=true;
cf(i,0,n)
{
return temp[n];
}
}
bool binarySearch(ll mid,ll k,ll x)
{
ll tot=0;
mid-=1;
if(mid<=k)
tot=(mid*(mid+1))/2;
else
{
tot=(k*(k+1))/2;
mid=2*k-1-mid;
tot+=((k*(k-1))/2-(mid*(mid+1))/2);
}
return tot<x;
}
/*
0 0 2 0 0 0 0 0
1 0 0 0 0 0 0 0
0 0 2 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
*/
void test()
{
int a[8][8];
f1(i,0,8)
{
f1(j,0,8)
{a[i][j]=0;}
}
int row,cols;cin>>row>>cols;
a[row][cols]=1;
// if(row>2 and row<8)
if(cols>=2 and cols<=8)
{
/*
case 1: n pos row and 2nd col then 2
case 2: n-1 pos row and 2nd pos col then 2
case 3: n pos row and m-1 col then 2
case 4: n-1 pos row and m pos col then 2
*/
a[row+1][cols+2]=2;
a[row+1][cols-2]=2;
a[row-1][cols+2]=2;
a[row-1][cols-2]=2;
}
else if(cols<=2){a[row+1][cols+2]=2;a[row-1][cols+2]=2;}
f1(i,0,8)
{
f1(j,0,8)
{cout<<a[i][j]<<" ";}cout<<nl;
}
}
void solve()
{
// int n,k;cin>>n>>k;int a[10];
// if(k==1){cout<<-1<<nl;continue;}
// f1(i,0,n)
// {a[i]=i;}
// swap(a[n-1],a[n-k-1]);
// f1(i,0,n/2){cout<<a[i]<<" "<<a[n-1-i]<<nl;}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin); //file input.txt is opened in reading mode i.e "r"
freopen("output.txt","w",stdout); //file output.txt is opened in writing mode i.e "w"
#endif
ll tc;
cin >> tc;
for (int t = 1; t <= tc; t++) {
// cout << "Case #" << t << ": ";
// solve();
int n,k;cin>>n>>k;int a[MAX_N];
if(k==n-1 and n==4){cout<<-1<<nl;continue;}
f1(i,0,n)
{a[i]=i;}
if(k==n-1){swap(a[n-1],a[n-k]);swap(a[n-3],a[n-4]);}
else{swap(a[n-1],a[n-k-1]);}
f1(i,0,n/2){cout<<a[i]<<" "<<a[n-1-i]<<nl;}
}
}