小美的彩带是由一条长度为
的彩带一直无限循环得到的,彩带的每一个位置都有一个颜色,用
表示。因此当
时,
。
小美每次会从左往右或从右往左剪一段长度为
的彩带,她想知道她每次剪下来的彩带有多少种颜色。
第一行输入两个整数
代表彩带长度、剪彩带次数。
第二行输入
个整数
代表彩带每一个位置的颜色。
此后
行,每行输入一个字符
和一个整数
代表裁剪方向和裁剪长度,其中 '
' 说明从左往右剪, '
' 说明从右往左剪 。
对于每一次裁剪彩带,在一行上输出一个整数代表颜色数量。
6 4 1 1 4 5 1 4 L 2 L 3 R 12 R 1
1 3 3 1
第一次剪彩带,剪下来的是
,有
这
种颜色;
第二次剪彩带,剪下来的是
,有
这
种颜色;
第三次剪彩带,剪下来的是
,有
这
种颜色。
第四次剪彩带,剪下来的是
,有
这一种颜色。
package com.e; import java.util.*; /** * 10 10 * 4 5 3 10 4 7 10 5 4 8 * L 8 * R 4 * L 3 * L 4 * L 1 * L 3 * R 4 * R 3 * L 10 * L 10 * 这个案例没通过 * * * 1 5 1 5 * 2 4 2 4 * 3 3 3 2 * 4 4 4 4 * 5 1 5 1 * 6 3 6 3 * 7 3 7 4 * 8 3 8 3 * 9 6 9 6 * 10 6 10 6 */ public class 减彩带 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int q = scanner.nextInt(); int[] colors = new int[n]; Set<Integer> set=new HashSet<>(); for (int i = 0; i < n; i++) { colors[i] = scanner.nextInt(); set.add( colors[i]); } // 初始化裁剪区间 int start = 0; int end = -1; int leftIndex=0; int beforLeftIndex=0; int rightIndex=0; int beforeRightIndex=0; int ColorCount[]=new int[q-1]; for(int j=0;j<q;j++){ String derect=scanner.next(); int cutCount=scanner.nextInt(); if(derect.equals("L")){ if(cutCount>=colors.length){ ColorCount[j]= set.size(); beforLeftIndex=leftIndex; leftIndex=(beforLeftIndex+cutCount)%colors.length; } if(cutCount<colors.length){ beforLeftIndex=leftIndex; leftIndex=(beforLeftIndex+cutCount)%colors.length; ColorCount[j]=getColorSizeLeft(beforLeftIndex,leftIndex,colors); } } //从右到左的下标取值时,要从颜色数组的反向取 if(derect.equals("R")){ if(cutCount>=colors.length){ ColorCount[j]= set.size(); beforeRightIndex=beforeRightIndex; rightIndex=(rightIndex+cutCount)%colors.length; } if(cutCount<colors.length){ beforLeftIndex=leftIndex; leftIndex=(leftIndex+cutCount)%colors.length; ColorCount[j]=getColorSizeRight(beforLeftIndex,leftIndex,colors); } } } for (int i = 0; i < ColorCount.length; i++) { System.out.print(ColorCount[i]+" "); } } public static int getColorSizeLeft(int startIndex,int endIndex,int[] colors){ Set<Integer> set=new HashSet<>(); if(endIndex>startIndex){ for(int i=startIndex;i<endIndex;i++){ set.add(colors[i]); } }else{ for(int i=startIndex;i<colors.length;i++){ set.add(colors[i]); } for(int i=0;i<endIndex;i++){ set.add(colors[i]); } } return set.size(); } public static int getColorSizeRight(int startIndex,int endIndex,int[] colors){ Set<Integer> set=new HashSet<>(); if(endIndex>startIndex){ for(int i=startIndex;i<endIndex;i++){ set.add(colors[(colors.length-1)-i]); } }else{ for(int i=startIndex;i<colors.length;i++){ set.add(colors[colors.length-1-i]); } for(int i=0;i<endIndex;i++){ set.add(colors.length-1-i); } } return set.size(); } }