【codeforces】Codeforces Round #274 (Div. 2)

2024-09-05 14:38
文章标签 codeforces round div 274

本文主要是介绍【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)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1139240

相关文章

Codeforces Round #240 (Div. 2) E分治算法探究1

Codeforces Round #240 (Div. 2) E  http://codeforces.com/contest/415/problem/E 2^n个数,每次操作将其分成2^q份,对于每一份内部的数进行翻转(逆序),每次操作完后输出操作后新序列的逆序对数。 图一:  划分子问题。 图二: 分而治之,=>  合并 。 图三: 回溯:

Codeforces Round #261 (Div. 2)小记

A  XX注意最后输出满足条件,我也不知道为什么写的这么长。 #define X first#define Y secondvector<pair<int , int> > a ;int can(pair<int , int> c){return -1000 <= c.X && c.X <= 1000&& -1000 <= c.Y && c.Y <= 1000 ;}int m

Codeforces Beta Round #47 C凸包 (最终写法)

题意慢慢看。 typedef long long LL ;int cmp(double x){if(fabs(x) < 1e-8) return 0 ;return x > 0 ? 1 : -1 ;}struct point{double x , y ;point(){}point(double _x , double _y):x(_x) , y(_y){}point op

Codeforces Round #113 (Div. 2) B 判断多边形是否在凸包内

题目点击打开链接 凸多边形A, 多边形B, 判断B是否严格在A内。  注意AB有重点 。  将A,B上的点合在一起求凸包,如果凸包上的点是B的某个点,则B肯定不在A内。 或者说B上的某点在凸包的边上则也说明B不严格在A里面。 这个处理有个巧妙的方法,只需在求凸包的时候, <=  改成< 也就是说凸包一条边上的所有点都重复点都记录在凸包里面了。 另外不能去重点。 int

Codeforces 482B 线段树

求是否存在这样的n个数; m次操作,每次操作就是三个数 l ,r,val          a[l] & a[l+1] &......&a[r] = val 就是区间l---r上的与的值为val 。 也就是意味着区间[L , R] 每个数要执行 | val 操作  最后判断  a[l] & a[l+1] &......&a[r] 是否= val import ja

CSS实现DIV三角形

本文内容收集来自网络 #triangle-up {width: 0;height: 0;border-left: 50px solid transparent;border-right: 50px solid transparent;border-bottom: 100px solid red;} #triangle-down {width: 0;height: 0;bor

创建一个大的DIV,里面的包含两个DIV是可以自由移动

创建一个大的DIV,里面的包含两个DIV是可以自由移动 <body>         <div style="position: relative; background:#DDF8CF;line-height: 50px"> <div style="text-align: center; width: 100%;padding-top: 0px;"><h3>定&nbsp;位&nbsp;

Codeforces Round 971 (Div. 4) (A~G1)

A、B题太简单,不做解释 C 对于 x y 两个方向,每一个方向至少需要 x / k 向上取整的步数,取最大值。 由于 x 方向先移动,假如 x 方向需要的步数多于 y 方向的步数,那么最后 y 方向的那一步就不需要了,答案减 1 代码 #include <iostream>#include <algorithm>#include <vector>#include <string>

CF#271 (Div. 2) D.(dp)

D. Flowers time limit per test 1.5 seconds memory limit per test 256 megabytes input standard input output standard output 题目链接: http://codeforces.com/contest/474/problem/D We s

CF #278 (Div. 2) B.(暴力枚举+推导公式+数学构造)

B. Candy Boxes time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output 题目链接: http://codeforces.com/contest/488/problem/B There