codechef AUG17 T2 Chef and Mover
Chef and Mover Problem Code: CHEFMOVR
Chef's dog Snuffles has so many things to play with! This time around, Snuffles has an array A containing N integers: A1, A2, ..., AN.
Bad news: Snuffles only loves to play with an array in which all the elements are equal.
Good news: We have a mover of size D!
A mover of size D is a tool which helps to change arrays. Chef can pick two existing elements Ai and Aj from the array, such that i + D = j and subtract 1 from one of these elements, and add 1 to the other element. In effect, a single operation of the mover, moves a value of 1 from one of the elements to the other.
Chef wants to find the minimum number of times she needs to use the mover of size D to make all the elements of the array A equal. Help her find this out.
Input
- The first line of the input contains an integer T, denoting the number of test cases. The description of T test cases follows.
 - The first line of each test case contains two integers N and D, denoting the number of elements in the array and the size of the mover.
 - The second line of each testcase contains N space-separated integers: A1, A2, ..., AN, denoting the initial elements of the array.
 
Output
- For each test case, output a single line containing the minimum number of uses or -1 if it is impossible to do what Snuffles wants.
 
Constraints
- 1 ≤ T ≤ 10
 - 2 ≤ N ≤ 105
 - 1 ≤ D < N
 - 1 ≤ Ai ≤ 109
 
Subtasks
- Subtask 1 (30 points) : N ≤ 103
 - Subtask 2 (70 points) : Original constraints
 
Example
Input:
3
5 2
1 6 5 0 3
3 1
0 3 0
4 2
3 4 3 5 Output:
5
2
-1
Explanation
Testcase 1:
Here is a possible sequence of usages of the mover:
- Move 1 from A3 to A1
 - Move 1 from A3 to A1
 - Move 1 from A2 to A4
 - Move 1 from A2 to A4
 - Move 1 from A2 to A4
 
At the end, the array becomes (3, 3, 3, 3, 3), which Snuffles likes. And you cannot achieve this in fewer moves. Hence the answer is 5.
Testcase 2:
Here is a possible sequence of usages of the mover:
- Move 1 from A2 to A1
 - Move 1 from A2 to A3
 
At the end, the array becomes (1, 1, 1), which Snuffles likes. And you cannot achieve this in fewer moves. Hence the answer is 2.
Testcase 3:
It is impossible to make all the elements equal. Hence the answer is -1.
——————————————————————————————————————
这道题我居然WA了两次QAQ
其实题意就是给你n个数和一个d 相邻距离为d的数字可以进行你+1我-1的操作 求最少的操作次数使得全部的数相等
所以我们一开始就要把所有的数加起来得到sum 如果sum%n!=0 那么肯定不存在这么一个答案
因为你无论怎么加减 总和都是不变的
然后再判断相邻d的所有数加起来是否为0 不如!=0 说明答案也不存在 这个也很好证明
最后统计答案的时候 我一开始想的是把所有的正数加起来 但是发现不行
比如这样一个例子 d=1时 4 1 1
按上面来算答案应该是2但是答案其实是3
因为你只能相邻交换 所以应该是4给第一个1 2 然后 第一个1给第二个1 1
答案是3
所以我们统计答案的时候可以像均分纸牌一样 每次拿出第一个数(<=k)
他只能从下一个数拿 而下一个数不可能从这个数再拿回来 所以他只能从下一个拿
这样把值推过去再慢慢统计就好辣
这样这个问题就解决辣
codechef AUG17 T2 Chef and Mover的更多相关文章
- codechef AUG17 T1 Chef and Rainbow Array
		
Chef and Rainbow Array Problem Code: RAINBOWA Chef likes all arrays equally. But he likes some array ...
 - codechef AUG17 T5 Chef And Fibonacci Array
		
Chef has an array A = (A1, A2, ..., AN), which has N integers in it initially. Chef found that for i ...
 - codechef T2 Chef and Sign Sequences
		
