【codeforces】Codeforces Round #274 (Div. 2)
2014-10-20 12:55
295 查看
A. Expression
考虑六种运算,取最大值即可,注意题目特别说明数的位置不能交换。
B. Towers
至多k次,每次将所有数排个序,如果最大的值比最小的大,那么最大的减一,最小的加一,保存是哪两个堆交换了,然后进行下一轮。如果最大和最小的相同则直接退出。
C. Exams
按左边的数left[i]为第一关键字,右边的数right[i]为第二关键字排序。
首先令ans = 0,从左到右遍历所有的数对,假设遍历到了第i个数对,如果ans小于等于right[i],则让ans = right[i],否则ans = left[i](left[i]一定大于等于当前的ans,根据排序我们可知)
D. Long Jumps
首先判断是否在尺子上存在两个刻度的差值等于x,y。由于尺子刻度是单调增的,所以我们可以用O(n)的单调队列来求是否存在。如果x,y都存在,那么就不需要再加一个刻度了,输出0。否则至少要加一个刻度。
如果x或y中的其中一个存在,那么再加一个长度等于另一个的刻度就行了。
如果都不存在,那么我们看是否存在y-x。
如果存在y-x,我们设满足条件的刻度对中最左端的刻度对上刻度小的点的大小为a,如果a+y<=l,那么我们只要添加一个刻度为a+y的点即可。如果a+y>l,这时我们设满足条件的刻度对中最右端的刻度对上刻度小的点的大小为a,如果a-x>=0,则我们只要添加一个刻度为a-x的点即可。
如果上面的条件都不满足,我们再看x+y是否存在,如果存在,那么我们设满足条件的其中一个刻度对的左端为c,那么我们只要添加刻度为c+x的点即可。
如果上面的条件都不满足,好了,只能添加两个点了,一个刻度为x,一个刻度为y。
E. Riding in a Lift
很直观的DP,令d = abs ( j - b ) - 1,dp[i][j]可以推到dp[i + 1][max ( 1 , j - d )] ~ dp[i + 1][min ( n , j + d )],相当于每一层往下推都是一个区间加的过程。直接上线段树,n*n*logn会被卡,于是我们换一个方法,将线段树换成标记,在max ( 1 , j - d )的位置+= dp[i][j],min ( n , j + d )的位置-= dp[i][j],因为不能走到自己,所以flag[j]
+= dp[i][j],flag[j + 1] -= dp[i][j]。
最后一层处理完就扫一下flag数组得到该层的dp值。
因为dp每一层只和上一层有关,于是我们可以用滚动数组优化空间。
考虑六种运算,取最大值即可,注意题目特别说明数的位置不能交换。
#include <cstdio> #include <cstring> #include <algorithm> using namespace std ; const int INF = 0x3f3f3f3f ; int a , b , c ; void solve () { int maxv = -INF ; maxv = max ( maxv , a + b + c ) ; maxv = max ( maxv , a + b * c ) ; maxv = max ( maxv , a * b + c ) ; maxv = max ( maxv , a * ( b + c ) ) ; maxv = max ( maxv , ( a + b ) * c ) ; maxv = max ( maxv , a * b * c ) ; printf ( "%d\n" , maxv ) ; } int main () { while ( ~scanf ( "%d%d%d" , &a , &b , &c ) ) solve () ; return 0 ; }
B. Towers
至多k次,每次将所有数排个序,如果最大的值比最小的大,那么最大的减一,最小的加一,保存是哪两个堆交换了,然后进行下一轮。如果最大和最小的相同则直接退出。
#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 ) const int INF = 0x3f3f3f3f ; const int MAXN = 105 ; const int MAXM = 10005 ; int n , k ; struct Node { int h ; int idx ; bool operator < ( const Node &a ) const { return h < a.h ; } } a[MAXN] ; struct ANS { int x , y ; ANS () {} ANS ( int x , int y ) : x ( x ) , y ( y ) {} } ans[MAXM] ; void solve () { int cnt = 0 ; For ( i , 1 , n ) { scanf ( "%d" , &a[i].h ) ; a[i].idx = i ; } For ( i , 1 , k ) { sort ( a + 1 , a + n + 1 ) ; if ( a[1].h == a .h ) break ; ans[++ cnt] = ANS ( a .idx , a[1].idx ) ; a .h -- ; a[1].h ++ ; } sort ( a + 1 , a + n + 1 ) ; printf ( "%d %d\n" , a .h - a[1].h , cnt ) ; For ( i , 1 , cnt ) printf ( "%d %d\n" , ans[i].x , ans[i].y ) ; } int main () { while ( ~scanf ( "%d%d" , &n , &k ) ) solve () ; return 0 ; }
C. Exams
按左边的数left[i]为第一关键字,右边的数right[i]为第二关键字排序。
首先令ans = 0,从左到右遍历所有的数对,假设遍历到了第i个数对,如果ans小于等于right[i],则让ans = right[i],否则ans = left[i](left[i]一定大于等于当前的ans,根据排序我们可知)
#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 ) const int INF = 0x3f3f3f3f ; const int MAXN = 5005 ; const int MAXM = 10005 ; int n ; struct Node { int x , y ; Node () {} Node ( int x , int y ) : x ( x ) , y ( y ) {} bool operator < ( const Node &a ) const { if ( x != a.x ) return x < a.x ; return y < a.y ; } } a[MAXN] ; void solve () { int minv = 0 ; For ( i , 1 , n ) { scanf ( "%d%d" , &a[i].x , &a[i].y ) ; } sort ( a + 1 , a + n + 1 ) ; For ( i , 1 , n ) { if ( minv <= a[i].y ) minv = a[i].y ; else minv = a[i].x ; } printf ( "%d\n" , minv ) ; } int main () { while ( ~scanf ( "%d" , &n ) ) solve () ; return 0 ; }
D. Long Jumps
首先判断是否在尺子上存在两个刻度的差值等于x,y。由于尺子刻度是单调增的,所以我们可以用O(n)的单调队列来求是否存在。如果x,y都存在,那么就不需要再加一个刻度了,输出0。否则至少要加一个刻度。
如果x或y中的其中一个存在,那么再加一个长度等于另一个的刻度就行了。
如果都不存在,那么我们看是否存在y-x。
如果存在y-x,我们设满足条件的刻度对中最左端的刻度对上刻度小的点的大小为a,如果a+y<=l,那么我们只要添加一个刻度为a+y的点即可。如果a+y>l,这时我们设满足条件的刻度对中最右端的刻度对上刻度小的点的大小为a,如果a-x>=0,则我们只要添加一个刻度为a-x的点即可。
如果上面的条件都不满足,我们再看x+y是否存在,如果存在,那么我们设满足条件的其中一个刻度对的左端为c,那么我们只要添加刻度为c+x的点即可。
如果上面的条件都不满足,好了,只能添加两个点了,一个刻度为x,一个刻度为y。
#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 ) const int INF = 0x3f3f3f3f ; const int MAXN = 100005 ; const int MAXM = 10005 ; int n , l , x , y ; int a[MAXN] ; int search ( int tmp , int flag ) { int i = 1 , j = 1 ; int ans = 0 ; while ( j <= n ) { while ( j <= n && a[j] - a[i] > tmp ) ++ i ; if ( a[j] - a[i] == tmp ) { if ( !ans ) ans = i ; if ( flag ) ans = i ; ++ j ; } while ( j <= n && a[j] - a[i] < tmp ) ++ j ; } return ans ; } void solve () { For ( i , 1 , n ) scanf ( "%d" , &a[i] ) ; int mark1 = search ( x , 0 ) ; int mark2 = search ( y , 0 ) ; if ( mark1 && mark2 ) printf ( "0\n" ) ; else if ( mark1 ) printf ( "1\n%d\n" , y ) ; else if ( mark2 ) printf ( "1\n%d\n" , x ) ; else { int tmp1 = search ( y - x , 0 ) ; int tmp2 = search ( y - x , 1 ) ; int tmp3 = search ( y + x , 0 ) ; if ( tmp1 ) { if ( a[tmp1] + y <= l ) printf ( "1\n%d\n" , a[tmp1] + y ) ; else if ( a[tmp2] - x >= 0 ) printf ( "1\n%d\n" , a[tmp2] - x ) ; else printf ( "2\n%d %d\n" , x , y ) ; } else if ( tmp3 ) printf ( "1\n%d\n" , a[tmp3] + x ) ; else printf ( "2\n%d %d\n" , x , y ) ; } } int main () { while ( ~scanf ( "%d%d%d%d" , &n , &l , &x , &y ) ) solve () ; return 0 ; }
E. Riding in a Lift
很直观的DP,令d = abs ( j - b ) - 1,dp[i][j]可以推到dp[i + 1][max ( 1 , j - d )] ~ dp[i + 1][min ( n , j + d )],相当于每一层往下推都是一个区间加的过程。直接上线段树,n*n*logn会被卡,于是我们换一个方法,将线段树换成标记,在max ( 1 , j - d )的位置+= dp[i][j],min ( n , j + d )的位置-= dp[i][j],因为不能走到自己,所以flag[j]
+= dp[i][j],flag[j + 1] -= dp[i][j]。
最后一层处理完就扫一下flag数组得到该层的dp值。
因为dp每一层只和上一层有关,于是我们可以用滚动数组优化空间。
#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 ) const int INF = 0x3f3f3f3f ; const int MAXN = 5005 ; const int mod = 1e9 + 7 ; int dp[2][MAXN] ; int flag[MAXN] ; int n , a , b , k ; inline void fix ( int& x ) { while ( x >= mod ) x -= mod ; while ( x < 0 ) x += mod ; } void solve () { int cur = 0 ; clr ( dp , 0 ) ; dp[cur][a] = 1 ; For ( i , 1 , k ) { cur ^= 1 ; clr ( flag , 0 ) ; For ( j , 1 , n ) { int d = abs ( j - b ) - 1 ; int L = max ( 1 , j - d ) ; int R = min ( n , j + d ) ; flag[L] += dp[cur ^ 1][j] ; flag[j] -= dp[cur ^ 1][j] ; flag[j + 1] += dp[cur ^ 1][j] ; flag[R + 1] -= dp[cur ^ 1][j] ; fix ( flag[L] ) ; fix ( flag[j] ) ; fix ( flag[j + 1] ) ; fix ( flag[R + 1] ) ; } For ( j , 1 , n ) { dp[cur][j] = dp[cur][j - 1] + flag[j] ; fix ( dp[cur][j] ) ; } } int ans = 0 ; For ( i , 1 , n ) { ans += dp[cur][i] ; if ( ans >= mod ) ans -= mod ; } printf ( "%d\n" , ans ) ; } int main () { while ( ~scanf ( "%d%d%d%d" , &n , &a , &b , &k ) ) solve () ; return 0 ; }
相关文章推荐
- [贪心] Codeforces 723C #375 (Div. 2) C. Polycarp at the Radio
- 我的隔天Codeforces——Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined)
- codeforces 411 div2-find amir
- codeforces 260 div2 B题
- Table CodeForces - 232B(144 div1 B) (dp+快速幂)
- codeforces 713C(Round #371 Div2 E) Sonya and Problem Wihtout a Legend ★ ★
- 【Codeforces Round #200 (Div. 1)】Codeforces 343D Water Tree
- 【Codeforces Round #376 (Div. 2)】 Codeforces 731A Night at the Museum
- 【codeforces】Codeforces Round #291 (Div. 2) 题解
- Codeforces Round #210 (Div. 2) C. Levko and Array Recovery && Codeforces 360 A 详解(思维+维护理论值)
- 【codeforces】Round #294 (Div. 2) D. A and B and Interesting Substrings
- codeforces div2 round#230 B
- codeforces 891-A. Pride Codeforces Round #446 (Div. 2) C. Pride
- 第十二次codeforces竞技结束 #263 Div 2
- Codeforces 779D 二分求上界 Codeforces Round #402 (Div. 2)
- Codeforces 307 div2 E.GukiZ and GukiZiana 分块
- codeforces 352 div 2 C.Recycling Bottles 贪心
- CodeForces Div1: 995 D. Game(数学期望)
- [Codeforces] Round #235 (Div. 2)
- Codeforces 318 div2.D Bear and Blocks(dp,贪心)