【HackerRank】Game Of Rotation
题目连接:Game Of Rotation
Mark is an undergraduate student and he is interested in rotation. A conveyor belt competition is going on in the town which Mark wants to win. In the competition, there's A conveyor belt which can be represented as a strip of 1xN blocks. Each block has a number written on it. The belt keeps rotating in such a way that after each rotation, each block is shifted to left of it and the first block goes to last position.
There is a switch near the conveyer belt which can stop the belt. Each participant would be given a single chance to stop the belt and his PMEAN would be calculated.
PMEAN is calculated using the sequence which is there on the belt when it stops. The participant having highest PMEAN is the winner. There can be multiple winners.
Mark wants to be among the winners. What PMEAN he should try to get which guarantees him to be the winner.

where i is the index of a block at the conveyor belt when it is stopped. Indexing starts from 1.
Input Format
First line contains N denoting the number of elements on the belt.
Second line contains N space separated integers.
Output Format
Output the required PMEAN
Constraints
1 ≤ N ≤ 106
-109 ≤ each number ≤ 109
题解:一开始非常2b的觉得尽量大的索引值对应大的数组值,于是就把数组排序,然后把索引*值的和求出来。
这个想法最大的bug就是数组只能通过平移变化,不能随意变化,所以排序得到的数组有可能通过变换得不到。
正确的方法就是模拟n次变换,每次把第一个元素放到最后,然后计算新的和。注意从旧的和可以直接得到新的和: new_sum = old_sum - (a1+a2+...+an) + n*a[i]; 其中a[i]是在当前的循环中被放到结尾的数。只有它的索引值增加了(n-1),其他元素的索引值都减少了1。
最后要注意的一点是java中int型的范围是: -2147483648~2147483647,题目中最坏的情况,所有数的和能够达到1015所以要用Long型,Long型的范围是-9223372036854774808~9223372036854774807,足够了。
代码如下:
import java.io.*;
import java.util.*;
import java.math.*; public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
Long[] num = new Long[n];
Long array_sum = 0L;
Long weight = 0L;
for(int i = 0;i < n;i++){
num[i]= in.nextLong();
array_sum += num[i];
weight += (i+1) * num[i];
}
Long max = weight;
for(int i = 0;i < n;i++){
weight = weight - array_sum + n*num[i];
max = Math.max(max, weight);
} System.out.println(max); }
}
【HackerRank】Game Of Rotation的更多相关文章
- 【HackerRank】How Many Substrings?
https://www.hackerrank.com/challenges/how-many-substrings/problem 题解 似乎是被毒瘤澜澜放弃做T3的一道题(因为ASDFZ有很多人做过 ...
- 【HackerRank】Running Time of Quicksort
题目链接:Running Time of Quicksort Challenge In practice, how much faster is Quicksort (in-place) than I ...
- 【HDOJ】1667 The Rotation Game
1. 题目描述有个#字型的条带,可以从横线或竖线进行循环移动,求通过各种移动最终使中心的8个字符全等的长度最短并相同长度字典序最小的操作序列.2. 基本思路24个数据,8种移动方式,数据量很小了,所以 ...
- 【UVa】1343 The Rotation Game(IDA*)
题目 题目 分析 lrj代码.... 还有is_final是保留字,害的我CE了好几发. 代码 #include <cstdio> #include <algorit ...
- 【hackerrank】Week of Code 30
Candy Replenishing Robot Find the Minimum Number 直接模拟 Melodious password dfs输出方案 Poles 题意:有多个仓库,只能从后 ...
- 【hackerrank】Week of Code 26
在jxzz上发现的一个做题网站,每周都有训练题,题目质量……前三题比较水,后面好神啊,而且类型差不多,这周似乎是计数专题…… Army Game 然后给出n*m,问需要多少个小红点能全部占领 解法:乘 ...
- 【HackerRank】Median
题目链接:Median 做了整整一天T_T 尝试了各种方法: 首先看了解答,可以用multiset,但是发现java不支持: 然后想起来用堆,这个基本思想其实很巧妙的,就是维护一个最大堆和最小堆,最大 ...
- 【HackerRank】Coin on the Table
题目链接:Coin on the Table 一开始想用DFS做的,做了好久都超时. 看了题解才明白要用动态规划. 设置一个三维数组dp,其中dp[i][j][k]表示在时间k到达(i,j)所需要做的 ...
- 【HackerRank】Pairs
题目链接:Pairs 完全就是Two Sum问题的变形!Two Sum问题是要求数组中和正好等于K的两个数,这个是求数组中两个数的差正好等于K的两个数.总结其实就是“骑驴找马”的问题:即当前遍历ar[ ...
随机推荐
- Linux 进程创建二(execve和wait)
三:execve系统调用 int execve(const char *filename, char *const argv[],char *const envp[]); fork创建了一个新的进程, ...
- hive0.13.1安装-mysql server作为hive的metastore
hive0.13.1在hadoop2.4.1伪分布式部署上安装过程 环境:redhat enterprice 6.5 +hadoop2.4.1+hive0.13.1+mysql单节点伪分布式部署 相关 ...
- IOS 关于取消延迟执行函数的种种。performSelector与cancelPreviousPerformRequestsWithTarget
本文非本人撰写 @interface NSObject (NSDelayedPerforming) - (void)performSelector:(SEL)aSelector withObj ...
- 关于spring MVC中加载多个validator的方法。
首先讲下什么叫做validator: validator是验证器,可以验证后台接受的数据,对数据做校验. SpringMVC服务器验证有两种方式,一种是基于Validator接口,一种是使用Annot ...
- 【BZOJ1930】[Shoi2003]pacman 吃豆豆 最大费用最大流
[BZOJ1930][Shoi2003]pacman 吃豆豆 Description 两个PACMAN吃豆豆.一开始的时候,PACMAN都在坐标原点的左下方,豆豆都在右上方.PACMAN走到豆豆处就会 ...
- 160824、ionic添加地图站点
1.基本的地图显示 <!DOCTYPE html> <html ng-app="myApp"> <head> <meta charset= ...
- &#x编码转换成汉字
import java.io.UnsupportedEncodingException; public class UnicodeDecoder { public static String esca ...
- 巨蟒python全栈开发django13:中间件部分
1.回顾昨日内容 2.session认证装饰器 3.django整个流程 4.中间件简单应用 5.简单统计访问次数 6.中间件其他方法 7.orm单表内容回顾
- coursera 《现代操作系统》 -- 第九周 文件系统(1)
文件的定义 文件名-> 路径名 磁盘结构 磁臂转还是那根柱子转? 盘片被轴带动旋转,磁头读取经过的扇区的数据. 典型的磁盘结构 一块盘呢由若干个盘片组成 每个盘片呢实际上有两个面啊,两个盘面 每 ...
- webpack安装和简单配置
1.webpack是一个基于node的项目,所以先装好node和npm 参考我的随笔:https://www.cnblogs.com/jtnote/p/6230384.html 2.先创建 ...