2024牛客寒假算法基础集训营5

E,F soyorin的数组操作

一次操作选一个右端点j(j\%2==0), 使得i\in [1,j],a_i+1*i

F要求最小操作

解题思路

  • E考虑能否
  • n为偶数,则可以直接选整个区间[1,n]进行操作
  • n为奇数,则a_n不会受操作影响
  • 要求非降序,则a_{n-1}<=a_n
  • 所以a_{n-1}最多进行\frac{a_n-a_{n-1}}{n-1}次加操作,a_{n-1}+=(n-1)*(\frac{a_n-a_{n-1}}{n-1})
  • 同理每个偶数位a_i最多进行\frac{a_{i+1}-a_i}{i}次加操作,将其更新为最大
  • 对于每个奇数位,直接进行判断是否满足
  • 从后往前累计操作数,判断当前进行操作后是否满足非降序
  • 不满足则为”NO“,因为后面的数已经加到最大了
  • F要求输出最小操作数
  • 在有解的情况下,一定是降序中两者相差最大的
  • 因为要将两者相差最大的调为非降序,至少需要其差值的步骤k
  • 又因为在操作过程中,区间可以选择往后伸展,去调整后面的,其所需步骤一定小于k
  • 调整后面的可以看作k中的一次操作

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 Main{
	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();
		    long[] a=new long[n+1];
		    long mx=0;
		    for(int i=1;i<=n;++i) {
		    	a[i]=input.nextLong();
		    	if(i!=1)mx=Math.max(mx, a[i-1]-a[i]);
		    }
		    if(n==1) {
		    	out.println(0);
		    	T--;
		    	continue;
		    }
		    if(n%2==0) {
		    	out.println(mx);
		    	T--;
		    	continue;
		    }else {
		    	long cnt=0;
			    boolean ok=true;
		    	for(int i=n-1;i>=1;--i) {
		    		a[i]+=cnt*i;
		    		if(a[i]>a[i+1]) {
		    			ok=false;
		    			break;
		    		}
		    		if(i%2==0) {
		    			cnt+=(a[i+1]-a[i])/i;
	                    a[i]+=i*((a[i+1]-a[i])/i);
		    		}
		    	}
		    	if(ok)out.println(mx);
		    	else out.println(-1);
		    }
		    
	    	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();
	    }
	}
}


G,H sakiko的排列构造

 

解题思路

  • n+k为质数,则k+n也为质数
  • 则构造出区间[k,n],p_i=n,n-1,\cdots ,kp_i+i均为质数
  • 此时还剩区间[1,k-1],数值1,2,\cdots ,k-1
  • 即构造n=k-1,同理子问题

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 Main{
	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 n=input.nextInt();
	    int cnt=0;
	    int[] prim=new int[1000001];
	    boolean[] isnotPrim=new boolean[1100001];
	    int N=1100000;

	    for(int i=2;i<=N;++i) {
	    	if(!isnotPrim[i]) {
	    		cnt++;
	    		prim[cnt]=i;
	    	}
	    	/* prim:1*i,2*i,……,i*i
               notprim:合数质因数分解,只被最小的质因数访问一次
            */
	    	for(int j=1;j<=cnt;++j) {
	    		int now=prim[j]*i;
	    		if(now>N)break;
	    		
	    		isnotPrim[now]=true;
	    		
	    		if(i%prim[j]==0) break;
	    		
	    	}
	    }
	    int[] ans=new int[n+1];
		int t=n;
		while(t>0) {
			int l=1,r=cnt;
			int may=0;//找到最小比t大的质数
			while(l<=r) {
				int mid=(l+r)>>1;
				if(prim[mid]>t) {
					may=prim[mid];
					r=mid-1;
				}else l=mid+1;
			}
			int p=may-t;
			int j=t;
			for(int i=p;i<=t;++i,--j) {
				ans[i]=j;
			}
			t=j;
		}
	    for(int i=1;i<=n;++i)out.print(ans[i]+" ");
	    
	    
 	    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();
	    }
	}
}

 K soyorin的通知

 

 

解题思路

  • n很小,最终求状态等于n的最值
  • 物品i,有代价a_i,价值k\in [1,b_i]
  • 物品0,有代价p,价值1
  • 操作次数不限,完全背包

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 Main{
	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 n=input.nextInt();
	    int p=input.nextInt();
	    long[] a=new long[n+1];
	    int[] b=new int[n+1];
	    for(int i=1;i<=n;++i) {
	    	a[i]=input.nextLong();
	    	b[i]=input.nextInt();
	    }
	    long[] res=new long[n+1];
	    for(int i=1;i<=n;++i) {
	    	res[i]=(long)i*p;
	    }
	    for(int i=1;i<=n;++i) {
	    	for(int j=1;j<=n-1;++j) {
	    		int nxt=Math.min(n, j+b[i]);
	    		res[nxt]=Math.min(res[nxt], res[j]+a[i]);
	    	}
	    }
	    out.print(res[n]);
 	    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();
	    }
	}
}

 

相关推荐

  1. 2024寒假算法基础集训1 D数组成鸡

    2024-02-23 04:44:01       38 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-02-23 04:44:01       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-23 04:44:01       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-23 04:44:01       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-23 04:44:01       18 阅读

热门阅读

  1. 【CF】团队训练赛1 J-Mex Tree 题解

    2024-02-23 04:44:01       36 阅读
  2. vs code Conda退出虚拟环境报错 ERROR REPORT

    2024-02-23 04:44:01       36 阅读
  3. docker 的volume 是个什么概念

    2024-02-23 04:44:01       30 阅读
  4. C语言——static的三大用法

    2024-02-23 04:44:01       29 阅读
  5. 怎么使用Git进行版本恢复

    2024-02-23 04:44:01       28 阅读
  6. golang 读取压缩包文件 && 写文件

    2024-02-23 04:44:01       27 阅读
  7. 【Go】五、Grpc 的入门使用

    2024-02-23 04:44:01       26 阅读
  8. 编程笔记 Golang基础 012 项目构建

    2024-02-23 04:44:01       28 阅读
  9. c语言实现模块度算法

    2024-02-23 04:44:01       29 阅读
  10. rust实战系列十四:复合数据类型

    2024-02-23 04:44:01       25 阅读