-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInteractive_Problems_Guide.cpp
122 lines (102 loc) · 2.92 KB
/
Interactive_Problems_Guide.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
//Guidelines:
//1->Use endl. to flush the output. that's why I have disabled the macro (#define endl '\n') above
//2-> Turn off the fast io (below part) interactive problems
// ios::sync_with_stdio(false); cin.tie(NULL);
//3->Read the input.normally. Provide the output. then read again. as Usual.
//THe following code is the solution for the problem:
// https://codeforces.com/contest/1797/problem/C
#include <bits/stdc++.h>
using namespace std;
#define f first
//GuideLine 1
// #define endl '\n'
#define s second
#define pb push_back
#define md 998244353
#define int long long
#define vi vector<int>
#define mod 1000000007
#define inf 1999999999999999999
#define vp vector<pair<int,int>>
#define all(c) c.begin(),c.end()
#define rall(c) c.rbegin(),c.rend()
#define in(a) for(auto &i:a)cin>>i
#define dbg(x) cout<<#x<<"->"<<x<<endl
#define mem(a,val) memset(a,val,sizeof(a))
#define out(a)for(auto i:a)cout<<i<<" ";cout<<endl
#define ebg(x,y) cout<<#x<<"->"<<x<<" "<<#y<<"->"<<y<<endl
void solve() {
//GuideLine 3.1: Read Input as Usual
int n, m;
cin >> n >> m;
int ans = 0;
auto query = [&](int x, int y) {
cout << "? " << x << " " << y << endl;
cin >> ans;
return ans;
};
auto end = [&](int x, int y) {
cout << "! " << x << " " << y << endl;
};
//GuideLine 3.2: Recieve the Output based on the Input & Proceed Accordingly.
int cnt1 = query(1, 1);
//GuideLine 3.3:At Last if ans is found Output the Answer as Usual.
if (!cnt1)end(1, 1);
else {
int cdist = min(1 + cnt1, m);
int rdist = min(1 + cnt1, n);
int cnt2 = query(1, cdist);
if (cnt2 < rdist - 1) {
end(1 + cnt2, cdist);
}
else {
int cnt3 = query(rdist, cdist);
end(rdist, cdist - cnt3);
}
}
}
int32_t main()
{
//GuideLine 2:
// ios::sync_with_stdio(false); cin.tie(NULL);
int t = 1;
cin >> t;
while (t--) {
solve();
}
return 0;
}
//INteractive Problem Example 2: Using Binary Search
//Problem Link: https://codeforces.com/problemset/problem/1807/E
//Interactive CODE:
void solve() {
int n; cin >> n;
int a[n + 1] = {0};
for (int i = 1; i <= n; i++)cin >> a[i];
function<int(int, int)> query = [&](int l, int r) {
int sum = 0ll;
cout << "? " << r - l + 1 << " ";
while (l <= r) {cout << l << " "; l++;}
cin >> sum;
return sum;
};
int l = 1, h = n + 1;
int pre[n + 1] = {0};
for (int i = 1; i <= n; i++) {
pre[i] = pre[i - 1] + a[i];
}
int ans = 0;
while (l <= h) {
int mid = ((h - l) / 2) + l;
int weight = query(l, mid);
int stones = pre[mid] - (l > 0 ? pre[l - 1] : 0);
if (weight == stones) {
l = mid + 1;
}
else {
h = mid - 1;
ans = mid;
}
}
cout << "! " << ans << endl;
}