牛客小白月赛 54
A sum
把所有的数放进一个大根堆,然后每次取出最大的两个相加,累加到答案中去,并重新放回到大根堆。
最大两数之和重新放入大根堆依旧是最大的数,所以可以优化成前缀和来做。
#include<bits/stdc++.h>
#define int long long
using namespace std;
int read() {
int x = 0, f = 1, ch = getchar();
while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
if (ch == '-') f = -1, ch = getchar();
while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
return x * f;
}
const int N = 2e5+12 , mod = 1e7 + 7;
int a[N];
void solve(){
int n = read() , res = 0;
for( int i = 1 ; i <= n ; i ++ ) a[i] = read();
sort( a + 1 , a + 1 + n , greater<int>() );
int sum = a[1];
for( int i = 2 ; i <= n ; i ++ ) {
sum += a[i] ;
if( sum < 0 ) break;
res += sum;
res %= mod;
}
cout << res << "\n";
return;
}
int32_t main() {
for( int T = read() ; T ; T -- )
solve();
return 0;
}
B Gaming
因为要下选择和一个 debuff 不选,所以要找到哪一个debuff 的积分最少,然后把这个 debuf 的积分删掉
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e6+5;
int n , m , a[N] , res = LONG_MAX , sum;
int read() {
int x = 0, f = 1, ch = getchar();
while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
if (ch == '-') f = -1, ch = getchar();
while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
return x * f;
}
int32_t main() {
n = read() , m = read();
for( int l , r , w ; n ; n -- )
l = read() , r = read() , w = read() , a[l] += w , a[r+1] -= w , sum += w;
for( int i = 1 ; i <= m ; i ++ ) a[i] += a[i-1] , res = min( res , a[i] );
cout << sum - res << "\n";
return 0;
}
C School
首先时间可以全部转化成距离
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n , h , m , q;
int read() {
int x = 0, f = 1, ch = getchar();
while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
if (ch == '-') f = -1, ch = getchar();
while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
return x * f;
}
int get( int x , int y ){
return x * m + y;
}
vector<pair<int,int>> p , t;
pair<int,int> Find( int x ){
int l = 0 , r = t.size() - 1 , mid , res;
while( l <= r ) {
mid = (l + r) >> 1;
if (t[mid].first <= x) res = mid, l = mid + 1;
else r = mid - 1;
}
return t[res];
}
int32_t main() {
n = read() , h = read() , m = read() , q = read();
for( int a , b , c , d ; n ; n -- ){
a = read() , b = read() , c = read() , d = read();
p.push_back( { get(a,b) , get(c,d) } );
}
sort( p.begin() , p.end() , []( pair<int,int> a , pair<int,int> b ){ return a.first < b.first;} );
t.push_back( {0,0} );
for( auto [ l , r ] : p ){
if( l <= t.back().second ) t.back().second = max( t.back().second , r );
else t.push_back( { l , r } );
}
for( int x , a , b ; q ; q -- ){
a = read() , b = read() , x = get( a , b );
auto k = Find(x);
if( k.second >= x ) cout << "No\n";
else cout << "Yes\n";
}
return 0;
}
D Word
#include<bits/stdc++.h>
using namespace std;
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n , m; cin >> n >> m;
vector< string > g(n+2);
for( int i = 1 ; i <= n ; i ++ )
cin >> g[i];
cin >> g[0] >> g[n+1];
vector e( n+2 , vector<int>() );
for( int i = 0 ; i <= n+1 ; i ++ )
for( int j = 0 ; j < i ; j ++ ){
int cnt = 0;
for( int l = 0 ; l < m && cnt < 2 ; l ++ )
if( g[i][l] != g[j][l] ) cnt ++;
if( cnt < 2 ) e[i].push_back(j) , e[j].push_back(i);
}
vector<int> dis( n+2 , INT_MAX );
dis[0] = 0;
vector lst( n + 2 , vector<int>() );
lst[0].push_back(0);
queue< int > q;
q.push(0);
while( q.size() ) {
int u = q.front(); q.pop();
if( u == n + 1 ){
cout << dis[u]-1 << "\n";
for( auto i : lst[u] )
cout << g[i] << "\n";
return 0;
}
for( auto v : e[u] ){
if( dis[v] > dis[u]+1 ) dis[v] = dis[u]+1 , q.push(v) , lst[v] = lst[u] , lst[v].push_back(v);
}
}
cout << "-1";
return 0;
}