B题:
题意:博弈,二维平面上n个点,每次可以下移,左移或对角线移动任意步,将任意点移到原点即胜利
思路:SG函数,初始有在对角线上的点则先手必胜。有三个禁区x轴,y轴和对角线,一旦移动到这些地方对手就赢了。所以相应得(1,2)和(2,1)就成为必败点了,这两个点无论如何移动都会进入到禁区。任意点最终都会移到(1,2)和(2,1),所以以(1,2)和(2,1)为起点并躲避禁区SG博弈就出结果了
代码:
#include
using namespace std;
const int maxn = 110;
int sg[maxn][maxn],vis[510];int main()
{sg[1][2] &#61; 0;sg[2][1] &#61; 0;for ( int i&#61;1 ; i<&#61;100 ; i&#43;&#43; )for ( int j&#61;1 ; j<&#61;100 ; j&#43;&#43; ){if ( i&#61;&#61;j ) continue;if ( i&#61;&#61;1&&j&#61;&#61;2 ) continue;if ( i&#61;&#61;2&&j&#61;&#61;1 ) continue;for ( int k&#61;0 ; k<510 ; k&#43;&#43; ) vis[k] &#61; 0;for ( int k&#61;1 ; k}
C题&#xff1a;
题意&#xff1a;切割一个X*Y的矩形&#xff0c;保证割线满足
1.两条割线最多只有一个交点
2.三条割线不会交于一点
3.两条割线的交点不会交在边界线上
4.任意割线不会割在角落上
最终矩形最终被分割成多少块
思路&#xff1a;由题意不难推出此题求的就是割线交点数&#43;割线数&#xff0c;在不考虑垂直线互相相交和平行线互相相交的情况可得答案为&#xff08;V&#43;1&#xff09;*&#xff08;H&#43;1&#xff09;。计算垂直线和平行线互相相交用树状数组统计即可
代码&#xff1a;
#include
using namespace std;
typedef long long LL;
const int maxn &#61; 100010;
int a[maxn],bit[maxn],m;
struct node
{int x,y;friend bool operator<( const node&a , const node&b ){return a.x>b.x;}
}Data[maxn];
int lowbit( int x )
{return x&(-x);
}
void add( int x , int val )
{for ( ; x<&#61;m ; x&#43;&#61;lowbit(x) )bit[x] &#43;&#61; val;
}
int sum( int x )
{int res &#61; 0;for ( ; x>&#61;1 ; x-&#61;lowbit(x) )res &#43;&#61; bit[x];return res;
}
LL slove( int n )
{for ( int i&#61;1 ; i<&#61;n ; i&#43;&#43; )scanf( "%d%d" , &Data[i].x , &Data[i].y ),a[i] &#61; Data[i].y;sort ( a&#43;1 , a&#43;n&#43;1 ); m &#61; unique( a&#43;1 , a&#43;n&#43;1 )-a-1; sort ( Data&#43;1 , Data&#43;n&#43;1 );LL res &#61; 0; for ( int i&#61;1 ; i<&#61;m ; i&#43;&#43; ) bit[i] &#61; 0;for ( int i&#61;1 ; i<&#61;n ; i&#43;&#43; ){int t &#61; lower_bound( a&#43;1 , a&#43;m&#43;1 , Data[i].y )-a;res &#43;&#61; sum( t ); add( t , 1 );}return res;
}
int main()
{for( int X,Y ; scanf( "%d%d" , &X , &Y )&#61;&#61;2 ; ){int H,V; scanf( "%d%d" , &H , &V ); LL ans &#61; 1LL*(H&#43;1)*(V&#43;1);ans &#43;&#61; slove( H );ans &#43;&#61; slove( V );printf( "%lld\n" , ans );}return 0;
}
D题&#xff1a;
题意&#xff1a;统计非1的个数
思路&#xff1a;水题
代码&#xff1a;
#include
using namespace std;int main()
{for ( int n ; scanf( "%d" , &n )&#61;&#61;1 ; ){int ans &#61; 0;for ( int i&#61;1 ; i<&#61;n ; i&#43;&#43; ){int x; scanf( "%d" , &x );if ( x!&#61;1 ) ans&#43;&#43;;}printf( "%d\n" , ans );}return 0;
}
E题&#xff1a;
题意&#xff1a;给两字符串S&#xff0c;T&#xff0c;求S里有多少长度为LEN(T)的子串和没有一个字符相同
思路&#xff1a;此题复杂度为LEN(T)*(LEN(S)-LEN(T)&#43;1)且LEN(S)<&#61;10000&#xff0c;最大复杂度为O(5000*5001)
代码&#xff1a;
#include
using namespace std;
char s[10010],t[10010];
int main()
{scanf( "%s" , s );scanf( "%s" , t );int ans &#61; 0;int lens &#61; strlen(s);int lent &#61; strlen(t);for ( int i&#61;0 ; i<&#61;lens-lent ; i&#43;&#43; ){bool ok &#61; true;for ( int j&#61;0 ; j}
F题&#xff1a;
题意&#xff1a;有n个场地&#xff0c;每个场地上有一些表演&#xff0c;要求每个场地都至少看一次表演&#xff0c;求看表演是可以获得最大价值
思路&#xff1a;n<&#61;10&#xff0c;表演总数<&#61;1000&#xff0c;状压DP&#xff1a;i表示时间节点&#xff0c;j表示状态&#xff08;表示那些场地去没去过&#xff09;
代码&#xff1a;
#include
using namespace std;
int Max( int a , int b ){ return a>b?a:b; }
int n,N,m;
struct node
{int l,r,val,idx;friend bool operator<( const node&a , const node&b ){return a.r}a[1010];
int b[2010],dp[2010][1050];int main()
{scanf( "%d" , &n );m &#61; 0;N &#61; 0;for ( int i&#61;0 ; i}
G题&#xff1a;
题意&#xff1a;炼油厂往加油站运送汽油&#xff0c;有很多道路可以选择不同道路花费的时间不同&#xff0c;求为所有加油站加满所需的最短时间
思路&#xff1a;二分时间&#xff0c;压时间建图&#xff0c;最大流看能否满流
代码&#xff1a;
#include
using namespace std;
const int maxn &#61; 3010;
const int maxm &#61; 50010;
const int inf &#61; 0x3f3f3f3f;
int p,r,c;
int D[maxn];
int E[maxn];
int I[maxm];
int J[maxm];
int T[maxm];
int tol,head[maxn];
struct edge
{int to,next,cap,flow;
}es[maxm];
void addedge( int u , int v , int w )
{es[tol].to &#61; v;es[tol].cap &#61; w;es[tol].flow &#61; 0;es[tol].next &#61; head[u];head[u] &#61; tol&#43;&#43;;es[tol].to &#61; u;es[tol].cap &#61; 0;es[tol].flow &#61; 0;es[tol].next &#61; head[v];head[v] &#61; tol&#43;&#43;;
}
int gap[maxn],dep[maxn],cur[maxn],Q[maxn],S[maxn];
void bfs( int s , int t )
{memset ( dep , -1 , sizeof(dep) );memset ( gap , 0 , sizeof(gap) );gap[0] &#61; 1;int front &#61; 0,rear &#61; 0;dep[t] &#61; 0;Q[rear&#43;&#43;] &#61; t;while ( front!&#61;rear ){int u &#61; Q[front&#43;&#43;];for( int i&#61;head[u] ; i!&#61;-1 ; i&#61;es[i].next ){int v &#61; es[i].to;if ( dep[v]!&#61;-1 ) continue;Q[rear&#43;&#43;] &#61; v;dep[v] &#61; dep[u]&#43;1;gap[dep[v]]&#43;&#43;;}}
}
int sap( int s , int t , int n )
{bfs( s , t );memcpy( cur , head , sizeof(cur) );int top &#61; 0,u &#61; s,ans &#61; 0;while( dep[s]es[S[i]].cap-es[S[i]].flow ){minx &#61; es[S[i]].cap-es[S[i]].flow;inser &#61; i;}for( int i&#61;0 ; i}
bool ok( int limit , int sumx )
{tol &#61; 0; memset( head , -1 , sizeof(head) );int s &#61; 0,t &#61; p&#43;r&#43;1;for ( int i&#61;1 ; i<&#61;p ; i&#43;&#43; )addedge( s , i , D[i] );for ( int i&#61;1 ; i<&#61;r ; i&#43;&#43; )addedge( i&#43;p , t , E[i] );for ( int i&#61;1 ; i<&#61;c ; i&#43;&#43; )if ( T[i]<&#61;limit ) addedge( I[i] , J[i]&#43;p , inf );return sap( s , t , t&#43;1 )&#61;&#61;sumx;
}
int main()
{for ( ; scanf( "%d%d%d" , &p , &r , &c )&#61;&#61;3 ; ){int sumd &#61; 0,sume &#61; 0;for ( int i&#61;1 ; i<&#61;p ; i&#43;&#43; ) scanf( "%d" , &D[i] ),sumd &#43;&#61; D[i];for ( int i&#61;1 ; i<&#61;r ; i&#43;&#43; ) scanf( "%d" , &E[i] ),sume &#43;&#61; E[i];for ( int i&#61;1 ; i<&#61;c ; i&#43;&#43; ) scanf( "%d%d%d" , &I[i] , &J[i] , &T[i] );int l&#61;1,r&#61;1000000;while ( l<&#61;r ){int mid &#61; (l&#43;r)>>1;if ( ok(mid,sumd) ) r &#61; mid-1;else l &#61; mid&#43;1;}if ( l>1000000 ) printf( "-1\n" );else printf( "%d\n" , l );}return 0;
}
I题&#xff1a;
题意&#xff1a;有n个关灯序列和m盏灯&#xff0c;按关灯序列开关灯&#xff0c;问最少个关灯序列可以使得所有点全灭
思路&#xff1a;关灯序列不可能遍历2*n次以上&#xff0c;暴力开关灯
代码&#xff1a;
#include
using namespace std;bool on[1010];
int a[1010][1010];int main()
{int n,m;scanf ( "%d%d" , &n , &m );int ans &#61; -1;for ( int i&#61;1 ; i<&#61;m ; i&#43;&#43; )on[i] &#61; false;int s;scanf( "%d" , &s );for ( int i&#61;1 ; i<&#61;s ; i&#43;&#43; ){int x;scanf( "%d" , &x );on[x] &#61; true;}for ( int i&#61;1 ; i<&#61;n ; i&#43;&#43; ){scanf( "%d" , &a[i][0] );for ( int j&#61;1 ; j<&#61;a[i][0] ; j&#43;&#43; )scanf ( "%d" , &a[i][j] );}for ( int i&#61;1 ; i<&#61;n&#43;n ; i&#43;&#43; ){int t &#61; i;if ( t>n ) t &#61; t-n;for( int j&#61;1 ; j<&#61;a[t][0] ; j&#43;&#43; )on[a[t][j]] &#61; !on[a[t][j]];bool ok &#61; true;for( int j&#61;1 ; j<&#61;m ; j&#43;&#43; )if ( on[j] ) ok &#61; false;if ( ok&&ans&#61;&#61;-1 ) ans &#61; i;}printf( "%d\n" , ans );return 0;
}
L题&#xff1a;
题意&#xff1a;询问树上两条路径的公共节点数
思路&#xff1a;若两条路径有公共节点则其中一条路径的最近公共祖先一定在另一条路径上。两条路径的交汇点一定是从四个点的公共祖先之间。
代码&#xff1a;
#include
using namespace std;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
typedef pair pii;
const int maxn &#61; 100010;
const int maxm &#61; 200010;
int tol,head[maxn];
struct edge
{int to,next;
}es[maxm];
void addedge( int u , int v )
{es[tol].to &#61; v;es[tol].next &#61; head[u];head[u] &#61; tol&#43;&#43;;
}
int fa[maxn][20],dep[maxn];
void dfs( int u , int f , int d )
{for ( int i&#61;head[u] ; i!&#61;-1 ; i&#61;es[i].next ){int v &#61; es[i].to; if ( v&#61;&#61;f ) continue;fa[v][0] &#61; u; dep[v] &#61; d&#43;1; dfs( v , u , d&#43;1 );}
}
int lca( int u , int v )
{if ( dep[u]&#61;0 ; i-- )if ( d&(1<&#61;0 ; i-- )if ( fa[u][i]!&#61;fa[v][i] ){u &#61; fa[u][i];v &#61; fa[v][i];}return fa[u][0];
}
int main()
{tol &#61; 0; memset( head , -1 , sizeof(head) );int n; scanf( "%d" , &n );int q; scanf( "%d" , &q );for ( int i&#61;2 ; i<&#61;n ; i&#43;&#43; ){int u,v; scanf( "%d%d" , &u , &v );addedge( u , v );addedge( v , u );}fa[1][0] &#61; 0; dep[1] &#61; 1; dfs( 1 , 0 , 1 );for ( int j&#61;1 ; j<20 ; j&#43;&#43; )for ( int i&#61;1 ; i<&#61;n ; i&#43;&#43; )fa[i][j] &#61; fa[fa[i][j-1]][j-1];for ( int i&#61;0 ; i&#61;dep[f1] ){ok &#61; true; ans -&#61; dep[f1]-dep[ff1];}if ( dep[ff2]>&#61;dep[f1] ){ok &#61; true; ans -&#61; dep[f1]-dep[ff2];}if ( dep[ff3]>&#61;dep[f1] ){ok &#61; true; ans -&#61; dep[f1]-dep[ff3];}if ( dep[ff4]>&#61;dep[f1] ){ok &#61; true; ans -&#61; dep[f1]-dep[ff4];}if ( ok ) printf( "%d\n" , ans&#43;1 );else printf( "%d\n" , ans );}return 0;
}