【codeforces】Codeforces Round #284 (Div. 1) 【题解】

传送门:【codeforces】Codeforces Round #284 (Div. 1)


498A. Crazy Town

题目等价于给N条直线,如果两个点分别在直线两侧,则++cnt,最后的答案就是cnt的值。方法就是带入点的坐标到直线方程ax+by+c中看得到的两个值的乘积是否小于0,小于0则++cnt。

而我,用了十分蠢的方法!将直线变成线段,将所给两个点连接,这样题目就变成求与所给两点构成的线段相交的线段数了。。。(真是蠢。。)

#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
#include <string>
#include <map>
#include <iostream>
using namespace std ;

typedef long long LL ;

#define rep( i , a , b ) for ( int i = ( a ) ; i <  ( b ) ; ++ i )
#define For( i , a , b ) for ( int i = ( a ) ; i <= ( b ) ; ++ i )
#define rev( i , a , b ) for ( int i = ( a ) ; i >= ( b ) ; -- i )
#define clr( a , x ) memset ( a , x , sizeof a )

const int MAXN = 305 ;
const double eps = 1e-10 ;

struct Point {
	double x , y ;
	Point () {}
	Point ( double x , double y ) : x ( x ) , y ( y ) {}
	Point operator - ( const Point& p ) const {
		return Point ( x - p.x , y - p.y ) ;
	}
} ;

struct Line {
	Point p1 , p2 ;
} ;

Line l , t ;
int n ;

int dcmp ( double x ) {
	return ( x > eps ) - ( x < -eps ) ;
}

double cross ( Point a , Point b ) {
	return a.x * b.y - a.y * b.x ;
}

int into ( Point a , Point b , Point c ) {
	if ( dcmp ( a.x - min ( b.x , c.x ) ) < 0 ) return 0 ;
	if ( dcmp ( a.x - max ( b.x , c.x ) ) > 0 ) return 0 ;
	if ( dcmp ( a.y - min ( b.y , c.y ) ) < 0 ) return 0 ;
	if ( dcmp ( a.y - max ( b.y , c.y ) ) > 0 ) return 0 ;
	return 1 ;
}

int intersection ( Line x , Line y ) {
	double a = cross ( y.p1 - x.p1 , y.p2 - x.p1 ) ;
	double b = cross ( y.p1 - x.p2 , y.p2 - x.p2 ) ;
	double c = cross ( x.p1 - y.p1 , x.p2 - y.p1 ) ;
	double d = cross ( x.p1 - y.p2 , x.p2 - y.p2 ) ;
	//printf ( "%lf %lf %lf %lf\n" , a , b , c , d ) ;
	if ( dcmp ( a * b ) < 0 && dcmp ( c * d ) < 0 ) return 1 ;
	if ( dcmp ( a ) == 0 && into ( x.p1 , y.p1 , y.p2 ) ) return 1 ;
	if ( dcmp ( b ) == 0 && into ( x.p2 , y.p1 , y.p2 ) ) return 1 ;
	if ( dcmp ( c ) == 0 && into ( y.p1 , x.p1 , x.p2 ) ) return 1 ;
	if ( dcmp ( d ) == 0 && into ( y.p2 , x.p1 , x.p2 ) ) return 1 ;
	return 0 ;
}

void solve () {
	int ans = 0 ;
	double a , b , c ;
	scanf ( "%d" , &n ) ;
	rep ( i , 0 , n ) {
		scanf ( "%lf%lf%lf" , &a , &b , &c ) ;
		if ( dcmp ( b ) == 0 ) {
			t.p1 = Point ( - ( c + b * 1000000.0 ) / a ,  1000000.0 ) ;
			t.p2 = Point ( - ( c - b * 1000000.0 ) / a , -1000000.0 ) ;
		} else {
			t.p1 = Point (  1000000.0 , - ( c + a * 1000000.0 ) / b ) ;
			t.p2 = Point ( -1000000.0 , - ( c - a * 1000000.0 ) / b ) ;
		}
		if ( intersection ( l , t ) ) {
			++ ans ;
			//printf ( "%d\n" , i ) ;
		}
	}
	printf ( "%d\n" , ans ) ;
}

