Codeforces Round #703 (Div. 2)补题

题目

A

B

AC代码：

/*
* @Author: hesorchen
* @Date: 2020-11-26 09:12:46
* @LastEditTime: 2021-02-19 15:31:36
* @Description: 栽种绝处的花
*/
#include <bits/stdc++.h>
using namespace std;

int x[1010];
int y[1010];

int main()
{
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
for (int i = 1; i <= n; i++)
cin >> x[i] >> y[i];
sort(x + 1, x + 1 + n);
sort(y + 1, y + 1 + n);
if (n & 1)
cout << 1 << endl;
else
cout << 1ll * (abs(x[n / 2] - x[n / 2 + 1]) + 1) * (abs(y[n / 2] - y[n / 2 + 1]) + 1) << endl;
}

return 0;
}

C2

• position==1，那么最大值肯定在[2,n]，对这个区间进行二分，每次查询[1,mid]，如果结果不为1，那么说明最大值在[mid+1,n]，否则最大值在[2,mid]
• position==n，那么最大值肯定在[1,n-1]，对这个区间进行二分，每次查询[mid,n]，如果结果不为n，那么说明最大值在[1,mid-1]，否则最大值在[mid,n-1]
• position在[2,n-1]，我们再查询一次[1,position]，如果结果还是position，那么最大值在区间[1,position]，我们把position看做n，按照情况二处理即可。否则把position看做1，按照情况一处理。

AC代码：

/*
* @Author: hesorchen
* @Date: 2020-11-26 09:12:46
* @LastEditTime: 2021-02-19 15:39:29
* @Description: 栽种绝处的花
*/
#include <bits/stdc++.h>
using namespace std;

int main()
{
long long n;
cin >> n;
int pre = n, ans, flag = 1, res;
cout << "? " << 1 << ' ' << n << endl;
cout.flush();
cin >> pre;
if (n == 2)
{
int l = 1, r = 2;
cout << "? " << l << ' ' << r << endl;
cout.flush();
cin >> pre;
if (pre == l)
return cout << "! " << r << endl, 0;
else
return cout << "! " << l << endl, 0;
}
if (pre == 1) //情况1
{
int l = 2, r = n;
ans = n;
while (l <= r)
{
int mid = l + r >> 1;
cout << "? " << pre << ' ' << mid << endl;
cout.flush();
int temp;
cin >> temp;
if (temp != pre)
l = mid + 1;
else
{
r = mid - 1;
ans = mid;
}
}
}
else if (pre == n) //情况2
{
int l = 1, r = n - 1;
ans = 1;
while (l <= r)
{
int mid = l + r >> 1;
cout << "? " << mid << ' ' << pre << endl;
cout.flush();
int temp;
cin >> temp;
if (temp != pre)
r = mid - 1;
else
{
l = mid + 1;
ans = mid;
}
}
}
else //情况3
{
int l = 1, r = n;
int re1, re2;
cout << "? " << 1 << ' ' << pre << endl;
cout.flush();
cin >> re1;

if (re1 == pre) //类比情况2
{
int l = 1, r = pre - 1;
ans = 1;
while (l <= r)
{
int mid = l + r >> 1;
cout << "? " << mid << ' ' << pre << endl;
cout.flush();
int temp;
cin >> temp;
if (temp != pre)
r = mid - 1;
else
{
l = mid + 1;
ans = mid;
}
}
}
else //类比情况1
{
int l = pre + 1, r = n;
ans = r;
while (l <= r)
{
int mid = l + r >> 1;
cout << "? " << pre << ' ' << mid << endl;
cout.flush();
int temp;
cin >> temp;
if (temp != pre)
l = mid + 1;
else
{
r = mid - 1;
ans = mid;
}
}
}
}
cout << "! " << ans << endl;
return 0;
}

02-19 1189
08-04 2316
02-19 79