每次选两个数,加上小的数的值,求最高分
排序,每次挑小的
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define int long long
typedef pair<int,int>pii;
#define N 310000
int a[N],b[N];
void solve(){
int n;
cin>>n;
for(int i=0;i<n*2;i++) cin>>a[i];
int res=0;
sort(a,a+n*2);
for(int i=0;i<n*2;i+=2){
res+=a[i];
}
cout<<res<<endl;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t=1;
cin>>t;
// init();
while(t--){
solve();
}
}
不让满足的话只需要大小交替即可
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define int long long
typedef pair<int,int>pii;
#define N 310000
int a[N],b[N];
void solve(){
int n;
cin>>n;
int l=1,r=n;
for(int i=1;i<=n;i++){
if(i&1) cout<<l++<<" ";
else cout<<r--<<" ";
}
cout<<endl;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t=1;
cin>>t;
// init();
while(t--){
solve();
}
}
字典树最大,贪心的思考,必须把大的放到前面,由于插入后还会删去,我们一定要把最后面最大值插进去,不然前面要减一,先排序加上序号,排序,每次取min(a[i],a[i-1]-1)
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define int long long
typedef pair<int,int>pii;
#define N 310000
int a[N],b[N];
void solve(){
int n;
cin>>n;
for(int i=0;i<n;i++) cin>>a[i],a[i]+=i+1;
int mx=1e18;
sort(a,a+n,greater<>());
for(int i=0;i<n;i++) {
if(mx>a[i]) {
cout<<a[i]<<" ";
mx=a[i];
}
else cout<<mx-1<<" ",mx--;
}
cout<<endl;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t=1;
cin>>t;
// init();
while(t--){
solve();
}
}
D:Sum over all Substrings (Hard Version)
d1暴力枚举所有子串构造节课
d2要想每一个1怎么构造,发现三个一组,动态规划
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define int long long
typedef pair<int,int>pii;
#define N 1010000
//int a[N],b[N];
int dp[N];
void solve(){
int n;
cin>>n;
string s;
cin>>s;
s=" "+s;
for(int i=1;i<=n;i++) dp[i]=0;
for(int i=1;i<=n;i++){
if(s[i]=='1') {
if(i>=3) dp[i]=dp[i-3]+i;
else dp[i]=i;
}
else dp[i]=dp[i-1];
}
cout<<accumulate(dp+1,dp+n+1,0ll)<<endl;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t=1;
cin>>t;
// init();
while(t--){
solve();
}
}