Codeforces Round #598 (Div. 3)补题
作者:互联网
文章目录
- A. Payment Without Change
- B. Minimize the Permutation
- C. Platforms Jumping
- D. Binary String Minimizing
- E. Yet Another Division Into Teams
- F. Equalizing Two Strings
题目
A. Payment Without Change
#include<bits/stdc++.h>
using namespace std;
int main(){
int t;
cin>>t;
while(t--){
int a,b,n,s;
cin>>a>>b>>n>>s;
int x=s/n;
x=x>a?a:x;
s=s-n*x;
if(s>b)puts("NO");
else puts("YES");
}
}
B. Minimize the Permutation
感觉写麻烦了
#include<bits/stdc++.h>
using namespace std;
const int MAXN=1e2+10;
deque<int>q;
int a[MAXN];
int main(){
int t;
scanf("%d",&t);
while(t--){
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
while(!q.empty()&&a[q.front()]>a[i]){
q.pop_front();
}
while(!q.empty()&&a[q.back()]>a[i]){
q.pop_back();
}
q.push_back(i);
}
int lim=1;
while(!q.empty()){
int x=q.front();
int pos=x;
q.pop_front();
while(pos>lim&&a[pos]<a[pos-1]){
swap(a[pos],a[pos-1]);
pos--;
}
lim=x;
}
for(int i=1;i<=n;i++){
printf("%d%c",a[i],i==n?'\n':' ');
}
}
return 0;
}
C. Platforms Jumping
#include<bits/stdc++.h>
using namespace std;
int a[1010];
int ans[1010];
int main(){
int n,m,d;
scanf("%d%d%d",&n,&m,&d);
int cur=n;
for(int i=1;i<=m;i++){
scanf("%d",&a[i]);
cur-=a[i];
}
if((d-1)*(m+1)>=cur){
puts("YES");
int j=0;
int now=0;
while(j<n){
for(int i=1;cur&&i<=d-1;i++,cur--){
ans[++j]=0;
}
if(j>=n)
break;
++now;
for(int i=1;i<=a[now];i++){
ans[++j]=now;
}
}
for(int i=1;i<=n;i++){
printf("%d%c",ans[i],i==n?'\n':' ');
}
}
else
puts("NO");
}
D. Binary String Minimizing
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
ll t;
cin>>t;
while(t--){
ll n,k,now;
scanf("%lld%lld",&n,&k);
string s;
cin>>s;
vector<int>vec;
bool flag=false;
for(int i=0;i<s.size();i++){
if(!flag&&s[i]=='1'){
flag=true;
now=i;
}
if(flag&&s[i]=='0'){
vec.push_back(i);
}
}
for(int i=0;k&&i<vec.size();i++){
if(k>=vec[i]-now){
k-=vec[i]-now;
swap(s[vec[i]],s[now]);
now++;
}
else{
swap(s[vec[i]],s[vec[i]-k]);
k=0;
break;
}
}
cout<<s<<endl;
}
}
E. Yet Another Division Into Teams
一个队只有3,4,5人三种情况,先排序后dp,dp[i]表示以i为结尾的答案
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn=2e5+10;
const ll INF=0x3f3f3f3f;
ll a[maxn];
ll p[maxn];
ll dp[maxn];
ll team[maxn];
pair<ll,ll>pa[maxn];
int main(){
ll n;
scanf("%lld",&n);
for(ll i=1;i<=n;i++){
scanf("%lld",&a[i]);
pa[i].first=a[i];
pa[i].second=i;
}
sort(pa+1,pa+1+n);
memset(p,-1,sizeof(p));
memset(dp,INF,sizeof(dp));
dp[0]=0;
for(ll i=1;i<=n;i++){
for(ll j=2;j<=4&&i+j<=n;j++){
if(dp[i+j]>dp[i-1]+pa[i+j].first-pa[i].first){
p[i+j]=i-1;
dp[i+j]=dp[i-1]+pa[i+j].first-pa[i].first;
//cout<<i+j<<' '<<p[i+j]<<endl;
}
}
}
ll cnt=0;
ll cur=n;
while(cur){
cnt++;
for(int i=cur;i>p[cur];i--){
team[pa[i].second]=cnt;
}
cur=p[cur];
}
printf("%lld %lld\n",dp[n],cnt);
for(ll i=1;i<=n;i++){
printf("%lld%c",team[i],i==n?'\n':' ');
}
}
F. Equalizing Two Strings
讨论题,若两个字符串字符集不同 ,则NO,否则若存在相同的字母出现次数超过1,则YES,因为先时两个相同的字母相邻,则可在交换两个字母不引起变化的同时任意改变另一个字符串。
若不满足上述两种情况,则两个字符串变为两个不含重复元素排列,判断逆序对的个数就行是都相同即可。
#include<bits/stdc++.h>
using namespace std;
string s1,s2;
int cnt1[26],cnt2[26],bit[2][27];
int lowbit(int x){
return x&(-x);
}
void up(int id,int pos,int val){
while(pos<=26){
bit[id][pos]+=val;
pos+=lowbit(pos);
}
}
int qu(int id,int pos){
int ans=0;
while(pos>0){
ans+=bit[id][pos];
pos-=lowbit(pos);
}
return ans;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
memset(cnt1,0,sizeof(cnt1));
memset(cnt2,0,sizeof(cnt2));
memset(bit,0,sizeof(bit));
int n;
scanf("%d",&n);
cin>>s1;
cin>>s2;
for(int i=0;i<s1.size();i++){
cnt1[s1[i]-'a']++;
}
for(int i=0;i<s2.size();i++){
cnt2[s2[i]-'a']++;
}
bool flag1=true,flag2=false;
for(int i=0;i<26;i++){
if(cnt1[i]!=cnt2[i])flag1=false;
if(cnt1[i]>1)flag2=true;
}
if(!flag1)puts("NO");
else if(flag2)puts("YES");
else{
int inv1=0,inv2=0;
for(int i=0;i<s1.size();i++){
inv1+=qu(0,s1[i]-'a'+1);
up(0,s1[i]-'a'+1,1);
}
for(int i=0;i<s2.size();i++){
inv2+=qu(1,s2[i]-'a'+1);
up(1,s2[i]-'a'+1,1);
}
if((inv1&1)==(inv2&1))
puts("YES");
else
puts("NO");
}
}
}
GL&HF
发布了22 篇原创文章 · 获赞 3 · 访问量 406
私信
关注
标签:int,598,ll,pos,while,补题,Div,now,scanf 来源: https://blog.csdn.net/qq_44290978/article/details/104008652