小组练习第二周----C

You are given a long decimal number a consisting of n digits from 1 to 9. You also have a function f that maps every digit from 1 to 9 to some (possibly the same) digit from 1 to 9.

You can perform the following operation no more than once: choose a non-empty contiguous subsegment of digits in a, and replace each digit x from this segment with f(x). For example, if a=1337, f(1)=1, f(3)=5, f(7)=3, and you choose the segment consisting of three rightmost digits, you get 1553 as the result.

What is the maximum possible number you can obtain applying this operation no more than once?

Input
The first line contains one integer n (1≤n≤2⋅105) — the number of digits in a.

The second line contains a string of n characters, denoting the number a. Each character is a decimal digit from 1 to 9.

The third line contains exactly 9 integers f(1), f(2), …, f(9) (1≤f(i)≤9).

Output
Print the maximum number you can get after applying the operation described in the statement no more than once.

题意:给定一个数组,将数组分别为指定的份,使每份里面的数的和为奇数
思路:先判断共有多少个奇数,通过奇数的数目与份数比较,得出是否有结果,接着没遇见一个奇数时就记录下输出
注:该题主要由于数据量过大,导致一直超时所以用上了快速输入
代码(未用快速输入):

import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class Main{
	public static void main(String[] args) throws IOException {
		Scanner sc=new Scanner(System.in);
		for(int t=sc.nextInt();t>0;t--) {
			int n=sc.nextInt();
			int m=sc.nextInt();
			int arr[]=new int[n];
			StringBuilder s=new StringBuilder("");
			int j=0;
			for(int i=0;i<n;i++) {
				arr[i]=sc.nextInt();
				if(arr[i]%2==1) {
					if(j<m-1)s=s.append((i+1)+" ");
					j++;
				}
			}
			if(j-m>=0&&(j-m)%2==0) {
				System.out.println("YES");
				System.out.println(s.append(n+""));
			}else {
				System.out.println("NO");
			}
		}
	}
}

该代码我优化了好几次结果时Time limit,然后才想到用快速输入
代码(快速输入):

import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class Main{
 public static void main(String[] args) throws IOException {
  StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
  PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
  while (in.nextToken() != StreamTokenizer.TT_EOF) {
   int t=(int)in.nval;
   for(;t>0;t--) {
    in.nextToken();//指向下一个
    int n=(int)in.nval;
    in.nextToken();//指向下一个
    int m=(int)in.nval;
    int arr[]=new int[n];
    StringBuilder s=new StringBuilder("");
    int j=0;
    for(int i=0;i<n;i++) {
     in.nextToken();//指向下一个
     arr[i]=(int)in.nval;
     if(arr[i]%2==1) {
      if(j<m-1)s=s.append((i+1)+" ");
      j++;
     }
    }
    if(j-m>=0&&(j-m)%2==0) {
     System.out.println("YES");
     System.out.println(s.append(n+""));
    }else {
     System.out.println("NO");
    }
   }
   //break;
  }
 }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值