Codeforce Round #614解题报告A~E
A. ConneR and the A.R.C. Markland-N
把楼堵着,然后双指针枚举
#include
using namespace std;
const int maxn = 1e3 + 10;
int a[maxn];
void solve()
{
int n, s, k;
cin >> n >> s >> k;
map mp;
for(int i = 1, x; i <= k; i++)
scanf("%d", &x), mp[x]++;
int ans = 0;
while(1)
{
if((!mp[ans+s] && ans + s <= n)|| (!mp[s-ans] && s-ans>=1) )
{
cout << ans << endl;
return;
}
ans++;
}
}
int main()
{
int T; scanf("%d", &T);
while(T--) solve();
}
B. JOE is on TV!
懒得看题就直接看样例去了,盲猜就过了。。。
#include
using namespace std;
int main()
{
double n; cin >> n;
double ans = 0;
for(int i = n; i >= 1; i--)
ans += 1.0/(double)i;
printf("%.12f", ans);
return 0;
}
C. NEKO's Maze Game
关键就在于什么样的格局可以阻断道路。
假如我现在在\((1,x)\)点上放了一个障碍,那么在\((2,x-1),(2,x),(2,x+1)\)放置障碍都会阻断道路。
那么就根据这个判断,同时记录一下障碍的数量。
#include
using namespace std;
const int maxn = 1e5 + 10;
map, int> mp;
int n, q;
int num;
int main()
{
cin >> n >> q;
for(int i = 1, x, y; i <= q; i++)
{
scanf("%d%d", &x, &y);
if(x == 2) x = 0;
mp[{x,y}]++;
if(mp[{x,y}] % 2 == 1)
{
if(mp[{x^1, y-1}]%2 || mp[{x^1, y}]%2 || mp[{x^1, y+1}]%2)
{
puts("NO");
if(mp[{x^1, y-1}]%2) num++;
if(mp[{x^1, y}]%2) num++;
if(mp[{x^1, y+1}]%2) num++;
}
else
{
if(num) puts("NO");
else puts("YES");
}
}
else
{
if(mp[{x^1, y+1}]%2 || mp[{x^1, y}]%2 || mp[{x^1, y-1}]%2)
{
if(mp[{x^1, y+1}]%2) num--;
if(mp[{x^1, y}]%2) num--;
if(mp[{x^1, y-1}]%2) num--;
}
if(num == 0) puts("YES");
else puts("NO");
}
}
return 0;
}
D. Aroma's Search
每个点的坐标是一个阶乘级的递推式,而且明确的给出了范围在\(1e16\)以内。
这就说明点不会特别多,最多也就60来个点。
所以暴力枚举点的可能。
#include
using namespace std;
const int maxn = 1e5 + 10;
typedef long long ll;
ll x[maxn], y[maxn];
ll cnt, ax, ay, bx, by, xs, ys, t, ans;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> x[0] >> y[0] >> ax >> ay >> bx >> by >> xs >> ys >> t;
for(int i = 1; ; i++)
{
x[i] = ax*x[i-1] + bx;
y[i] = ay*y[i-1] + by;
if(x[i] >= xs+t || y[i] >= ys+t)
{
cnt = i;
break;
}
}
for(int i = 0; i < cnt; i++)
{
for(int j = 0; j < cnt; j++)
{
ll d = abs(x[i]-xs)+abs(y[i]-ys)+abs(x[j]-x[i])+abs(y[j]-y[i]);
if(d <= t) ans = max(ans, abs(j-i) + 1ll);
}
} cout << ans << endl;
return 0;
}
E. Xenon's Attack on the Gangs
推荐up主的讲解:https://www.bilibili.com/video/av84326197?p=5
#include
#define pb push_back
using namespace std;
typedef long long ll;
const int maxn = 3e3 + 10;
ll f[maxn][maxn];
ll cnt[maxn][maxn];
int fa[maxn][maxn], n;
vector g[maxn];
void dfs(int x, int faa, int root)
{
cnt[root][x] = 1;
fa[root][x] = faa;
for(auto v : g[x])
{
if(v == faa) continue;
dfs(v, x, root);
cnt[root][x] += cnt[root][v];
}
}
ll solve(int x, int y)
{
if(x == y) return 0;
if(f[x][y] != -1) return f[x][y];
f[x][y] = cnt[y][x]*cnt[x][y]+max(solve(fa[y][x], y), solve(x, fa[x][y]));
return f[x][y];
}
int main()
{
scanf("%d", &n);
for(int i = 1, x, y; i < n; i++)
{
scanf("%d%d", &x, &y);
x--; y--;
g[x].pb(y); g[y].pb(x);
}
for(int i = 0; i < n; i++) dfs(i, -1, i);
memset(f, -1, sizeof f);
ll ans = 0;
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
ans = max(ans, solve(i, j));
cout << ans << endl;
return 0;
}