# 思维训练3

A

#include <bits/stdc++.h>
using namespace std;
int a[100],b[100];
int main()
{
int t,n;
cin>>t;
while(t--)
{
int cnt=0,sum=0;
cin>>n;
for(int i=1;i<=n;i++)
cin>>a[i];
sort(a+1,a+n+1);
for(int i=1;i<n;i++)
b[++cnt]=abs(a[i+1]-a[i]);
for(int i=1;i<=cnt;i++)
if(b[i]>1)
sum++;
if(sum==0)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
}

B

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[100],b[100];
int c[100],d[100],q[100];
int main()
{
int t,n;
cin>>t;
while(t--)
{
int e=0,s=0;
ll sum=0;
cin>>n;
for(int i=1;i<=n;i++)
cin>>a[i],c[i]=a[i];
for(int i=1;i<=n;i++)
cin>>b[i],d[i]=b[i];
sort(c+1,c+n+1);
sort(d+1,d+n+1);
e=c[1];
s=d[1];
//cout<<e<<" "<<s<<endl;
for(int i=1;i<=n;i++)
{
int g=a[i]-e;
int h=b[i]-s;
sum+=max(g,h);
}
cout<<sum<<endl;
}
}

C


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int w[100];
int main()
{
int t,n;
cin>>t;
while(t--)
{
cin>>n;
for(int i=1;i<=n;i++){
cin>>w[i];
}
sort(w+1,w+1+n);
int ans=0;
for(int k=2;k<=n<<1;k++){
int l=1,r=n,cur=0;
while(l<r){
if(w[l]+w[r]==k){
cur++;
l++;
r--;
}else
if(w[l]+w[r]<k){
l++;
}else{
r--;
}
}
ans=max(ans,cur);
}
cout<<ans<<endl;
}
return 0;
}

D

#include<bits/stdc++.h>
using namespace std;
int main(){
int t,n,ans;
int a[200001];
stack<int> pos0;
stack<int> pos1;
string s;
cin>>t;
while (t--){
memset(a,0,sizeof(a));
while (!pos0.empty())
pos0.pop();
while (!pos1.empty())
pos1.pop();
cin>>n;
cin>>s;
ans=0;
for (int i=0; i<n; i++){
if(s[i]=='0'){
if (pos1.empty()){
pos0.push(++ans);
a[i]=ans;
}
else{
a[i]=pos1.top();
pos0.push(a[i]);
pos1.pop();
}
}
else  {
if (pos0.empty()){
pos1.push(++ans);
a[i]=ans;
}
else{
a[i]=pos0.top();
pos1.push(a[i]);
pos0.pop();
}
}
}

cout<<ans<<endl;
for (int i=0; i<n; i++)
cout<<a[i]<<' ';
cout<<endl;

}
return 0;
}

03-28 1万+
03-15 1万+

02-09
10-16 1719
12-18 2465
12-19 561
11-15 3304
03-30 3779
08-24 3万+