Codeforces 908

文章讨论了一个关于整数数组的问题,给定一个数组,询问是否可以通过选择一个元素并左移数组来达到某个状态。关键在于判断是否存在环,如果存在环且步数有限,则无法确定是否可以回退到初始状态。
摘要由CSDN通过智能技术生成

C Anonymous Informant

题目大意

  • 给定一个数组b,问是否存在数组a,进行k次操作变为b
  • 操作为:选择数组a中,a_x=x的一点,然后数组整体左移x
  • 左移一位:a_1,a_2,\cdots ,a_n\rightarrow a_2,\cdots a_n,a_1
  • 1\leq k\leq 1e9,1\leq n\leq 2e5

解题思路

  • 对于a_i>n的点,一定不会被选择
  • 进行一次操作后,a_x=x的点会被移动到数组末尾
  • 则对于b_n,其一定是上一步进行操作所选的定点
  • 考虑从数组b反推回a,若能回退k步,则一定存在
  • 由于回退过程中,若出现当前状态与前面某状态相同,则成环,之后可以回退任意步
  • 对于末尾为x的状态,下次再回到该状态,需要n/x
  • 而最大环为末尾位一直为1,大小为n
  • 所以若存在数组a,最多回退n

 

package Tx;
import java.io.*;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.BitSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Random;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.Vector;







public class Tx1{
	static long md=(long)998244353;
	static long Linf=Long.MAX_VALUE/2;
	static int inf=Integer.MAX_VALUE/2;
	
	
	public static void main(String[] args) throws IOException{
		AReader input=new AReader();
	    PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
	    
	    int T=input.nextInt();
	    while(T>0){
	    	int n=input.nextInt();
	    	int k=input.nextInt();
	    	k=Math.min(n, k);
	    	int[] b=new int[n+1];
	    	for(int i=1;i<=n;++i) {
	    		b[i]=input.nextInt();
	    	}
	    	int last=n;
	    	boolean ok=true;
	    	for(int i=1;i<=k;++i) {
	    		if(b[last]>n) {
	    			ok=false;
	    			break;
	    		}
	    		last-=b[last];
	    		if(last<=0)last+=n;
	    	}
	    	if(ok)out.println("Yes");
	    	else out.println("No");
	    	T--;
	    }
 	    out.flush();
	    out.close();
	}
	//System.out.println();
	//out.println();
	static
	class AReader{
	    BufferedReader bf;
	    StringTokenizer st;
	    BufferedWriter bw;

	    public AReader(){
	        bf=new BufferedReader(new InputStreamReader(System.in));
	        st=new StringTokenizer("");
	        bw=new BufferedWriter(new OutputStreamWriter(System.out));
	    }
	    public String nextLine() throws IOException{
	        return bf.readLine();
	    }
	    public String next() throws IOException{
	        while(!st.hasMoreTokens()){
	            st=new StringTokenizer(bf.readLine());
	        }
	        return st.nextToken();
	    }
	    public char nextChar() throws IOException{
	        //确定下一个token只有一个字符的时候再用
	        return next().charAt(0);
	    }
	    public int nextInt() throws IOException{
	        return Integer.parseInt(next());
	    }
	    public long nextLong() throws IOException{
	        return Long.parseLong(next());
	    }
	    public double nextDouble() throws IOException{
	        return Double.parseDouble(next());
	    }
	    public float nextFloat() throws IOException{
	        return Float.parseFloat(next());
	    }
	    public byte nextByte() throws IOException{
	        return Byte.parseByte(next());
	    }
	    public short nextShort() throws IOException{
	        return Short.parseShort(next());
	    }
	    public BigInteger nextBigInteger() throws IOException{
	        return new BigInteger(next());
	    }
	    public void println() throws IOException {
	        bw.newLine();
	    }
	    public void println(int[] arr) throws IOException{
	        for (int value : arr) {
	            bw.write(value + " ");
	        }
	        println();
	    }
	    public void println(int l, int r, int[] arr) throws IOException{
	        for (int i = l; i <= r; i ++) {
	            bw.write(arr[i] + " ");
	        }
	        println();
	    }
	    public void println(int a) throws IOException{
	        bw.write(String.valueOf(a));
	        bw.newLine();
	    }
	    public void print(int a) throws IOException{
	        bw.write(String.valueOf(a));
	    }
	    public void println(String a) throws IOException{
	        bw.write(a);
	        bw.newLine();
	    }
	    public void print(String a) throws IOException{
	        bw.write(a);
	    }
	    public void println(long a) throws IOException{
	        bw.write(String.valueOf(a));
	        bw.newLine();
	    }
	    public void print(long a) throws IOException{
	        bw.write(String.valueOf(a));
	    }
	    public void println(double a) throws IOException{
	        bw.write(String.valueOf(a));
	        bw.newLine();
	    }
	    public void print(double a) throws IOException{
	        bw.write(String.valueOf(a));
	    }
	    public void print(char a) throws IOException{
	        bw.write(String.valueOf(a));
	    }
	    public void println(char a) throws IOException{
	        bw.write(String.valueOf(a));
	        bw.newLine();
	    }
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值