A
统计一下每个字母的出现次数然后输出即可
cpp
#include <bits/stdc++.h>
#define rep(i,a,b) for(register int i = (a); i <= (b); ++i)
#define fep(i,a,b) for(register int i = (a); i >= (b); --i)
#define ls p<<1
#define rs p<<1|1
#define PII pair<int, int>
#define ll long long
#define ull unsigned long long
#define db double
#define endl '\n'
#define debug(a) cout<<#a<<"="<<a<<endl;
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define INF 0x3f3f3f3f
using namespace std;
const int N = 1e5+10;
int t;
void solve()
{
int n; string str;
cin >> n >> str;
map<char,int>cnt;
rep(i,0,str.size()-1)
{
cnt[str[i]]++;
}
int ans = 0;
for(auto x:cnt)
{
int num=x.first-'A'+1;
if(x.second>=num) ans++;
}
cout << ans << endl;
}
int main()
{
IOS
// freopen("1.in", "r", stdin);
cin >> t;
while(t --)
solve();
return 0;
}
B
前k个数是后n-k个数的顺序,后面的数逆序即可
cpp
#include <bits/stdc++.h>
#define rep(i,a,b) for(register int i = (a); i <= (b); ++i)
#define fep(i,a,b) for(register int i = (a); i >= (b); --i)
#define ls p<<1
#define rs p<<1|1
#define PII pair<int, int>
#define ll long long
#define ull unsigned long long
#define db double
#define endl '\n'
#define debug(a) cout<<#a<<"="<<a<<endl;
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define INF 0x3f3f3f3f
using namespace std;
const int N = 1e5+10;
int t,a[60];
void solve()
{
int n, k, cnt=1; cin >> n >> k;
rep(i,1,n) a[i]=0;
rep(i,n-k,n) a[cnt++] = i;
fep(i,n-k-1,1) a[cnt++] = i;
rep(i,1,n) cout << a[i] << ' ';
cout << endl;
}
int main()
{
IOS
// freopen("1.in", "r", stdin);
cin >> t;
while(t --)
solve();
return 0;
}
C
cpp
#include <bits/stdc++.h>
#define rep(i,a,b) for(register int i = (a); i <= (b); ++i)
#define fep(i,a,b) for(register int i = (a); i >= (b); --i)
#define ls p<<1
#define rs p<<1|1
#define PII pair<int, int>
#define ll long long
#define ull unsigned long long
#define db double
#define endl '\n'
#define debug(a) cout<<#a<<"="<<a<<endl;
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define INF 0x3f3f3f3f
using namespace std;
const int N = 2e5+10;
int t;
int a[N], b[N], s[N];
void solve()
{
int n, k; cin >> n >> k;
rep(i,1,n) s[i]=0;
rep(i,1,n) cin >> a[i], s[i] = s[i-1]+a[i];
rep(i,1,n) cin >> b[i];
int maxx=-INF, ans=-INF;
rep(i,1,min(k,n))
{
maxx=max(maxx, b[i]);
int cur=s[i]; cur+=max(0,k-i)*maxx;
ans=max(cur,ans);
}
cout << ans << endl;
}
int main()
{
IOS
// freopen("1.in", "r", stdin);
cin >> t;
while(t --)
solve();
return 0;
}
D
考虑维护每一个数组前缀和后缀的最大值
然后考虑3种情况即那个数在中间
a、b、c均可以在中间,
a在中间有b a c和c a b两种情况
b在中间有a b c和c b a两种情况
c在中间有a c b和b a c两种情况
这里只说a在中间的情况
我们枚举a的位置 i ∈ [ 2 , n − 1 ] i\in[2,n-1] i∈[2,n−1]
那么对于b a c,b在a左边所以就是b应选择的是 [ 1 , i − 1 ] [1,i-1] [1,i−1]中的最大值,也就是lb[i-1]
c在右边应选择的就是rc[i+1]
这种情况的最大值就是 a [ i ] + l b [ i − 1 ] + r c [ i + 1 ] a[i]+lb[i-1]+rc[i+1] a[i]+lb[i−1]+rc[i+1]
然后对其他几种情况做同样的处理即可
cpp
#include <bits/stdc++.h>
#define rep(i,a,b) for(register int i = (a); i <= (b); ++i)
#define fep(i,a,b) for(register int i = (a); i >= (b); --i)
#define ls p<<1
#define rs p<<1|1
#define PII pair<int, int>
#define ll long long
#define ull unsigned long long
#define db double
#define endl '\n'
#define debug(a) cout<<#a<<"="<<a<<endl;
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define INF 0x3f3f3f3f
using namespace std;
const int N = 2e5+10;
int t, a[N], b[N], c[N];
int la[N], ra[N], lb[N], rb[N], lc[N], rc[N];
void solve()
{
int n; cin >> n;
rep(i,0,n+1) la[i]=0,ra[i]=0,lb[i]=0,rb[i]=0,lc[i]=0,rc[i]=0;
rep(i,1,n) cin >> a[i];
rep(i,1,n) cin >> b[i];
rep(i,1,n) cin >> c[i];
rep(i,1,n) la[i] = max(la[i-1], a[i]);
fep(i,n,1) ra[i] = max(ra[i+1], a[i]);
rep(i,1,n) lb[i] = max(lb[i-1], b[i]);
fep(i,n,1) rb[i] = max(rb[i+1], b[i]);
rep(i,1,n) lc[i] = max(lc[i-1], c[i]);
fep(i,n,1) rc[i] = max(rc[i+1], c[i]);
ll ans=0;
rep(i,2,n-1)
{
ll maxx=0;
maxx = max(maxx, (ll)a[i] + lb[i-1] + rc[i+1]);
maxx = max(maxx, (ll)a[i] + lc[i-1] + rb[i+1]);
maxx = max(maxx, (ll)b[i] + la[i-1] + rc[i+1]);
maxx = max(maxx, (ll)b[i] + lc[i-1] + ra[i+1]);
maxx = max(maxx, (ll)c[i] + la[i-1] + rb[i+1]);
maxx = max(maxx, (ll)c[i] + lb[i-1] + ra[i+1]);
ans = max(ans, maxx);
}
cout << ans << endl;
}
int main()
{
IOS
// freopen("1.in", "r", stdin);
cin >> t;
while(t --)
solve();
return 0;
}
E1、E2
这道题目从数学的角度推公式比较好理解
考虑A、B选择的策略
考虑两列
a:x,y
b:u,v
a选择前面的得到的结果是x-1-(v-1)=x-v
a选择后面的得到的结果是y-1-(u-1)=y-u
哪个更优选择哪个
( x − v ) > ( y − u ) → x + u > y + v 也就是每个人会选择当前 a [ i ] + b [ i ] (x-v)>(y-u) \rightarrow x+u > y+v也就是每个人会选择当前a[i]+b[i] (x−v)>(y−u)→x+u>y+v也就是每个人会选择当前a[i]+b[i]值较大的这一列进行操作
cpp
#include <bits/stdc++.h>
#define rep(i,a,b) for(register int i = (a); i <= (b); ++i)
#define fep(i,a,b) for(register int i = (a); i >= (b); --i)
#define ls p<<1
#define rs p<<1|1
#define PII pair<int, int>
#define ll long long
#define ull unsigned long long
#define db double
#define endl '\n'
#define debug(a) cout<<#a<<"="<<a<<endl;
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define INF 0x3f3f3f3f
using namespace std;
const int N = 2e5+10;
int t;
struct wy
{
ll x, y, sum;
}a[N];
bool cmp(wy a, wy b)
{
return a.sum > b.sum;
}
void solve()
{
int n; cin >> n;
rep(i,1,n) cin >> a[i].x;
rep(i,1,n) cin >> a[i].y, a[i].sum=a[i].x+a[i].y;
sort(a+1,a+1+n, cmp);
ll ans=0;
rep(i,1,n)
{
if(i&1) ans += a[i].x-1;
else ans -= a[i].y-1;
}
cout << ans << endl;
}
int main()
{
IOS
// freopen("1.in", "r", stdin);
cin >> t;
while(t --)
solve();
return 0;
}