CHEFSIGN: 大厨与符号序列题目描述 大厨昨天捡到了一个奇怪的字符串 s,这是一个仅包含‘<’.‘=’和‘>’三种比较符号的字符串. 记字符串长度为 N,大厨想要在字符串的开头.结尾 ...
 - CodeChef SADPAIRS:Chef and Sad Pairs
		
vjudge 首先显然要建立圆方树 对于每一种点建立虚树,考虑这一种点贡献,对于虚树上已经有的点就直接算 否则对虚树上的一条边 \((u, v)\),\(u\) 为父亲,假设上面连通块大小为 \(x\ ...
 - codechef AUG17 T4 Palindromic Game
		
Palindromic Game Problem Code: PALINGAM There are two players A, B playing a game. Player A has a st ...
 - codechef AUG17 T3 Greedy Candidates
		
Greedy Candidates Problem Code: GCAC The placements/recruitment season is going on in various colleg ...
 - CodeChef A String Game(SG)
		
A String Game Problem code: ASTRGAME Submit All Submissions All submissions for this problem a ...
 - CF&&CC百套计划2 CodeChef December Challenge 2017 Chef And Easy Xor Queries
		
https://www.codechef.com/DEC17/problems/CHEFEXQ 题意: 位置i的数改为k 询问区间[1,i]内有多少个前缀的异或和为k 分块 sum[i][j] 表示第 ...
 - CF&&CC百套计划2 CodeChef December Challenge 2017 Chef and Hamming Distance of arrays
		
https://www.codechef.com/DEC17/problems/CHEFHAM #include<cstdio> #include<cstring> #incl ...
 
随机推荐
- linux学习(1)——这是一个新的开始,加油吧少年
			
(一)自己简单总结 学会使用简单命令 Tab:实现自动补全功能 Ctrl+D:退出当前终端 Ctrl+Z:暂停当前进程 Ctrl+L:清屏 Ctrl+A:可以让光标移动到最前列 Ctrl+E:可以让 ...
 - 学习Pytbon第十七篇,面向对象编程
			
面向对象技术简介 类(Class): 用来描述具有相同的属性和方法的对象的集合.它定义了该集合中每个对象所共有的属性和方法.对象是类的实例. 类变量:类变量在整个实例化的对象中是公用的.类变量定义在类 ...
 - strak组件(6):列表定制列应用和引入静态文件
			
效果图: 新增函数 def get_choice_text(title, field) 闭包函数,显示choice字段 def inner(self, obj=None, is_header=None ...
 - 裸机——ADC
			
1.首先是ADC的基本知识 模拟信号,连续的 数字信号,离散的 模拟信号,现实世界的很多东西都是连续的,所以使用模拟信号才能准确描述,但是模拟信号不方便控制. 数字信号,计算机中的信号大都为数字的,数 ...
 - 7,Flask 中路由系统
			
Flask中的路由系统 @app.route("/",methods=["GET","POST"]) 为什么要这么用?其中的工作原理我们知道 ...
 - [转] PHP在不同页面之间传值的三种常见方式
			
转自: http://my.oschina.net/jiec/blog/196153 一. POST传值 post传值是用于html的<form>表单跳转的方法,很方便使用.例如: < ...
 - unity3d中C#与JS的一些区别
			
unity3d目前支持C#和JS两种脚本语言. 学习过程中发现很多教程使用的是JS语言,自己还是用C#比较多,unity原生使用的是Mono,使用C#会更加接近unity的编程思想. 1.方法的定义, ...
 - Python调用Webservice
			
使用Python调用webservice 推荐使用 suds包 该包一般在Python2.x python3各种麻烦 略过 实例 import suds # webservice url url ...
 - Python学习-day18 Web框架
			
众所周知,对于所有的Web应用,本质上其实就是一个socket服务端,用户的浏览器其实就是一个socket客户端. ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1 ...
 - SSH非交互式密码授权远程执行脚本
			
公司有上百台服务器,需要为每台服务器都执行一个脚本,因为所有服务器的账号密码都是一样的,所以可以不用搭建ansible等自动化运维工具,我们直接通过SSH远程执行即可完成. 本文以三台服务器为例,系统 ...