Codeforces Round #791 (Div. 2) A - D 题解
A. AvtoBus
直接判断就好了,大的话就尽量用4,小的话就尽量用6,然后根据取余的关系找就行了
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
#include <functional>
#include <map>
#include <set>
#include <cmath>
#include <cstring>
#include <deque>
#include <stack>
using namespace std;
typedef long long ll;
#define pii pair<int, int>
const ll maxn = 2e5 + 10;
const ll inf = 1e17 + 10;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while(t--)
{
ll n;
cin >> n;
ll a = 0, b = 0;
if(n <= 3 || n % 2 == 1) cout << -1 << endl;
else
{
n >>= 1;
b = n >> 1;
a = n / 3;
if(n % 3 == 1) a++;
else if(n % 3 == 2) a++;
cout << a << " " << b << endl;
}
}
return 0;
}
B. Stone Age Problem
只要在更改的时候,判断一下更改时间的先后,再进行更改就行了,这样就是 \(O(1)\) 的修改
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
#include <functional>
#include <map>
#include <set>
#include <cmath>
#include <cstring>
#include <deque>
#include <stack>
using namespace std;
typedef long long ll;
#define pii pair<int, int>
const ll maxn = 2e5 + 10;
const ll inf = 1e17 + 10;
pii num[maxn];
int main()
{
int n, m;
scanf("%d%d", &n, &m);
long long ans = 0;
for(int i=0; i<n; i++)
{
scanf("%d", &num[i].first);
num[i].second = 0;
ans += num[i].first;
}
ll pre = 0, val = 0;
for(int i=1; i<=m; i++)
{
int t;
scanf("%d", &t);
if(t == 1)
{
int a, b;
scanf("%d%d", &a, &b);
a--;
if(num[a].second < pre)
{
num[a].first = val;
num[a].second = pre;
}
ans -= num[a].first;
ans += b;
num[a].first = b;
num[a].second = i;
}
else
{
ll a;
scanf("%lld", &a);
ans = n * a;
pre = i;
val = a;
}
printf("%lld\n", ans);
}
return 0;
}
C. Rooks Defenders
用树状数组维护一下行和列是否有车存在,为了防止同行或同列有多个车的存在,还要开两个数组去维护同行同列有多少个车的数量,然后在删除和添加时,车的数量 0-1 变换的时候才用树状数组维护
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
#include <functional>
#include <map>
#include <set>
#include <cmath>
#include <cstring>
#include <deque>
#include <stack>
using namespace std;
typedef long long ll;
#define pii pair<int, int>
const ll maxn = 2e5 + 10;
const ll inf = 1e17 + 10;
int r[maxn], v[maxn];
int row[maxn], vol[maxn];
int n, m;
inline int lowbit(int x)
{
return x & (-x);
}
void add(int x, int val, int* way)
{
for(int i=x; i<=n; i+=lowbit(i))
way[i] += val;
}
int query(int x, int* way)
{
int ans = 0;
for(int i=x; i; i-=lowbit(i))
ans += way[i];
return ans;
}
int main()
{
scanf("%d%d", &n, &m);
while(m--)
{
int t, a, b, c, d;
scanf("%d%d%d", &t, &a, &b);
if(t == 3)
{
scanf("%d%d", &c, &d);
int x = query(c, r) - query(a - 1, r);
int y = query(d, v) - query(b - 1, v);
if(x == c - a + 1 || y == d - b + 1)
printf("Yes\n");
else
printf("No\n");
}
else if(t == 1)
{
if(++row[a] == 1)
add(a, 1, r);
if(++vol[b] == 1)
add(b, 1, v);
}
else
{
if(--row[a] == 0)
add(a, -1, r);
if(--vol[b] == 0)
add(b, -1, v);
}
}
return 0;
}
D. Toss a Coin to Your Graph...
二分 + 记忆化搜索
答案是单调的,所以直接二分答案,然后检查的时候就记忆化搜索,看看在限制当前最高值的情况下,能不能走 k 步,如果走的发现是个环,则直接返回可以走 k 步就行
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
#include <functional>
#include <map>
#include <set>
#include <cmath>
#include <cstring>
#include <deque>
#include <stack>
using namespace std;
typedef long long ll;
#define pii pair<int, int>
const ll maxn = 2e5 + 10;
const ll inf = 1e17 + 10;
int num[maxn];
int a[maxn], vis[maxn];
ll dp[maxn];
vector<int>gra[maxn];
ll n, m, k;
ll dps(int now, int x)
{
if(dp[now]) return dp[now] == -1 ? 0 : dp[now];
if(num[now] > x) {dp[now] = -1; return 0;}
vis[now] = 1;
ll ans = 0;
for(int i=0; i<gra[now].size() && ans < k; i++)
{
int nex = gra[now][i];
if(vis[nex]) ans = k;
else
ans = max(ans, dps(nex, x));
}
vis[now] = 0;
return dp[now] = ans + 1;
}
bool judge(int x)
{
for(int i=1; i<=n; i++) dp[i] = vis[i] = 0;
ll ans = 0;
for(int i=1; i<=n && ans < k; i++)
ans = max(ans, dps(i, x));
return ans >= k;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m >> k;
for(int i=1; i<=n; i++) {cin >> num[i]; a[i] = num[i];}
while(m--)
{
int a, b;
cin >> a >> b;
gra[a].push_back(b);
}
sort(a + 1, a + n + 1);
int l = 1, r = n;
while(l < r)
{
int mid = l + r >> 1;
if(judge(a[mid]))
r = mid;
else
l = mid + 1;
}
if(judge(a[l]))
cout << a[l] << endl;
else
cout << -1 << endl;
return 0;
}