int main () {
	while ( ~scanf ( "%lf%lf%lf%lf" , &l.p1.x , &l.p1.y , &l.p2.x , &l.p2.y ) ) solve () ;
	return 0 ;
}


498B. Name That Tune

首先我们可以明确这题用背包可以写,但是妥妥的会超时~

我们需要考虑优化。

我们设dp[i][j]表示枚举到第i首歌,所用时间恰好为j的概率。

假设第i首歌识别的概率是pi,最多所用次数为ti。

现在我们识别第i首歌所用总时间恰好为j,那么其可能是dp[i-1][j-ti],dp[i-1][j-ti+1],......,dp[i-1][j-1]转移过来的。

注意到dp[i][j]=dp[i-1][j-ti]*(1-pi)^(ti-1)+dp[i-1][j-ti+1]*(1-pi)^(ti-2)*p+dp[i-1][j-ti+2]*(1-pi)^(ti-3)*p+......+dp[i-1][j-1]*p,除了dp[i-1][j-ti]这项的系数特别一点外(因为第ti次一定能听出来),其他的所乘的系数就是一个等比数列。于是我们考虑记录前缀和,令tmp=dp[i-1][j-ti+1]*(1-pi)^(ti-2)+dp[i-1][j-ti+2]*(1-pi)^(ti-3)+......+dp[i-1][j-1],这样从j推向j+1时只要tmp=tmp*(1-pi)+dp[i-1][j]即可(注意!如果j-ti-1>=0我们需要减掉不能转移过来的状态dp[i-1][j-ti-1]*(1-pi)^ti)。只要注意特别的dp[i-1][j-ti]项的处理,还有就是考虑滚动数组优化空间就行了。

#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std ;

typedef long long LL ;

#define rep( i , a , b ) for ( int i = ( a ) ; i <  ( b ) ; ++ i )
#define For( i , a , b ) for ( int i = ( a ) ; i <= ( b ) ; ++ i )
#define rev( i , a , b ) for ( int i = ( a ) ; i >= ( b ) ; -- i )
#define clr( a , x ) memset ( a , x , sizeof a )

const int MAXN = 5005 ;

double dp[2][MAXN] ;
int n , t ;

void solve () {
	double ans = 0 , pi ;
	int cur = 0 , ti ;
	clr ( dp[cur] , 0 ) ;
	dp[0][0] = 1 ;
	For ( i , 1 , n ) {
		scanf ( "%lf%d" , &pi , &ti ) ;
		pi /= 100.0 ;
		double q = pow ( 1 - pi , 1.0 * ti ) ;
		cur ^= 1 ;
		double tmp = 0 ;
		rep ( j , 0 , i ) dp[cur][j] = 0 ;
		For ( j , i , t ) {
			tmp += dp[cur ^ 1][j - 1] ;
			if ( j - ti - 1 >= 0 ) tmp -= dp[cur ^ 1][j - ti - 1] * q ;
			dp[cur][j] = tmp * pi ;
			if ( j - ti >= 0 ) dp[cur][j] += dp[cur ^ 1][j - ti] * q ;
			tmp *= ( 1 - pi ) ;
			ans += dp[cur][j] ;
		}
	}
	printf ( "%.6f\n" , ans ) ;
}

int main () {
	while ( ~scanf ( "%d%d" , &n , &t ) ) solve () ;
	return 0 ;
}



498C. Array and Operations

网络流!昨晚cf竟然没看出来!!!!!!首先,这是一个二分图,因为有关系的两个点属于不同的集合。又因为一百个数最多3200个素数,然后暴力对每个素数跑最小割就好。

建图就是先对m对关系建边,设边的容量无穷大,然后源点向第一集合的数建边容量为当前枚举的素数在这个数中的个数,第二集合的数向汇点简便容量为当前枚举的素数在这个数中的个数。然后将对每个素数跑出来的结果相加就是答案。

这么裸的网络流竟然没看出来T  T,昨晚也太傻逼了,有关系的两点下标相加是奇数,我竟然认为1+3这样的是有关系的!(意思就是我竟然认为下标相加是偶数的才是有关系的。。T  T)

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;

typedef long long LL ;

