A:
#include<bits/stdc++.h>
using namespace std;
#define x first
#define y second
#define rep(i,a,n) for (int i = a; i < n; i ++ )
#define repn(i,a,n) for (int i = a; i <= n; i ++ )
#define pb push_back
#define IOS ios::sync_with_stdio(false); cin.tie(0);cout.tie(0);
typedef long long ll;
typedef pair<int,int> PII;
ll gcd(ll a,ll b) { return b ? gcd(b,a % b) : a; }
const int mod = 1e9+7;
int main()
{
IOS;
int t;
cin >> t;
while (t -- )
{
int n;
cin >> n;
int k;
cin >> k;
int res = 0;
int sum = 0;
for (int i = 0; i < n; i ++ )
{
int x;
cin >> x;
sum += x;
}
res = max(res, sum - k);
cout << res << endl;
}
return 0;
}
B:
排序加前缀和
#include<bits/stdc++.h>
using namespace std;
#define x first
#define y second
#define rep(i,a,n) for (int i = a; i < n; i ++ )
#define repn(i,a,n) for (int i = a; i <= n; i ++ )
#define pb push_back
#define IOS ios::sync_with_stdio(false); cin.tie(0);cout.tie(0);
typedef long long ll;
typedef pair<int,int> PII;
ll gcd(ll a,ll b) { return b ? gcd(b,a % b) : a; }
const int mod = 1e9+7;
const int N = 2e5;
ll a[N];
ll b[N];//前缀和
int main()
{
IOS;
int n, k;
cin >> n >> k;
for (int i = 1; i <= n; i ++ ) cin >> a[i];
sort(a + 1, a + n + 1, greater<ll>());
for (int i = 1; i <= n; i ++ ) b[i] = a[i] + b[i - 1];
while(k -- )
{
ll x, y;
cin >> x >> y;
ll res = b[x] - b[x - y];
cout << res << endl;
}
return 0;
}
C:我们可以这样想,因为两次操作都涉及到了B,所以我们可以假设B的位置是相对不变的,相对于答案是确定的。那么只需要去考虑a和c的位置,那么两个操作很明显可以看出来,ab->ba,a的位置肯定是往后移动了,当我们把答案中的a的位置记录下来的时候,原来的a肯定是在这个位置的前面或者正好在这个位置,(如果在前面,则需要进行交换,如果刚好,则不需要,如果在后面,则无法完成操作).所以只需要比较相对位置即可,同理,c也是如此!
#include<bits/stdc++.h>
using namespace std;
#define x first
#define y second
#define rep(i,a,n) for (int i = a; i < n; i ++ )
#define repn(i,a,n) for (int i = a; i <= n; i ++ )
#define pb push_back
#define IOS ios::sync_with_stdio(false); cin.tie(0);cout.tie(0);
typedef long long ll;
typedef pair<int,int> PII;
ll gcd(ll a,ll b) { return b ? gcd(b,a % b) : a; }
const int mod = 1e9+7;
void solve()
{
int n;
cin >> n;
string s, t;
cin >> s >> t;
for (auto c : {'a', 'b', 'c'}){
if(count(s.begin(), s.end(),c) != count(t.begin(), t.end(),c)){
cout << "NO"<< endl;
return;
}
}
string s1, t1;
vector<int> x, y;
for (int i = 0; i < n; i ++ )
{
if (s[i] != 'b'){
s1 += s[i];
x.pb(i);
}
}
for (int i = 0; i < n; i ++ )
{
if (t[i] != 'b'){
t1 += t[i];
y.pb(i);
}
}
if(s1 != t1){
cout << "NO" << endl;
return;
}
for (int i = 0; i < x.size(); i ++ )
{
if(s1[i] == 'a' ? (x[i] > y[i]) : (x[i] < y[i])){
cout << "NO" << endl;
return;
}
}
cout << "YES" << endl;
}
int main()
{
IOS;
int t;
cin >> t;
while (t -- )
{
solve();
}
return 0;
}