Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3)

B

思路是先对第一个输入处理后,
从第二个输入开始,当前输入如果大于前一个输入,那么输出等于此输入,并且把未用到的数字存在c中,end为指向数组末尾,用来保存数字。
如果当前输入等于前一个输入,那么输出c数组最前面的没被用的数字,begin指向数组开头,用于取数字。
开始最后一起输出,导致超时,边循环边输出就过了。

#include<bits/stdc++.h> 
using namespace std;
typedef long long ll;
 bool flag;
 int a[100010];
 int b[100010];
 int c[100010];
int main(){
 ios::sync_with_stdio(false);
 cout.tie(NULL);
 int t;
 cin>>t;
 while(t--){
  int n;
  cin>>n;
  
  memset(a,0,sizeof(a));
  memset(b,0,sizeof(b));
  memset(c,0,sizeof(c));
  flag=true;
  for(int i=1;i<=n;i++){
   cin>>a[i];
   if(a[i]<i){
    flag=false;
    string s;
    getline(cin,s);//开始这里用了continue然后导致没输入完一直不对
    break;
   }
  }
   int num;
   int begin=1;
   int end=1;
  if(flag==false){
   cout<<"-1";
  }
  else{
   b[1]=a[1];
   cout<<b[1]<<' ';
   if(a[1]>1){
    for(int i=1;i<a[1];i++){
    c[i]=i;
    }
    begin=1;
    end=a[1]-1;
   }
   else end=0;
   for(int i=2;i<=n;i++){
    if(a[i-1]<a[i]){
     b[i]=a[i];
     cout<<b[i]<<' ';
     for(int j=a[i-1]+1;j<a[i];j++){
      c[end+j-a[i-1]]=j;
     }
     end+=a[i]-a[i-1]-1;
    }
    else{
     b[i]=c[begin];
     cout<<b[i]<<' ';
     begin++;
     } 
   }
  }
  cout<<endl;
 }
 return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值