文章目录
- A. Space Navigation
- B. New Colony
- C. Fence Painting
- D. AB Graph
- E. Sorting Books
- F. AB Tree
#699-Div.2
A. Space Navigation
对于最终位置(x,y)
,我们只关心那两个方向的字符是否足够即可
#include <cstdio>
#include <cstring>
#define maxn 100005
int T, n, m;
char s[maxn];int main() {scanf( "%d", &T );while( T -- ) {scanf( "%d %d %s", &n, &m, s + 1 );char x = n < 0 ? 'L' : 'R';char y = m < 0 ? 'D' : 'U';int len = strlen( s + 1 );int cntx = 0, cnty = 0;for( int i = 1;i <= len;i ++ ) {if( s[i] == x ) cntx ++;if( s[i] == y ) cnty ++;}if( n < 0 ) n = -n;if( m < 0 ) m = -m;if( cntx >= n && cnty >= m ) printf( "YES\n" );else printf( "NO\n" );}return 0;
}
B. New Colony
发现数据非常小,最暴力也只用滚1e51e51e5次就一定是−1-1−1,所以kkk那么大纯粹虾银
#include <cstdio>
#define maxn 105
int T, n, k;
int h[maxn];int main() {scanf( "%d", &T );while( T -- ) {scanf( "%d %d", &n, &k );for( int i = 1;i <= n;i ++ )scanf( "%d", &h[i] );int ans;for( int i = 1;i <= k;i ++ ) {int pos = 1;for( int j = 2;j <= n;j ++ )if( h[pos] >= h[j] ) pos ++;else {h[pos] ++;break;}ans = pos;if( ans == n ) break;}if( ans == n ) printf( "-1\n" );else printf( "%d\n", ans );} return 0;
}
C. Fence Painting
统计需要染色的颜色和个数,判断是否足够
对于无效的笔又必须使用,显然将之用到最后一次有效操作的木板上
这样之前的所有染色都会被覆盖掉
注意最后一次有效操作的模板查找
#include <cstdio>
#include <queue>
using namespace std;
#define maxn 100005
queue < int > q[maxn];
int T, n, m;
int a[maxn], b[maxn], c[maxn], cnt[maxn];
bool vis[maxn];int main() {scanf( "%d", &T );again :while( T -- ) {scanf( "%d %d", &n, &m );for( int i = 1;i <= n;i ++ ) {while( ! q[i].empty() ) q[i].pop();vis[i] = cnt[i] = 0;}for( int i = 1;i <= n;i ++ )scanf( "%d", &a[i] );for( int i = 1;i <= n;i ++ )scanf( "%d", &b[i] ), vis[b[i]] = 1;for( int i = 1;i <= m;i ++ )scanf( "%d", &c[i] ), cnt[c[i]] ++;if( ! vis[c[m]] ) {printf( "NO\n" );goto again;}int pos;for( int i = 1;i <= n;i ++ ) {if( a[i] != b[i] ) q[b[i]].push( i );if( b[i] == c[m] && ( a[i] != b[i] || ( a[i] == b[i] && ! q[b[i]].size() ) ) ) pos = i;}for( int i = 1;i <= n;i ++ )if( q[b[i]].size() > cnt[b[i]] ) {printf( "NO\n" );goto again;}printf( "YES\n" );for( int i = 1;i <= m;i ++ )if( ! q[c[i]].empty() ) {printf( "%d ", q[c[i]].front() );q[c[i]].pop();}else printf( "%d ", pos );printf( "\n" );}return 0;
}
D. AB Graph
大讨论就是猴赛雷!!!
-
m=2k+1m=2k+1m=2k+1
aba
aaa
都是回文串,由此可知不管边长什么样随便选两个点都是正确的在这里我们采取在
(1,2)
之间反复横跳 -
m=2km=2km=2k
-
先特判一下n=2n=2n=2的情况
-
存在Ea→b=Eb→aE_{a\rightarrow b}=E_{b\rightarrow a}Ea→b=Eb→a,那么只需要在这样一对边横跳即可,回文串只包含了一个字符
-
一个简单的三元环所经过的路径字符一样,直接绕环走
-
不存在那么完美的两种情况,这意味着一对边一定是
ab
类型的显然,任意三个点中一定会有至少一个点连出的两条边为
ab
(当控制另外两个点连出边一样时,第三个点的边就已确定)
- m2=3t\frac{m}{2}=3t2m=3t,走法
bab
- m2=3t+1\frac{m}{2}=3t+12m=3t+1,走法
abb
- m2=3t+2\frac{m}{2}=3t+22m=3t+2,走法
bba
- m2=3t\frac{m}{2}=3t2m=3t,走法
-
PS:代码虽然看着冗长,但本质是ctrl+c
ctrl+v
(#.#)
#include <cstdio>
#include <iostream>
using namespace std;
#define maxn 1005
int T, n, m;
bool flag;
char s[maxn][maxn];bool check0( int i, int j, int k ) {if( s[i][j] == 'b' && s[j][k] == 'a' && s[k][i] == 'b' ) return 1;else return 0;
}bool check1( int i, int j, int k ) {if( s[i][j] == 'b' && s[j][k] == 'b' && s[k][i] == 'a' ) return 1;else return 0;
}bool check2( int i, int j, int k ) {if( s[i][j] == 'a' && s[j][k] == 'b' && s[k][i] == 'b' ) return 1;else return 0;
}void print( int i, int j, int k ) {printf( "YES\n" );for( int p = 0;p <= ( m << 1 );p ++ ) {printf( "%d ", i );int t = i;i = j;j = k;k = t;}printf( "\n" );flag = 1;
}bool Check( int i, int j, int k ) {if( s[i][j] == s[j][k] && s[j][k] == s[k][i] ) return 1;else return 0;
}int check( int i, int j ) {for( int k = 1;k <= n;k ++ )if( s[i][j] == s[j][k] ) return k;return 0;
}void Print( int i, int j, int k ) {flag = 1;printf( "YES\n%d %d %d\n", i, j, k );
}int main() {scanf( "%d", &T );again :while( T -- ) {scanf( "%d %d", &n, &m );for( int i = 1;i <= n;i ++ )scanf( "%s", s[i] + 1 );if( m & 1 ) {int now = 1;printf( "YES\n" );for( int i = 0;i <= m;i ++ ) {printf( "%d ", now );now = 3 - now;}printf( "\n" );goto again;}else {for( int i = 1;i < n;i ++ )for( int j = i + 1;j <= n;j ++ )if( s[i][j] == s[j][i] ) {printf( "YES\n" );for( int k = 0;k <= m;k ++ ) {printf( "%d ", i );swap( i, j );}printf( "\n" );goto again;}if( n == 2 ) {printf( "NO\n" );goto again;}m >>= 1;flag = 0;if( m == 1 ) {int pos;if( pos = check( 1, 2 ) ) Print( 1, 2, pos );else if( pos = check( 1, 3 ) ) Print( 1, 3, pos );else if( pos = check( 2, 1 ) ) Print( 2, 1, pos );else if( pos = check( 2, 3 ) ) Print( 2, 3, pos );else if( pos = check( 3, 1 ) ) Print( 3, 1, pos );else if( pos = check( 3, 2 ) ) Print( 3, 2, pos );}else {flag = 0;int k = m % 3;switch( k ) {case 0 : {if( Check( 1, 2, 3 ) ) print( 1, 2, 3 );else if( check0( 1, 2, 3 ) ) print( 1, 2, 3 );else if( check0( 1, 3, 2 ) ) print( 1, 3, 2 );else if( check0( 2, 1, 3 ) ) print( 2, 1, 3 );else if( check0( 2, 3, 1 ) ) print( 2, 3, 1 );else if( check0( 3, 1, 2 ) ) print( 3, 1, 2 );else if( check0( 3, 2, 1 ) ) print( 3, 2, 1 );break;}case 1 : {if( Check( 1, 2, 3 ) ) print( 1, 2, 3 );else if( check1( 1, 2, 3 ) ) print( 1, 2, 3 );else if( check1( 1, 3, 2 ) ) print( 1, 3, 2 );else if( check1( 2, 1, 3 ) ) print( 2, 1, 3 );else if( check1( 2, 3, 1 ) ) print( 2, 3, 1 );else if( check1( 3, 1, 2 ) ) print( 3, 1, 2 );else if( check1( 3, 2, 1 ) ) print( 3, 2, 1 );break;}case 2 : {if( Check( 1, 2, 3 ) ) print( 1, 2, 3 );else if( check2( 1, 2, 3 ) ) print( 1, 2, 3 );else if( check2( 1, 3, 2 ) ) print( 1, 3, 2 );else if( check2( 2, 1, 3 ) ) print( 2, 1, 3 );else if( check2( 2, 3, 1 ) ) print( 2, 3, 1 );else if( check2( 3, 1, 2 ) ) print( 3, 1, 2 );else if( check2( 3, 2, 1 ) ) print( 3, 2, 1 );break;}}}if( ! flag ) printf( "NO\n" );}}return 0;
}
E. Sorting Books
如果直接去求操作谁,显然要考虑操作数量以及顺序问题,顺序问题一般都非常吃力不讨好
反过来从后往前考虑,去求不操的数量的最大值即可,这时候就不需要管顺序了
设dpi:[i,n]dp_i:[i,n]dpi:[i,n] 最大不操次数,li,ri:il_i,r_i:ili,ri:i 值出现的最左点和最右点,cntc:cnt_c:cntc: 到iii位置为止ccc值出现的个数
-
现在位置iii上的值的最左点就在iii处,那么贪心的可以选择[i,n][i,n][i,n]所有与aia_iai相同点不操作
dprai+1+cntaidp_{r_{a_i}+1}+cnt_{a_i}dprai+1+cntai
-
最左点不在iii处,贪心的也可以选择[i,n][i,n][i,n]所有出现的与aia_iai相同点不操作
cntaicnt_{a_i}cntai
-
位置iii操作
dpi+1dp_{i+1}dpi+1
#include <cstdio>
#include <iostream>
using namespace std;
#define maxn 500005
int n;
int a[maxn], l[maxn], r[maxn], cnt[maxn], dp[maxn];int main() {scanf( "%d", &n );for( int i = 1;i <= n;i ++ ) {scanf( "%d", &a[i] );if( ! l[a[i]] ) l[a[i]] = i;r[a[i]] = i;}for( int i = n;i;i -- ) {++ cnt[a[i]];dp[i] = dp[i + 1];if( i == l[a[i]] )dp[i] = max( dp[i], dp[r[a[i]] + 1] + cnt[a[i]] );elsedp[i] = max( dp[i], cnt[a[i]] );}printf( "%d\n", n - dp[1] );return 0;
}
F. AB Tree
-
每层点赋一样的,可以使答案最优
可以把每一层看成物品,物品的价值就是这一层的点数,跑一个01背包
如果存在一组物品使得它们的和恰好为xxx,那么就直接输出层数即可
-
非要修改的话,修改单层的叶子结点,可以使答案最优
先找到叶子节点最多的一层,试图通过改当前层的叶子节点的状态来弥补DPDPDP状态
在此基础上,需要套用bitset
优化即可卡过去
#include <cstdio>
#include <vector>
#include <bitset>
#include <iostream>
using namespace std;
#define maxn 100005
#define maxk 3000
vector < int > G[maxn], D[maxn];
int n, x, m;
int dep[maxn], num[maxn], id[maxn], w[maxn], leaf_cnt[maxn];
bool vis[maxn];
bitset < maxn > f[maxk];void dfs1( int u, int fa ) {dep[u] = dep[fa] + 1;m = max( m, dep[u] );num[dep[u]] ++;if( G[u].size() == 1 ) leaf_cnt[dep[u]] ++;for( int i = 0;i < G[u].size();i ++ ) {int v = G[u][i];if( v == fa ) continue;else dfs1( v, u );}
}void dfs2( int x, int y ) {if( ! x ) return;for( int i = 0;i < D[x].size();i ++ ) {if( w[x] > y || f[x - 1][y] ) break;y -= w[x];vis[D[x][i]] = 1;}dfs2( x - 1, y );
}int main() {scanf( "%d %d", &n, &x );for( int i = 2, fa;i <= n;i ++ ) {scanf( "%d", &fa );G[fa].push_back( i );G[i].push_back( fa );}dfs1( 1, 0 );int tot = 0;for( int i = 1;i <= n;i ++ ) {//merge depth i who has the same nodes in numbersif( ! id[num[i]] ) {id[num[i]] = ++ tot;w[tot] = num[i];D[tot].push_back( i );}else D[id[num[i]]].push_back( i );}f[0][0] = 1;for( int i = 1;i <= tot;i ++ ) {f[i] = f[i - 1];int siz = D[i].size();for( int j = 1;j <= siz;j <<= 1 ) {siz -= j;f[i] |= f[i] << j * w[i];}if( siz ) f[i] |= f[i] << siz * w[i];}if( f[tot][x] ) {printf( "%d\n", m );dfs2( tot, x );for( int i = 1;i <= n;i ++ )if( vis[dep[i]] ) printf( "a" );else printf( "b" );}else {printf( "%d\n", m + 1 );int ans;for( int i = x;~ i;i -- )if( f[tot][i] ) {ans = i;break;}dfs2( tot, ans );int pos = -1;for( int i = 1;i <= m;i ++ )if( ! vis[i] && leaf_cnt[i] >= x - ans ) {pos = i;break;}for( int i = 1;i <= n;i ++ )if( dep[i] == pos && G[i].size() == 1 ) {if( ans == x ) printf( "b" );else printf( "a" ), ans ++;}else {if( vis[dep[i]] ) printf( "a" );else printf( "b" );}}return 0;
}