本文主要是介绍【codeforces】Codeforces Round #274 (Div. 2),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
A. Expression
考虑六种运算,取最大值即可,注意题目特别说明数的位置不能交换。
#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[n].h ) break ;ans[++ cnt] = ANS ( a[n].idx , a[1].idx ) ;a[n].h -- ;a[1].h ++ ;}sort ( a + 1 , a + n + 1 ) ;printf ( "%d %d\n" , a[n].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】Codeforces Round #274 (Div. 2)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!