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,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...
随机推荐
- HDU 5938 Four Operations 【字符串处理,枚举,把数字字符串变为数值】
Problem Description Little Ruins is a studious boy, recently he learned the four operations! Now he ...
- Maven学习笔记2
Maven安装配置 1.下载Maven 官方地址:http://maven.apache.org/download.cgi 将下载的压缩包解压到你要安装 Maven 的文件夹.假设你解压缩到文件夹 – ...
- Coderforces 85 D. Sum of Medians(线段树单点修改)
D. Sum of Medians time limit per test 3 seconds memory limit per test 256 megabytes input standard i ...
- 【枚举】【前缀和】【map】ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) C. Molly's Chemicals
处理出前缀和,枚举k的幂,然后从前往后枚举,把前面的前缀和都塞进map,可以方便的查询对于某个右端点,有多少个左端点满足该段区间的和为待查询的值. #include<cstdio> #in ...
- 【kd-tree】bzoj1176 [Balkan2007]Mokia
裸题不多说,注意在sqrt(n*log(n))次插入后重构树以保持深度. #include<cstdio> #include<cmath> #include<algori ...
- Cmder | 一款命令行增强工具
文章目录 什么是cmder 安装cmder 让cmder便于使用 将cmder添加到右键菜单中 在设置中添加语言环境 设置默认使用cmd.PowerShell还是bash 调节背景的透明度 添加 ll ...
- 51单片机软件I2C驱动中的CY
做一个MSP430的项目,虽然430内部有硬件I2C的模块,略难,准备直接移植51的..碰到一句代码 dat <<= 1; //移出数据的最高位 pSDA = CY; //送数据口 dig ...
- Android Studio 下使用git -- 个人,本地版本控制
第一步:下载安装git 下载地址 : https://git-scm.com/downloads 第二步:Android Studio 下配置git路径. 配置之后,Test弹出如下成功的提示即可. ...
- U盘安装Ubuntu 16.04出现:Failed to load ldlinux.c32
启动的时候如果不开启UEFI,则会提示: Failed to load ldlinux.c32 Boot failed: please change disks and press a key to ...
- MySQL Workbench 工具
http://www.cnblogs.com/mr-wid/archive/2013/05/09/3068229.html http://www.cnblogs.com/daimage/archive ...