A Programming Contest
auto i = A. rbegin ( ) ;
i++
实现1(meet in middle 双指针)
#include <iostream>
#include <bits/stdc++.h>
#define For(i,x,y) for(int i=(x); i<=(y); i++)
#define fori(i,x,y) for(int i=(x); i<(y); i++)
#define rep(i,y,x) for(int i=(y); i>=(x); i--)
#define mst(x,a) memset(x,a,sizeof(x))
#define pb push_back
#define sz(a) (int)a.size()
#define ALL(x) x.begin(),x.end()
#define mp make_pair
#define fi first
#define se second
#define db double
#define endl '\n'
#define debug(a) cout << #a << ": " << a << endl
using namespace std;
typedef long long LL;
typedef long long ll;
typedef unsigned long long ULL;
const LL INF = 0x3f3f3f3f3f3f3f3f ;
const int inf = 0x3f3f3f3f ;
const int mod = 1e9 + 7 ;
typedef pair< int , int > pa;
typedef pair< ll, ll> pai;
typedef pair< db, db> pdd;
const db eps = 1e-8 ;
const db pi = acos ( - 1.0 ) ;
template < typename T1, typename T2> void ckmin ( T1 & a, T2 b) { if ( a > b) a = b; }
template < typename T1, typename T2> void ckmax ( T1 & a, T2 b) { if ( a < b) a = b; }
int read ( ) {
int x = 0 , f = 0 ; char ch = getchar ( ) ;
while ( ! isdigit ( ch) ) f | = ch == '-' , ch = getchar ( ) ;
while ( isdigit ( ch) ) x = 10 * x + ch - '0' , ch = getchar ( ) ;
return f ? - x : x;
}
template < typename T> void print ( T x) {
if ( x < 0 ) putchar ( '-' ) , x = - x;
if ( x >= 10 ) print ( x / 10 ) ;
putchar ( x % 10 + '0' ) ;
}
template < typename T> void print ( T x, char let) {
print ( x) , putchar ( let) ;
}
template < class T > bool uin ( T & a, T b) { return a > b ? ( a = b, true ) : false ; }
template < class T > bool uax ( T & a, T b) { return a < b ? ( a = b, true ) : false ; }
vector< LL> merge ( vector< LL> A, int x) {
vector< LL> res;
auto i = A. begin ( ) ;
for ( auto a : A) {
while ( x + * i < a) res. pb ( x+ * i++ ) ;
res. pb ( a) ;
}
while ( i!= A. end ( ) ) res. pb ( x+ * i++ ) ;
return res;
}
void sol ( ) {
vector< LL> B{ 0 } , C{ 0 } ;
int n, t;
cin>> n>> t;
fori ( i, 0 , n) {
int x; cin>> x;
B = merge ( B, x) ;
swap ( B, C) ;
}
auto i = B. rbegin ( ) ;
int ans = 0 ;
for ( auto a: C) {
if ( a > t) break ;
while ( a + * i > t) i ++ ;
ckmax ( ans, a+ * i) ;
}
cout<< ans<< endl;
}
int main ( )
{
ios:: sync_with_stdio ( 0 ) ; cin. tie ( 0 ) ; cout. tie ( 0 ) ;
#ifdef LOCAL
freopen ( "in.txt" , "r" , stdin ) ;
#endif
sol ( ) ;
return 0 ;
}
实现2(meet in middle && 二分)
在这里插入代码片