2023/11/22#算法周赛补题(2)

链接:登录—专业IT笔试面试备考平台_牛客网
来源:牛客网
 

题目描述

Joker最近进行了一轮非常有争议的比赛,之后他的贡献迅速发生了变化。公告在主页上挂了 n 秒。在第 i 秒,第|ai| 人点赞或删除赞。如果是 ai > 0,则第 |ai |个人点赞。如果 ai < 0,则 表示第|ai| 人移除赞。每个人放置和移除赞的次数不得超过一次。如果一个人之前没有放入喜欢的内容,则不能移除喜欢的内容。

Joker想分析一下他的贡献在主页面发布公告时发生了怎样的变化。他向平台的创建者提出申请,要求将序列 a1, a2,..., an给他。但由于平台的不完善,序列 a 被打乱了。

你将会得到一个描述用户活动的打乱后的序列 a 。你需要指出从 1 到 n 的每个时刻,帖子在该时刻可能获得的最大和最小点赞数。

输入描述:

 
 

第一行输入数据包含一个数字t(1 <= t <= 1000 ) - 测试用例的数量。

在测试用例的第一行中,有一个数字n(1 <= n <= 100 ) - Joker的公告挂在主页上的秒数。

下一行包含n个数字a1, a2, a3,..., an。( 1 <= |ai| <= n ) - 数组 a 。保证存在这样一种 a 的排列,即条件中描述的事件序列是正确的。

输出描述:

 
 

对于每个测试用例,输出两行,每行包含 n 个数字。

在第一行中,针对每个测试用例,输出Joker在第 i 秒看到公告时可能获得的最大点赞数。

在第二行中,针对每个测试用例,输出Joker在第 i 秒发布消息时可能获得的最小点赞数。

示例1

输入

复制5 3 1 2 -2 2 1 -1 6 4 3 -1 2 1 -2 5 4 2 -2 1 3 7 -1 6 -4 3 2 4 1
5
3
1 2 -2
2
1 -1
6
4 3 -1 2 1 -2
5
4 2 -2 1 3
7
-1 6 -4 3 2 4 1

输出

复制1 2 1 1 0 1 1 0 1 0 1 2 3 4 3 2 1 0 1 0 1 2 1 2 3 4 3 1 0 1 2 3 1 2 3 4 5 4 3 1 0 1 0 1 2 3
1 2 1
1 0 1
1 0
1 0
1 2 3 4 3 2
1 0 1 0 1 2
1 2 3 4 3
1 0 1 2 3
1 2 3 4 5 4 3
1 0 1 0 1 2 3

说明

 
     

在第一个测试案例中,最大值是通过以下排列达到的:1, 2, -2 。最小值是通过以下序列达到: 2, -2, 1 

import java.util.*;
public class Main {
 public static void main(String[] args){
    Scanner sc=new Scanner(System.in);
         int t=sc.nextInt();
       while(t-->0){
           int n=sc.nextInt();
           int[] arr=new int[n];
           int count1=0,count2=0;
           for(int i=0;i<n;i++){
               arr[i]=sc.nextInt();
               if(arr[i]>0){
                   count1++;
               }else{
                   count2++;
               }
           }
           for(int i=1;i<=count1;i++){
               System.out.print(i+" ");
           }
           for(int i=count1-1;i>=0;i--){
               if(count1-i<=count2){
                   System.out.print(i+" ");
               }  
           }
            System.out.println();
           for(int i=1;i<=count2;i++){
               System.out.print("1 0"+" ");
           }
           for(int i=1;i<=count1-count2;i++){
               System.out.print(i+" ");
           }
           System.out.println();
       }
 }
}

 补充知识点:

在进行算法比赛时,时间复杂度是不可避免的,不仅要想简化的方法  去优化,比如二分,前缀和,来降低时间复杂度。Java快读也是一个好的用法,因为Java中的输入Scanner非常慢,还没读入有时候时间就到了,在比赛时准备一套快读模板也是非常必要的。

import java.io.*;
import java.util.StringTokenizer;
public class Main {
static int zu;
public static void main(String[] args) {
zu=in.nextInt();
while(zu-->0) solve();
out.flush();
}
static void solve(){
}
static FastReader in = new FastReader();
static PrintWriter out=new PrintWriter(System.out);
static class FastReader{
static BufferedReader br;
static StringTokenizer st;
FastReader(){
br=new BufferedReader(new InputStreamReader(System.in));
}
String next(){
String str="";
while(st==null||!st.hasMoreElements()){
try {
str=br.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
st=new StringTokenizer(str);
}
return st.nextToken();
}
int nextInt(){
return Integer.parseInt(next());
}
double nextDouble(){
return Double.parseDouble(next());
}
long nextLong(){
return Long.parseLong(next());
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值