codeforces ECR169
codeforces ECR 169
A
#include<bits/stdc++.h>
using namespace std;
const int maxn=50;
int a[maxn];
void solve()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)cin>>a[i];
if(n==2)
{
if((a[2]-a[1])!=1)
{
cout<<"YES\n";
}
else
{
cout<<"NO\n";
}
}
else
{
cout<<"NO"<<'\n';
}
}
int main()
{
std::ios::sync_with_stdio(0);
std::cin.tie(0),cout.tie(0);
int t;
cin>>t;
while(t--)solve();
return 0;
}
B
#include<bits/stdc++.h>
using namespace std;
const int maxn=50;
int a[maxn];
#define fi first
#define se second
void solve()
{
pair<int ,int>a[2];
cin>>a[0].fi>>a[0].se;
cin>>a[1].fi>>a[1].se;
if(a[0].se<a[1].fi||a[1].se<a[0].fi)
{
cout<<1<<'\n';
}
else
{
int ans=min(a[0].se,a[1].se)-max(a[0].fi,a[1].fi);
if(a[0].se!=a[1].se)ans++;
if(a[0].fi!=a[1].fi)ans++;
cout<<ans<<'\n';
}
}
int main()
{
std::ios::sync_with_stdio(0);
std::cin.tie(0),cout.tie(0);
int t;
cin>>t;
while(t--)solve();
return 0;
}
C
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxn=2e5+9;
int a[maxn];
void solve()
{
int n,k;
cin>>n>>k;
for(int i=1;i<=n;i++)cin>>a[i];
sort(a+1,a+1+n);
for(int i=n-1;i>=1;i-=2)
{
if(a[i+1]-a[i]<=k)
{
k-=a[i+1]-a[i];
a[i]+=a[i+1]-a[i];
}
else
{
a[i]+=k;
k=0;
break;
}
}
int cnt=1;
int sum=0;
for(int i=n;i>=1;i--)
{
if(cnt%2==1)
{
sum+=a[i];
}
else
{
sum-=a[i];
}
cnt++;
}
cout<<sum<<'\n';
}
signed main()
{
std::ios::sync_with_stdio(0);
std::cin.tie(0),cout.tie(0);
int t;
cin>>t;
while(t--)solve();
return 0;
}
D
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 9;
string t[maxn];
string mol[6] = {"BG", "BR", "BY", "GR", "GY", "RY"};
void solve()
{
int n, q;
cin >> n >> q;
map<string, vector<int>> b;
// BG、BR、BY、GR、GY 或 RY
b["BG"] = vector<int>(), b["BR"] = vector<int>(), b["BY"] = vector<int>(), b["GR"] = vector<int>(), b["GY"] = vector<int>(), b["RY"] = vector<int>();
string s;
for (int i = 1; i <= n; i++)
{
cin >> s;
t[i] = s;
b[s].push_back(i);
}
for (int i = 0; i < 6; i++)
{
sort(b[mol[i]].begin(), b[mol[i]].end());
}
int x, y;
for (int i = 1; i <= q; i++)
{
cin >> x >> y;
if (x == y)
{
cout << 0 << '\n';
continue;
}
if (t[x][0] == t[y][0] || t[x][0] == t[y][1] || t[x][1] == t[y][0] || t[x][1] == t[y][1])
{
int mi = abs(y - x);
cout << mi << "\n";
}
else
{
if (x > y)
swap(x, y);
int mi = 0x7ffffff;
for (int _ = 0; _ < 6; _++)
{
if ((mol[_].find(t[x][0]) != -1 || mol[_].find(t[x][1]) != -1) && mol[_] != t[x])
{
if (b[mol[_]].size() == 0)
continue;
if (b[mol[_]][0] > y)
{
mi = min(mi, abs(2 * b[mol[_]][0] - x - y));
}
else if (b[mol[_]][b[mol[_]].size() - 1] < x)
{
mi = min(mi, abs(x + y - 2 * b[mol[_]][b[mol[_]].size() - 1]));
}
else
{
mi = min(mi, y - x);
}
}
}
cout << ((mi == 0x7ffffff) ? -1 : mi) << '\n';
}
}
}
int main()
{
std::ios::sync_with_stdio(0);
std::cin.tie(0), cout.tie(0);
int t;
cin >> t;
while (t--)
solve();
return 0;
}
/*
1
1 1
BR
1 1
*/
任何时候都有比放弃更好的选择。