Constant Palindrome Sum

简介: Constant Palindrome Sum

Constant Palindrome Sum


传送门

需要知识:差分数组

题意:对于数列,使a[i]+a[n-i+1]=x,用[1,k]之间任意一个数替换a[i]或者是a[n-i+1],求出替换最小的次数。

这道题用的是差分数组,用差分数组维护x=[2,2k]的次数。这道题我是没有想出来,看了题解才知道,而且差分我也很少运用,算是盲区了

对于每对a[i]+a[n-i+1],设置sum=a[i]+a[n-i+1],minn=min(a[i],a[n-i+1]),maxx=max(a[i],a[n-i+1]).

当x=[2,minn]时,两个数必须都修改,因为a[i]最小为1嘛.

当x=[minn+1,sum-1],[sum+1,maxx+k],修改一个数

当x[maxx+k+1,2k]修改两个数

最后的出来的是次数,而不是那个数字,所以用差分还是很好做的

import java.util.*;
public class pd{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    int t = sc.nextInt();
    while(t-->0){
      int n = sc.nextInt();
      int k = sc.nextInt();
      int[] a = new int[n];
      int[] cnt = new int[2*k+1];
      int[] pref = new int[2*k+2];
      for(int i=0;i<n;i++){
        a[i]=sc.nextInt();
      }
      for(int i=0;i<n/2;i++){
        cnt[a[i]+a[n-i-1]]++;
      }
      for(int i=0;i<n/2;i++){
        //int l1=a[i]+1;
        //int r1 = a[n-i+1]+1;
        //int l2 = a[i]+k;
        //int r2 = a[n-i+1]+k;
        int l = Math.min(a[i],a[n-i-1])+1;
        int r = Math.max(a[i],a[n-i-1])+k;
        pref[l]++;
        pref[r+1]--;
      }
      for(int i=1;i<=2*k+1;i++){
        pref[i]+=pref[i-1];
      }
      int ans = Integer.MAX_VALUE;
      for(int sum=2;sum<=2*k;sum++){
        ans = Math.min(ans,pref[sum]-cnt[sum]+(n/2-pref[sum])*2);
      }
      System.out.println(ans);
    }
  }
}
相关文章
|
2月前
|
Linux Windows
【已解决】ValueError: num_samples should be a positive integer value, but got num_samples=0
【已解决】ValueError: num_samples should be a positive integer value, but got num_samples=0
TypeError: randint() received an invalid combination of arguments - got (int, int, int), but expecte
TypeError: randint() received an invalid combination of arguments - got (int, int, int), but expecte
594 0
成功解决ValueError: min_samples_split must be an integer greater than 1 or a float in (0.0, 1.0]; got th
成功解决ValueError: min_samples_split must be an integer greater than 1 or a float in (0.0, 1.0]; got th
1024. Palindromic Number (25)
#include #include #include #include #include using namespace std; bool judge(string s){ string st = s; reverse(st.
773 0
|
Java
[LeetCode]Palindrome Number解析
链接:https://leetcode.com/problems/palindrome-number/#/description难度:Easy题目:9.Palindrome Number Determine whether an integer is a palindrome.
815 0
|
人工智能 算法
1305 Pairwise Sum and Divide
1305 Pairwise Sum and Divide 题目来源: HackerRank 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 有这样一段程序,fun会对整数数组A进行求值,其中Floor表示向下取整:   fun(A)     sum = 0     for i = 1 to A.
813 0
[LeetCode]--9. Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to s
974 0

热门文章

最新文章

  • 1
    流量控制系统,用正则表达式提取汉字
    25
  • 2
    Redis09-----List类型,有序,元素可以重复,插入和删除快,查询速度一般,一般保存一些有顺序的数据,如朋友圈点赞列表,评论列表等,LPUSH user 1 2 3可以一个一个推
    26
  • 3
    Redis08命令-Hash类型,也叫散列,其中value是一个无序字典,类似于java的HashMap结构,Hash结构可以将对象中的每个字段独立存储,可以针对每字段做CRUD
    26
  • 4
    Redis07命令-String类型字符串,不管是哪种格式,底层都是字节数组形式存储的,最大空间不超过512m,SET添加,MSET批量添加,INCRBY age 2可以,MSET,INCRSETEX
    27
  • 5
    S外部函数可以访问函数内部的变量的闭包-闭包最简单的用不了,闭包是内层函数+外层函数的变量,简称为函数套函数,外部函数可以访问函数内部的变量,存在函数套函数
    24
  • 6
    Redis06-Redis常用的命令,模糊的搜索查询往往会对服务器产生很大的压力,MSET k1 v1 k2 v2 k3 v3 添加,DEL是删除的意思,EXISTS age 可以用来查询是否有存在1
    30
  • 7
    Redis05数据结构介绍,数据结构介绍,官方网站中看到
    22
  • 8
    JS字符串数据类型转换,字符串如何转成变量,+号只要有一个是字符串,就会把另外一个转成字符串,- * / 都会把数据转成数字类型,数字型控制台是蓝色,字符型控制台是黑色,
    20
  • 9
    JS数组操作---删除,arr.pop()方法从数组中删除最后一个元素,并返回该元素的值,arr.shift() 删除第一个值,arr.splice()方法,删除指定元素,arr.splice,从第一
    20
  • 10
    定义好变量,${age}模版字符串,对象可以放null,检验数据类型console.log(typeof str)
    19