https://codeforces.com/contest/1998
C. Perform Operations to Maximize Score
解析:
容易发现,要想结果最大,那么一定是最大值序列中的最大值加上剩下数字的中位数。
情况一:最大值可能值经过加1操作,这种情况下将所有的k次操作全部操作到这个数上最优,因为,将加1操作直接操做上最终结果一定加1,而操作到其他数字上最终结果最优情况下也是加1,所以这样操作情况不会变得更差。
情况二:最大化中位数。这种情况可以使用二分来查找最大情况的中位数。
#include <iostream>
#include <string>
#include <cstring>
#include <cmath>
#include <ctime>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <math.h>
#include <map>
#include <sstream>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <stdio.h>
#include <tuple>
using namespace std;
typedef long long LL;
#define int LL
#define ld long double
const LL INF = 0x3f3f3f3f3f3f3f3f;
typedef unsigned long long ULL;
typedef pair<long long, long long> PLL;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
const int inf = 0x3f3f3f3f;
const LL Mod = 998244353;
const ld eps = 1e-12;
const int N = 2e5 + 10, M = 5e5 + 10;
int n, k;
struct Node {
int a, b;
}A[N];
bool cmp(const Node& a, const Node& b) {
if (a.a == b.a) {
return a.b < b.b;
}
return a.a < b.a;
}
bool check(int mid) {
int t = k, sum = (n)/2;
if ((n - 1) % 2 == 0)sum++;
for (int i = n-1; i > 0;i--) {
if (A[i].b) {
if (A[i].a >= mid)sum--;
else {
if (t >= mid - A[i].a) {
t -= mid - A[i].a;
sum--;
}
}
}
else {
if (A[i].a >= mid)sum--;
}
}
//cout << "_________" << sum <<" "<<mid<< endl;
if (sum <=0)return 1;
return 0;
}
signed main() {
int T;
cin >> T;
while (T--) {
scanf("%lld%lld", &n, &k);
for (int i = 1; i <= n; i++) {
scanf("%lld", &A[i].a);
}
for (int i = 1; i <= n; i++) {
scanf("%lld", &A[i].b);
}
sort(A + 1, A + 1 + n, cmp);
int ans = 0;
for (int i = n; i > 0; i--) {
if (A[i].b == 1) {
if (i == (n + 1) / 2)
if (n % 2)ans = A[i].a + k + A[(n + 1) / 2 - 1].a;
else ans = A[i].a + k + A[(n + 1) / 2 + 1].a;
else if (i > (n + 1) / 2)
if (n % 2)ans = A[i].a + k + A[(n + 1) / 2 - 1].a;
else ans = A[i].a + k + A[(n + 1) / 2].a;
else
if (n % 2)ans = A[i].a + k + A[(n + 1) / 2].a;
else ans = A[i].a + k + A[(n + 1) / 2 + 1].a;
break;
}
}
//n--;
int l = 0, r = INF, ret = l;
while (l <= r) {
int mid = l + (r - l) / 2;
if (check(mid)) {
ret = mid;
l = mid + 1;
}
else {
r = mid - 1;
}
}
//cout << "__________________________" << ans <<" "<<ret<< endl;
ans = max(ans, ret + A[n].a);
printf("%lld\n", ans);
}
return 0;
}
/*
8
2 10
3 3
1 1
3 10
3 3 3
0 0 0
4 4
2 1 5 1
0 1 0 1
5 4
7 5 2 5 4
0 0 1 0 1
5 1
5 15 15 2 11
1 0 0 1 1
5 2
10 11 4 10 15
1 1 0 1 0
4 4
1 1 2 5
1 1 0 0
2 1000000000
1000000000 1000000000
1 1
*/
D. Determine Winning Islands in Race
解析:
因为Bessie 只有一条路可以走,Elsie可以使用附加边,所以情况变得简单了很多。
观察发现,如果后手想要赢,就必须通过附加边超越先手。所以决定后手能不能赢的条件就只有附加边。
设 di 表示后手从1 到点 i 的距离,那么对于附加边 (u,v)而言,只有先手在 (u,v-di-1) 区间上后手才能赢。因此我们将者中后手能赢的区间都标记为1,用差分数组维护即可。
#include <iostream>
#include <string>
#include <cstring>
#include <cmath>
#include <ctime>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <math.h>
#include <map>
#include <sstream>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <stdio.h>
#include <tuple>
using namespace std;
typedef long long LL;
//#define int LL
/*
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
*/
#define ld long double
const LL INF = 0x3f3f3f3f3f3f3f3f;
typedef unsigned long long ULL;
typedef pair<long long, long long> PLL;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
const int inf = 0x3f3f3f3f;
const LL Mod = 998244353;
const ld eps = 1e-12;
const int N = 2e5 + 10, M = 5e5 + 10;
int T = 1;
int n, m;
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int T;
cin >> T;
while (T--) {
cin >> n >> m;
vector<vector<int>>g(n+2);
vector<int>sum(n + 2,0);
vector<int>dist(n + 2, inf);
for (int i = 1; i <= n; i++) {
g[i].push_back(i + 1);
}
for (int i = 1; i <= m; i++) {
int a, b;
cin >> a >> b;
g[a].push_back(b);
}
dist[1] = 0;
for (int i = 1; i <= n; i++) {
for (int x : g[i]) {
dist[x] = min(dist[x],dist[i] + 1);
int s = x - dist[i] - 2;
if (s > i) {
sum[s + 1]--;
sum[i + 1]++;
}
}
}
for (int i = 1; i < n; i++) {
sum[i] += sum[i - 1];
if (sum[i]) {
cout << "0";
}
else {
cout << "1";
}
}
cout << endl;
}
return 0;
}