Hackerrank - Game Of Rotation 题解
旋转一个数组以得到最大值。
陷阱就是:不能排序。须要模拟操作旋转,并设计公式计算旋转后的和。
要求是O(n)时间完毕。
原题:
https://www.hackerrank.com/challenges/game-of-rotation
#pragma once
#include <stdio.h> class GameOfRotation
{
public:
GameOfRotation()
{
int N = 0;
scanf("%d", &N);
int *A = new int[N];
long long one = 0, sum = 0, ans = 0;
for (int i = 0; i < N; i++)
{
scanf("%d", &A[i]);
one += (long long)A[i];
sum += (long long)(i+1) * (long long)A[i];
}
ans = sum;
for (int i = 1; i < N; i++)
{
sum = sum - one + (long long)A[i-1] * (long long)N;
ans = ans < sum ? sum : ans;
}
printf("%lld", ans);
delete [] A;
}
}; int gameOfRotation()
{
GameOfRotation();
return 0;
}
Hackerrank - Game Of Rotation 题解的更多相关文章
- Hackerrank - [Algo] Matrix Rotation
https://www.hackerrank.com/challenges/matrix-rotation-algo 又是一道耗了两小时以上的题,做完了才想起来,这不就是几年前在POJ上做过的一个同类 ...
- 【HackerRank】Game Of Rotation
题目连接:Game Of Rotation Mark is an undergraduate student and he is interested in rotation. A conveyor ...
- POJ2286:The Rotation Game——题解
http://poj.org/problem?id=2286 题目大意:如图所示有一种玩具,每次可以拉动A-H的开关使得整个行(或列)向字母方向移动一位(如果移动到头的话则到行(列)尾部) 求使得中间 ...
- The Rotation Game (POJ 2286) 题解
[问题描述] (由于是英文的,看不懂,这里就把大意给大家说一下吧……都是中国人,相信大家也不愿意看英文……) 如图,一个井字形的棋盘,中间有着1-3任意的数,有ABCDEFGH八个操作,每个操作意味着 ...
- 【题解】【数组】【查找】【Leetcode】Search in Rotated Sorted Array
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
- HackerRank "Self Balancing Tree"
Something to learn: Rotation ops in AVL tree does not require recursion. https://github.com/andreima ...
- UVA1434-The Rotation Game(迭代加深搜索)
Problem UVA1434-The Rotation Game Accept:2209 Submit:203 Time Limit: 3000 mSec Problem Description ...
- 算法(第四版)C# 习题题解——1.2
写在前面 整个项目都托管在了 Github 上:https://github.com/ikesnowy/Algorithms-4th-Edition-in-Csharp 这一节内容可能会用到的库文件有 ...
- leetcode & lintcode 题解
刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...
随机推荐
- Verilog的IDE Quartus II
Quartus II 主要用于Verilog的开发,他是开发FPGA的利器,但他需要和modelsim相互配合,才能实现它的编写和仿真.modelsim是第三方的EDA,需要另外安装,对于Quart ...
- Web应用程序指纹识别工具BlindElephant
Web应用程序指纹识别工具BlindElephant BlindElephant是一款Web应用程序指纹识别工具.该工具可以读取目标网站的特定静态文件,计算其对应的哈希值,然后和预先计算出的哈希值 ...
- 【pb_ds】【平衡树启发式合并】【并查集】bzoj2733 [HNOI2012]永无乡
用并查集维护联通性.对每个联通块维护一个平衡树.合并时启发式合并.比较懒,用了pb_ds. #include<cstdio> #include<ext/pb_ds/assoc_con ...
- 【网络流】【Dinic】【Next Array】Dinic模板
注意:有时加边不一定要加反向弧. Next Array版. #include<cstdio> #include<cstring> #include<algorithm&g ...
- 杂谈PID控制算法——第二篇:调·三个量
上面一篇文章讲了一下PID算法中的三个常量大致的在PID算法中起的一个作用,但在实际的使用中,究竟应该如何调节(或者用更加专业的话说是整定)PID控制算法的三个.首先可以将KP,KI,KD三个常量全部 ...
- hadoop FileSplit
/** A section of an input file. Returned by {@link * InputFormat#getSplits(JobContext)} and passed t ...
- STM3的Uart中断接受数据和非中断接受数据!
//非中断方式接受数据if(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == SET) //接收数据寄存器非空标志位{ str = USART_Recei ...
- BigDecimal类整除报错的解决方案
例如: BigDecimal num1 = new BigDecimal("10"); BigDecimal num2 = new BigDecimal("3" ...
- postgres访问认证配置文件pg_hba.conf
pg_hba.conf(默认位于/var/lib/pgsql/10/data/pg_hba.conf)是设置访问认证的主要文件,格式为每条记录一行,每行指定一条访问认证. 设定一条访问认证包含了5个部 ...
- 腾讯云linux服务器分区方案
刚刚在腾讯云买了一台服务器,刚买的服务器的数据盘都是需要自己来分区的,下面就记录一下操作. 通过命令fdisk-l查看硬盘信息 可以看到有两块硬盘/dev/vda和/dev/vdb,启动vda是系统盘 ...