文章目录
- A - Tiny Arithmetic Sequence
- B - Do you know the second highest mountain?
- C - Secret Number
- D - Game in Momotetsu World
- E - Xor Distances
- F - Insertion Sort
Mynavi Programming Contest 2021(AtCoder Beginner Contest 201)
A - Tiny Arithmetic Sequence
if−elseif-elseif−else直接判
B - Do you know the second highest mountain?
sortsortsort排序
C - Secret Number
本来以为要计数DPDPDP/生成函数,但是仔细一看密码位数固定只有444位,直接暴力枚举判断
D - Game in Momotetsu World
考试时一直正着跑两人交替,总是错;考虑过倒着跑回去,但是没有实现出来(自己想要的效果)
每个人都是聪明的,都想尽可能拉开与对方的正差距,缩小与对方的负差距
ci,j−dpi,jc_{i,j}-dp_{i,j}ci,j−dpi,j写得真的很妙
#include <cstdio>
#include <iostream>
using namespace std;
#define maxn 2005
int dp[maxn][maxn], c[maxn][maxn];
char s[maxn];
int n, m;int main() {scanf( "%d %d", &n, &m );for( int i = 1;i <= n;i ++ ) {scanf( "%s", s + 1 );for( int j = 1;j <= m;j ++ )c[i][j] = ( s[j] == '+' ? 1 : -1 );}for( int i = n;i;i -- )for( int j = m;j;j -- ) {if( i == n && j == m ) continue;else if( i == n ) dp[i][j] = c[i][j + 1] - dp[i][j + 1];else if( j == m ) dp[i][j] = c[i + 1][j] - dp[i + 1][j];else dp[i][j] = max( c[i + 1][j] - dp[i + 1][j], c[i][j + 1] - dp[i][j + 1] );}if( dp[1][1] > 0 ) printf( "Takahashi\n" );else if( dp[1][1] < 0 ) printf( "Aoki\n" );else printf( "Draw\n" );return 0;
}
E - Xor Distances
EEE竟然比DDD简单凸(艹皿艹 )eggs
di,j=dj,i⇒di,j=dk,i⨁dk,j=dk,i⨁dk,j⨁dx,k⨁dx,k=dx,i⨁dx,jd_{i,j}=d_{j,i}\Rightarrow d_{i,j}=d_{k,i}\bigoplus d_{k,j}=d_{k,i}\bigoplus d_{k,j}\bigoplus d_{x,k}\bigoplus d_{x,k}=d_{x,i}\bigoplus d_{x,j}di,j=dj,i⇒di,j=dk,i⨁dk,j=dk,i⨁dk,j⨁dx,k⨁dx,k=dx,i⨁dx,j
两个点之间的最短距离异或等于任选一个点做超级点,超级点到两个点的最短距离异或和(不妨就设为111)
异或是二进制操作,各位独立,考虑拆解每一位iii
如果iii位有贡献2i2^i2i,那么一定是两个点到111的距离第iii位异或为10=11^0=110=1
所以拆开每位分别统计有多少个点的距离该位为111,乘法原理,任何一个都可以和该位不是111的任何一个组起来
#include <cstdio>
#include <vector>
using namespace std;
#define maxn 200005
#define int long long
#define mod 1000000007
vector < pair < int, int > > G[maxn];
int n;
int dep[maxn];void dfs( int u, int fa ) {for( int i = 0;i < G[u].size();i ++ ) {int v = G[u][i].first, w = G[u][i].second;if( v == fa ) continue;else dep[v] = dep[u] ^ w, dfs( v, u );}
}signed main() {scanf( "%lld", &n );for( int i = 1, u, v, w;i < n;i ++ ) {scanf( "%lld %lld %lld", &u, &v, &w );G[u].push_back( make_pair( v, w ) );G[v].push_back( make_pair( u, w ) );}dfs( 1, 0 );int ans = 0;for( int j = 0;j < 60;j ++ ) {int cnt = 0;for( int i = 1;i <= n;i ++ )if( 1ll << j & dep[i] ) cnt ++;else;ans = ( ans + ( 1ll << j ) % mod * cnt % mod * ( n - cnt ) % mod ) % mod;}printf( "%lld\n", ans );return 0;
}
F - Insertion Sort
显然,对于每个数最多只会操作一次,假设不动数的集合为SSS,且S1<S2<...<SxS_1<S_2<...<S_xS1<S2<...<Sx
那么其必定满足posS1<posS2<...<posSxpos_{S_1}<pos_{S_2}<...<pos_{S_x}posS1<posS2<...<posSx且对于某个点i,i∉Si,i∉Si,i∈/S
- i:Aii:A_ii:Ai
- posi<S1:Bipos_i<S_1:B_iposi<S1:Bi
- Sx<posi:CiS_x<pos_i:C_iSx<posi:Ci
设dpi:Sdp_i:Sdpi:S中最大下标为iii时的最小操作数
dpi=min(∑j=1i−1min(Aj,Bj),minj<i,posj<posi(dpj+∑k=j+1i−1Ak)dp_i=min(\sum_{j=1}^{i-1}min(A_j,B_j),\min_{j<i,pos_j<pos_i}(dp_j+\sum_{k=j+1}^{i-1}A_k)dpi=min(∑j=1i−1min(Aj,Bj),minj<i,posj<posi(dpj+∑k=j+1i−1Ak)
对posipos_iposi建线段树,logloglog查询
最后的答案即为min(dpi+∑j=i+1nmin(Aj,Cj))min(dp_i+\sum_{j=i+1}^nmin(A_j,C_j))min(dpi+∑j=i+1nmin(Aj,Cj))
#include <cstdio>
#include <iostream>
using namespace std;
#define inf 1e15
#define maxn 200005
#define int long long
int n;
int P[maxn], A[maxn], B[maxn], C[maxn];
int Asum[maxn], Bsum[maxn], Csum[maxn], pos[maxn];
int dp[maxn], t[maxn << 2];void modfiy( int num, int l, int r, int p, int v ) {if( l == r ) {t[num] = v;return;}int mid = ( l + r ) >> 1;if( p <= mid ) modfiy( num << 1, l, mid, p, v );else modfiy( num << 1 | 1, mid + 1, r, p, v );t[num] = min( t[num << 1], t[num << 1 | 1] );
}int query( int num, int l, int r, int L, int R ) {if( L <= l && r <= R ) return t[num];int mid = ( l + r ) >> 1, ans = inf;if( L <= mid ) ans = min( ans, query( num << 1, l, mid, L, R ) );if( mid < R ) ans = min( ans, query( num << 1 | 1, mid + 1, r, L, R ) );return ans;
}signed main() {scanf( "%lld", &n );for( int i = 1;i <= n;i ++ ) {scanf( "%lld", &P[i] );pos[P[i]] = i;}for( int i = 1;i <= n;i ++ )scanf( "%lld %lld %lld", &A[i], &B[i], &C[i] );for( int i = 1;i <= n;i ++ ) {Asum[i] = Asum[i - 1] + A[i];Bsum[i] = Bsum[i - 1] + min( A[i], B[i] );Csum[i] = Csum[i - 1] + min( A[i], C[i] );}int ans = inf;for( int i = 1;i <= n;i ++ ) {dp[i] = min( Bsum[i - 1], query( 1, 1, n, 1, pos[i] ) + Asum[i - 1] );ans = min( ans, dp[i] + Csum[n] - Csum[i] );modfiy( 1, 1, n, pos[i], dp[i] - Asum[i] );}printf( "%lld\n", ans );return 0;
}