#define rep( i , a , b ) for ( int i = ( a ) ; i <  ( b ) ; ++ i )
#define For( i , a , b ) for ( int i = ( a ) ; i <= ( b ) ; ++ i )
#define rev( i , a , b ) for ( int i = ( a ) ; i >= ( b ) ; -- i )
#define clr( a , x ) memset ( a , x , sizeof a )
#define cpy( a , x ) memcpy ( a , x , sizeof a )

const int MAXN = 105 ;
const int MAXE = 405 ;
const int INF = 0x3f3f3f3f ;

struct Edge {
	int v , rc , c , n ;
	Edge () {}
	Edge ( int v , int c , int n ) : v ( v ) , c ( c ) , rc ( c ) , n ( n ) {}
} ;

Edge E[MAXE] ;
int H[MAXN] , cntE ;
int d[MAXN] , gap[MAXN] , cur[MAXN] , pre[MAXN] ;
int Q[MAXN] , head , tail ;
int s , t , nv ;
int flow ;
int n , m ;
int num[MAXN] ;
int a[5005] , cnt ;

void clear () {
	cntE = 0 ;
	clr ( H , -1 ) ;
}

void addedge ( int u , int v , int c ) {
	E[cntE] = Edge ( v , c , H[u] ) ;
	H[u] = cntE ++ ;
	E[cntE] = Edge ( u , 0 , H[v] ) ;
	H[v] = cntE ++ ;
}

void rev_bfs () {
	clr ( d , -1 ) ;
	clr ( gap , 0 ) ;
	head = tail = 0 ;
	Q[tail ++] = t ;
	d[t] = 0 ;
	gap[0] = 1 ;
	while ( head != tail ) {
		int u = Q[head ++] ;
		for ( int i = H[u] ; ~i ; i = E[i].n ) {
			int v = E[i].v ;
			if ( ~d[v] ) continue ;
			d[v] = d[u] + 1 ;
			gap[d[v]] ++ ;
			Q[tail ++] = v ;
		}
	}
}

int isap () {
	rev_bfs () ;
	cpy ( cur , H ) ;
	flow = 0 ;
	int u = pre[s] = s , i , minv , f , pos ;
	while ( d[s] < nv ) {
		if ( u == t ) {
			f = INF ;
			for ( i = s ; i != t ; i = E[cur[i]].v ) if ( f > E[cur[i]].c ) {
				f = E[cur[i]].c ;
				pos = i ;
			}
			for ( i = s ; i != t ; i = E[cur[i]].v ) {
				E[cur[i]].c -= f ;
				E[cur[i] ^ 1].c += f ;
			}
			flow += f ;
			u = pos ;
		}
		for ( i = cur[u] ; ~i ; i = E[i].n ) if ( E[i].c && d[u] == d[E[i].v] + 1 ) break ;
		if ( ~i ) {
			cur[u] = i ;
			pre[E[i].v] = u ;
			u = E[i].v ;
		} else {
			if ( 0 == -- gap[d[u]] ) break ;
			for ( minv = nv , i = H[u] ; ~i ; i = E[i].n ) if ( E[i].c && minv > d[E[i].v] ) {
				cur[u] = i ;
				minv = d[E[i].v] ;
			}
			d[u] = minv + 1 ;
			gap[d[u]] ++ ;
			u = pre[u] ;
		}
	}
	return flow ;
}

void divide ( int x ) {
	for ( int i = 2 ; i * i <= x ; ++ i ) {
		if ( x % i == 0 ) {
			a[++ cnt] = i ;
			while ( x % i == 0 ) x /= i ;
		}
	}
	if ( x > 1 ) a[++ cnt] = x ;
}

int unique ( int n ) {
	int cnt = 1 ;
	sort ( a + 1 , a + n + 1 ) ;
	For ( i , 2 , n ) if ( a[i] != a[cnt] ) a[++ cnt] = a[i] ;
	return cnt ;
}

int count ( int x , int y ) {
	int res = 0 ;
	while ( x % y == 0 ) {
		x /= y ;
		++ res ;
	}
	return res ;
}

