2019-7-9暑假第1场
P6284 流量(Tarifa.cpp)
签到题,注意加上第n+1天的流量。
显然,只有当
现在,我们只需要记录
2.向上飞。x--,y++
当
不然,则有两种情况。笨笨鸟持续向上飞(持续操作)或上下交替飞(间隔操作)。那我们如何记录呢?这时就到我们
我们只需要看最后一次操作的结束时间(开始时间+持续时间)是否等于当前时间
3.向下飞。x++,y++
当
显然 ,此时笨笨鸟没有进行操作,继续
最重要的是,题目要求输出任意一条路径,
#include <cstdio>
#include <vector>
#include <cstdlib>
using namespace std;
int n,m;
char Map[ 15 ][ 100005 ];
bool vis[ 15 ][ 100005 ] , f;
struct as{
int t,x;
as(){}
as( int T , int X ) {
t = T;
x = X;
}
};
vector< as > v;
void pr( ) {
int s = v.size();
printf("%d\n",s);
for( int i = 0 ; i < s ; i ++ )
printf("%d %d\n",v[ i ].t , v[ i ].x);
}
int dx[ 5 ] = { 0 , 0 , -1 , 1 } , dy[ 5 ] = { 0 , 1 , 1 , 1 };
void dfs( int x , int y , int t ) {
vis[ x ][ y ] = 1;
if( y == m ) {
pr( );
exit( 0 );
}
for( int i = 1 ; i <= 3 ; i ++ ) {
int fx = x + dx[ i ] , fy = y + dy[ i ];
if( fx < 1 || fx > n || fy < 1 || fy > m || Map[ fx ][ fy ] == 'X' || vis[ fx ][ fy ] )
continue;
if( i == 1 ) { //平飞
if( x == 1 ) {
v[ v.size( ) - 1 ].x ++;
dfs( fx , fy , t + 1 );
v[ v.size( ) - 1 ].x --;
}
if( x == n )
dfs( fx , fy , t + 1 );
}
if( i == 2 ) { //向上飞
if( v.size() > 0 && v[ v.size( ) - 1 ].t + v[ v.size() - 1 ].x == t ) {
v[ v.size( ) - 1 ].x ++;
dfs( fx , fy , t + 1 );
v[ v.size( ) - 1 ].x --;
}
else {
v.push_back( as( t , 1 ) );
dfs( fx , fy , t + 1 );
v.pop_back();
}
}
if( i == 3 ) //向下飞
dfs( fx , fy , t + 1 );
}
}
int main( ) {
//freopen("Jetpack.in","r",stdin);
//freopen("Jetpack.out","w",stdout);
scanf("%d",&m);
n = 10;
for( int i = 1 ; i <= n ; i ++ ) {
getchar( );
for( int j = 1 ; j <= m ; j ++ )
scanf("%c",&Map[ i ][ j ]);
}
dfs( n , 1 , 0 );
return 0;
}
solution 2
正解,用
整道题可分三种情况:
1.当x=1 时
笨笨鸟可以从
但我们要记录操作,所以需要分别判断。可以得到:
if( f[ x ][ y - 1 ] ) {
f[ x ][ y ] = f[ x ][ y - 1 ];
pre[ x ][ y ] = x;
}
if( f[ x + 1 ][ y - 1 ] ) {
f[ x ][ y ] = f[ x + 1 ][ y - 1 ];
pre[ x ][ y ] = x +1;
}
2.当1<x<10 时
笨笨鸟只能从
同上,得:
if( f[ x - 1 ][ y - 1 ] ) {
f[ x ][ y ] = f[ x - 1 ][ y - 1 ];
pre[ x ][ y ] = x - 1;
}
if( f[ x + 1 ][ y - 1 ] ) {
f[ x ][ y ] = f[ x + 1 ][ y - 1 ];
pre[ x ][ y ] = x + 1;
}
3.当x=10 时
笨笨鸟只能从
同上,得:
if( f[ x - 1 ][ y - 1 ] ) {
f[ x ][ y ] = f[ x - 1 ][ y - 1 ];
pre[ x ][ y ] = x - 1;
}
if( f[ x ][ y - 1 ] ) {
f[ x ][ y ] = f[ x ][ y - 1 ];
pre[ x ][ y ] = x;
}
最后,我们从最后一列可以到达的的点中随便找一个,以它开始向前回溯(使用
#include <cstdio>
#define N 100000
#define P 50000
int n , m , way;
char s[12][N + 2];
bool f[12][N + 2];
struct code {
int x , y;
}pre[12][N + 2]; //其实pre只需要储存x,y每次都会向前移动一格
struct Node {
int start , finish;
}move[P + 2];
void solve(int x , int y , int num) {
if(x == 10 and y == 1) return;
else solve(pre[x][y].x , pre[x][y].y , num - 1);
if(!m and pre[x][y].x == x) {
way = 0;
return;
}
if(pre[x][y].x == x + 1 and way != 1) {
way = 1;
move[++ m].start = num - 1;
return;
}
if(pre[x][y].x == x - 1 and way != 2) {
way = 2;
move[m].finish = num - 1;
move[m].finish -= move[m].start;
return;
}
}
int main() {
//freopen("Jetpack.in" , "r" , stdin);
//freopen("Jetpack.out" , "w" , stdout);
scanf("%d", &n);
for(int i = 1; i <= 10; i ++) {
getchar();
for(int j = 1; j <= n; j ++) scanf("%c", &s[i][j]);
}
f[10][1] = 1;
for(int j = 2; j <= n; j ++)
for(int i = 10; i >= 1; i --) {
if(s[i][j] == 'X') continue;
if(i == 10) {
if(f[i][j - 1]) {
f[i][j] = f[i][j - 1];
pre[i][j].x = i;
pre[i][j].y = j - 1;
}
else if(f[i - 1][j - 1]) {
f[i][j] = f[i - 1][j - 1];
pre[i][j].x = i - 1;
pre[i][j].y = j - 1;
}
}
else if(i == 1) {
if(f[i][j - 1]) {
f[i][j] = f[i][j - 1];
pre[i][j].x = i;
pre[i][j].y = j - 1;
}
else if(f[i + 1][j - 1]) {
f[i][j] = f[i + 1][j - 1];
pre[i][j].x = i + 1;
pre[i][j].y = j - 1;
}
}
else {
if(f[i - 1][j - 1]) {
f[i][j] = f[i - 1][j - 1];
pre[i][j].x = i - 1;
pre[i][j].y = j - 1;
}
else if(f[i + 1][j - 1]) {
f[i][j] = f[i + 1][j - 1];
pre[i][j].x = i + 1;
pre[i][j].y = j - 1;
}
}
}
int ex , ey = n;
for(int i = 1; i <= n; i ++)
if(f[i][n]) {
ex = i;
break;
}
way = -1;
solve(ex , ey , n - 1);
if(way == 1) move[m].finish = n - 1 - move[m].start;
printf("%d\n", m);
for(int i = 1; i <= m; i ++) printf("%d %d\n", move[i].start , move[i].finish);
return 0;
}
P6286 加密(Cezar.cpp)
正解Trie树+拓扑排序。不懂Trie树的点这。
P6287 魔力树(Mag.cpp)
此题有一个神仙结论:最优链上最多包含一个‘2’,且‘2’不能在链的两边。 至于证明过于简单但很麻烦(其实是我懒),给大家推荐一个博客,点这。
有了这个结论之后,我们就可以愉快的开始树形dp了。
我们用
1.当u为1时,
2.当u为2时,
我们再用
每次更新
最后我们更新答案,可能会出现以下情况:
1.当u的值为1时
1.
2.不存在该上关系但有含2,更新答案为
3.答案的链不含2,更新答案为
1.当u的值为2时
最长的含2链为
#include <cstdio>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
#define LL long long
#define INF 1e15
const int MAXN = 1000000;
int n,a,b;
LL p = INF , q = 1 , Mag[ MAXN + 5 ];
vector< int > Graph[ MAXN + 5 ];
void update( LL a , LL b ) {
if( a * q < b * p ) p = a , q = b; //更新时为了避免除法,移项变为乘法
return;
}
LL f1[ MAXN + 5 ] , f2[ MAXN + 5 ];
void dfs( int u , int fa ) {
int f1_1 = 0 , f1_2 = 0 , f2_1 = 0;
if( Mag[ u ] == 1 ) f1[ u ] = 1;
if( Mag[ u ] == 2 ) f2[ u ] = 1;
for( int i = 0 ; i < Graph[ u ].size( ) ; i ++ ) {
int v = Graph[ u ][ i ];
if( v == fa ) continue;
dfs( v , u );
if( Mag[ u ] == 1 ) {
f1[ u ] = max( f1[ u ] , f1[ v ] + 1 );
f2[ u ] = max( f2[ u ] , f2[ v ] + 1 );
}
else if( Mag[ u ] == 2 )
f2[ u ] = max( f2[ u ] , f1[ v ] + 1 );
if( f1[ f1_1 ] < f1[ v ] ) {
f1_2 = f1_1;
f1_1 = v;
}
else if( f1[ f1_2 ] < f1[ v ] )
f1_2 = v;
if( f2[ f2_1 ] < f2[ v ] )
f2_1 = v;
}
if( Mag[ u ] == 1 ) {
if( f1_1 == f2_1 )
update( 2 , f1[ f1_2 ] + f2[ f2_1 ] + 1 );
else
update( 2 , f1[ f1_1 ] + f2[ f2_1 ] + 1 );
update( 1 , f1[ f1_1 ] + f1[ f1_2 ] + 1 );
}
if( Mag[ u ] == 2 )
update( 2 , f1[ f1_1 ] + f1[ f1_2 ] + 1 );
}
int main( ) {
scanf("%d",&n);
for( int i = 1 ; i <= n - 1 ; i ++ ) {
scanf("%d %d",&a,&b);
Graph[ a ].push_back( b );
Graph[ b ].push_back( a );
}
for( int i = 1 ; i <= n ; i ++ ) {
scanf("%lld",&Mag[ i ]);
p = min( Mag[ i ] , p );
}
dfs( 1 , 0 );
LL r = __gcd( p , q );
printf("%lld/%lld\n", p / r , q / r );
return 0;
}
P6288 魔兽争霸(Kralj.cpp)
1.40分思路
兽王给每个勇士选择的对手都是 1 号兽人,那么,GM所指定骑士的顺序就一定是兽人的编号,即第i个骑士对第i个兽人。现在我们只需找一种骑士的排法,使得胜场数最大。
贪心的想,最强的骑士对比他弱的兽人中最强的,第二强的骑士对比他弱的兽人中最强的······直到某一位骑士无法打败任何一人。可以排序后用单调性优化。
#include <cstdio>
#include <algorithm>
using namespace std;
const int MAXN = 500000;
int n,a[ MAXN + 5 ],p[ MAXN + 5 ],v[ MAXN + 5 ];
bool cmp( const int &x , const int &y ) {
return x > y;
}
int main( ) {
//freopen("Kralj.in","r",stdin);
//freopen("Kralj.out","w",stdout);
scanf("%d",&n);
for( int i = 1 ; i <= n ; i ++ )
scanf("%d",&a[ i ]);
for( int i = 1 ; i <= n ; i ++ )
scanf("%d",&p[ i ]);
for( int i = 1 ; i <= n ; i ++ )
scanf("%d",&v[ i ]);
sort( p + 1 , p + n + 1 , cmp );
sort( v + 1 , v + n + 1 , cmp );
int Ans = 0 , last = 1;
for( int i = 1 ; i <= n ; i ++ ) {
for( int j = last ; j <= n ; j ++ ) {
if( v[ i ] > p[ j ] ) {
Ans ++;
last = j + 1;
break;
}
if( j == n && v[ i ] < p[ j ] ) last = n + 1;
}
if( last == n + 1 ) break;
}
printf("%d",Ans);
return 0;
}
2.100分思路
假设骑士走到
当一个区间
移项得:
令
因为
我们从
注意跑环时,
#include <cstdio>
#include <vector>
#include <set>
using namespace std;
const int MAXN = 500000;
int n,a[ MAXN + 5 ],p[ MAXN + 5 ],v[ MAXN + 5 ];
int pre[ MAXN + 5 ],s[ MAXN + 5 ];
int Min = 0x3f3f3f3f , Min_dex;
vector< int > vec[ MAXN + 5 ];
set< int > Set;
int solve( int x ) {
int Ans = 0 , tot = 1;
while( tot <= n ) {
for( int i = 0 ; i < vec[ x ].size( ) ; i ++ )
Set.insert( vec[ x ][ i ] );
auto it = Set.lower_bound( p[ x ] );
if( it == Set.end() )
Set.erase( Set.begin() );
else {
Set.erase( it );
Ans ++;
}
tot ++;
x = ( x + 1 ) > n ? 1 : x + 1;
}
return Ans;
}
int main( ) {
//freopen("Kralj.in","r",stdin);
//freopen("Kralj.out","w",stdout);
scanf("%d",&n);
for( int i = 1 ; i <= n ; i ++ )
scanf("%d",&a[ i ]);
for( int i = 1 ; i <= n ; i ++ )
scanf("%d",&p[ i ]);
for( int i = 1 ; i <= n ; i ++ )
scanf("%d",&v[ i ]);
for( int i = 1 ; i <= n ; i ++ ) {
pre[ a[ i ] ] ++;
vec[ a[ i ] ].push_back( v[ i ] );
}
for( int i = 1 ; i <= n ; i ++ ) {
pre[ i ] += pre[ i - 1 ];
s[ i ] = pre[ i ] - i;
if( s[ i ] < Min ) {
Min = s[ i ];
Min_dex = i;
}
}
int cut = ( Min_dex + 1 ) > n ? 1 : ( Min_dex + 1 );
printf("%d",solve( cut ));
return 0;
}