文章目录
A - Add Odd or Subtract Even
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
ll t;
cin>>t;
while(t--){
ll a,b;
cin>>a>>b;
if(a==b)
cout<<0<<endl;
else if(a<b){
if((b-a)&1){
cout<<1<<endl;
}
else
cout<<2<<endl;
}
else{
if((b-a)&1){
cout<<2<<endl;
}
else
cout<<1<<endl;
}
}
}
B - WeirdSort
我的垃圾做法
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[1000],b[1000],pos[1000];
bool vis[1000];
int main(){
ll t;
cin>>t;
while(t--){
ll n,m;
cin>>n>>m;
memset(vis,false,sizeof(vis));
for(int i=1;i<=n;i++){
cin>>a[i];
}
for(ll i=1;i<=m;i++){
cin>>b[i];
vis[b[i]]=true;
}
bool flag=true;
for(int i=1;i<=n;i++){
for(int j=n;j>i;j--){
if(a[j]<a[j-1]){
if(vis[j-1]){
swap(a[j],a[j-1]);
}
else{
flag=false;
break;
}
}
}
if(!flag)break;
}
if(flag)puts("YES");
else puts("NO");
}
}
可以O(n)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,m;
const ll maxn=1e3+10;
ll a[maxn];
bool p[maxn];
int main(){
ll t;
cin>>t;
while(t--){
memset(p,false,sizeof(p));
cin>>n>>m;
for(ll i=1;i<=n;i++){
cin>>a[i];
}
for(ll i=1;i<=m;i++){
ll x;
cin>>x;
p[x]=true;
}
bool flag=true;
ll MAX=a[1];
ll curmax=0;
for(ll i=1;i<n;i++){
if(!p[i]){
curmax=MAX;
}
if(curmax>a[i+1]){
flag=false;
break;
}
MAX=max(a[i+1],MAX);
}
if(flag)puts("YES");
else puts("NO");
}
}
C - Perform the Combo
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn=2e5+10;
ll a[maxn];
int main(){
ll t;
ll n,m;
string s;
cin>>t;
while(t--){
map<char,ll>mp;
memset(a,0,sizeof(a));
cin>>n>>m>>s;
for(ll i=0;i<n;i++){
mp[s[i]]++;
}
for(ll i=1;i<=m;i++){
ll x;
scanf("%lld",&x);
a[x]++;
}
for(ll i=n;i>0;i--){
a[i]+=a[i+1];
}
for(int i=0;i<n;i++){
mp[s[i]]+=a[i+1];
}
for(ll i=0;i<26;i++){
printf("%lld%c",mp['a'+i],i==25?'\n':' ');
}
}
}
D - Three Integers
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF=1e18;
ll ABS(ll x){
return x>0?x:-x;
}
int main(){
ll t;
cin>>t;
while(t--){
ll a,b,c;
cin>>a>>b>>c;
ll ans=INF;
ll A=a,B=b,C=c;
for(ll i=1;i<=2e4;i++){
for(ll j=1;j*i<=2e4;j++){
for(ll k=1;k*j*i<=2e4;k++){
if(ans>ABS(a-i)+ABS(b-j*i)+ABS(c-k*j*i)){
ans=ABS(a-i)+ABS(b-j*i)+ABS(c-k*j*i);
A=i;
B=j*i;
C=k*j*i;
}
}
}
}
cout<<ans<<endl;
cout<<A<<' '<<B<<' '<<C<<endl;
}
}
E - Construct the Binary Tree
最大的情况是一条链,然后一个点一个点往上提,提到符合条件的最高的位置,确定每一层点的个数后建二叉树即可
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn=5e3+10;
ll ans[maxn];
ll p[maxn];
ll depth[maxn];
ll po[100];
ll cnt;
void build(int root){
for(int i=0;i<=1;i++){
if(ans[depth[root]+1]){
ans[depth[root]+1]--;
cnt++;
p[cnt]=root;
depth[cnt]=depth[root]+1;
build(cnt);
}
}
}
int main(){
po[1]=1;
for(int i=2;i<=30;i++){
po[i]=po[i-1]*2;
}
int t;
cin>>t;
while(t--){
memset(p,-1,sizeof(p));
memset(ans,0,sizeof(ans));
memset(depth,0,sizeof(depth));
ll n,d;
cin>>n>>d;
for(int i=1;i<=n;i++){
ans[i]=1;
}
ll dep=n;
ll cur=n*(n-1)/2;
ll lim=0;
ll curn=n;
ll cur2=0;
ll x=1;
while(curn>0){
lim+=min(curn,x)*cur2;
curn-=min(curn,x);
cur2++;
x*=2;
}
if(d>cur||d<lim){
puts("NO");
continue;
}
curn=n;
ll curd=2;
while(cur>d){
ans[curn-min(cur-d,curn-curd)]++;
//cout<<cur<<' '<<curn-min(cur-d,curn-curd)<<endl;
ans[curn]--;
cur-=min(cur-d,curn-curd);
if(ans[curd]==po[curd])
curd++;
curn--;
}
cnt=1;
ll root=1;
ans[root]--;
depth[root]++;
build(root);
puts("YES");
for(ll i=2;i<=n;i++){
printf("%lld%c",p[i],i==n?'\n':' ');
}
}
}
F - Moving Points
树状数组解决偏序问题
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MAXN = 2e5+10;
ll N,Tree[2][MAXN];
ll Lowbit(ll i)
{
return i & (-i);
}
void Update(ll id,ll i,ll x)
{
while(i <= N)
{
Tree[id][i] = Tree[id][i] + x;
i += Lowbit(i);
}
}
ll Query(ll id,ll i)
{
ll sum = 0;
while(i > 0)
{
sum += Tree[id][i];
i -= Lowbit(i);
}
return sum;
}
int main(){
ll n;
cin>>n;
vector<pair<ll,ll>>vec(n);
for(ll i=0;i<n;i++){
cin>>vec[i].first;
}
for(ll i=0;i<n;i++){
cin>>vec[i].second;
}
sort(vec.begin(),vec.end());
vector<ll>speed(n);
for(ll i=0;i<n;i++){
speed[i]=vec[i].second;
}
sort(speed.begin(),speed.end());
speed.resize(unique(speed.begin(),speed.end())-speed.begin());
ll ans=0;
N=speed.size();
for(ll i=0;i<n;i++){
ll pos=lower_bound(speed.begin(),speed.end(),vec[i].second)-speed.begin()+1;
ans+=1LL*vec[i].first*Query(0,pos)-Query(1,pos);
Update(0,pos,1);
Update(1,pos,vec[i].first);
}
cout<<ans<<endl;
}