void solve () {
	int ans = 0 ;
	int u , v ;
	clear () ;
	cnt = 0 ;
	s = n , t = n + 1 , nv = t + 1 ;
	rep ( i , 0 , n ) {
		scanf ( "%d" , &num[i] ) ;
		if ( i & 1 ) addedge ( s , i , 0 ) ;
		else addedge ( i , t , 0 ) ;
		divide ( num[i] ) ;
	}
	rep ( i , 0 , m ) {
		scanf ( "%d%d" , &u , &v ) ;
		-- u ;
		-- v ;
		if ( u & 1 ) addedge ( u , v , INF ) ;
		else addedge ( v , u , INF ) ;
	}
	cnt = unique ( cnt ) ;
	For ( i , 1 , cnt ) {
		rep ( j , 0 , cntE ) E[j].c = E[j].rc ;
		rep ( j , 0 , n ) E[j << 1].c = count ( num[j] , a[i] ) ;
		int tmp = isap () ;
		ans += tmp ;
	}
	printf ( "%d\n" , ans ) ;
}

int main () {
	while ( ~scanf ( "%d%d" , &n , &m ) ) solve () ;
	return 0 ;
}


498D. Traffic Jams in the Land

这题呢,我们知道2,3,4,5,6的最小公倍数是60,所以时间是60一个周期,所以我们开60棵线段树,第i棵线段树的区间【L,R】表示我们在第i个时间点(从L出发的时间mod60后)从L出发到达R所需的时间。然后我们就可以在合并区间的时候维护了:sum[i][o] = sum[i][ls] + sum[(i+sum[i][ls])%60][rs]。这个合并方程就是本题的关键了。查询的时候也是用同样的方法合并。

这题想到了就很简单了。YY大法好!

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;

typedef long long LL ;

#define rep( i , a , b ) for ( int i = ( a ) ; i <  ( b ) ; ++ i )
#define For( i , a , b ) for ( int i = ( a ) ; i <= ( b ) ; ++ i )
#define rev( i , a , b ) for ( int i = ( a ) ; i >= ( b ) ; -- i )
#define clr( a , x ) memset ( a , x , sizeof a )
#define cpy( a , x ) memcpy ( a , x , sizeof a )
#define ls ( o << 1 )
#define rs ( o << 1 | 1 )
#define lson ls , l , m
#define rson rs , m + 1 , r
#define root 1 , 1 , n
#define rt o , l , r
#define mid ( ( l + r ) >> 1 )

const int MAXN = 100005 ;

int sum[60][MAXN << 2] ;
int a[MAXN] ;
int n , q ;

void pushup ( int o , int l , int r ) {
	int m = mid ;
	rep ( i , 0 , 60 ) sum[i][o] = sum[i][ls] + sum[( i + sum[i][ls] ) % 60][rs] ;
}

void build ( int o , int l , int r ) {
	if ( l == r ) {
		rep ( i , 0 , 60 ) sum[i][o] = 1 + ( i % a[l] == 0 ) ;
		return ;
	}
	int m = mid ;
	build ( lson ) ;
	build ( rson ) ;
	pushup ( rt ) ;
}

void update ( int x , int v , int o , int l , int r ) {
	if ( l == r ) {
		a[l] = v ;
		rep ( i , 0 , 60 ) sum[i][o] = 1 + ( i % v == 0 ) ;
		return ;
	}
	int m = mid ;
	if ( x <= m ) update ( x , v , lson ) ;
	else update ( x , v , rson ) ;
	pushup ( rt ) ;
}

int query ( int L , int R , int x , int o , int l , int r ) {
	if ( L <= l && r <= R ) return sum[x][o] ;
	int m = mid ;
	if ( R <= m ) return query ( L , R , x , lson ) ;
	if ( m <  L ) return query ( L , R , x , rson ) ;
	int tmp = query ( L , R , x , lson ) ;
	return tmp + query ( L , R , ( x + tmp ) % 60 , rson ) ;
}

void solve () {
	char op[5] ;
	int l , r ;
	For ( i , 1 , n ) scanf ( "%d" , &a[i] ) ;
	build ( root ) ;
	scanf ( "%d" , &q ) ;
	while ( q -- ) {
		scanf ( "%s%d%d" , op , &l , &r ) ;
		if ( op[0] == 'C' ) update ( l , r , root ) ;
		else printf ( "%d\n" , query ( l , r - 1 , 0 , root ) ) ;
	}
}

int main () {
	while ( ~scanf ( "%d" , &n ) ) solve () ;
	return 0 ;
}


评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值