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