旋转一个数组以得到最大值。

陷阱就是:不能排序。须要模拟操作旋转,并设计公式计算旋转后的和。

要求是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 题解的更多相关文章

  1. Hackerrank - [Algo] Matrix Rotation

    https://www.hackerrank.com/challenges/matrix-rotation-algo 又是一道耗了两小时以上的题,做完了才想起来,这不就是几年前在POJ上做过的一个同类 ...

  2. 【HackerRank】Game Of Rotation

    题目连接:Game Of Rotation Mark is an undergraduate student and he is interested in rotation. A conveyor ...

  3. POJ2286:The Rotation Game——题解

    http://poj.org/problem?id=2286 题目大意:如图所示有一种玩具,每次可以拉动A-H的开关使得整个行(或列)向字母方向移动一位(如果移动到头的话则到行(列)尾部) 求使得中间 ...

  4. The Rotation Game (POJ 2286) 题解

    [问题描述] (由于是英文的,看不懂,这里就把大意给大家说一下吧……都是中国人,相信大家也不愿意看英文……) 如图,一个井字形的棋盘,中间有着1-3任意的数,有ABCDEFGH八个操作,每个操作意味着 ...

  5. 【题解】【数组】【查找】【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 ...

  6. HackerRank "Self Balancing Tree"

    Something to learn: Rotation ops in AVL tree does not require recursion. https://github.com/andreima ...

  7. UVA1434-The Rotation Game(迭代加深搜索)

    Problem UVA1434-The Rotation Game Accept:2209  Submit:203 Time Limit: 3000 mSec  Problem Description ...

  8. 算法(第四版)C# 习题题解——1.2

    写在前面 整个项目都托管在了 Github 上:https://github.com/ikesnowy/Algorithms-4th-Edition-in-Csharp 这一节内容可能会用到的库文件有 ...

  9. leetcode & lintcode 题解

    刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...

随机推荐

  1. 详解Oracle数据货场中三种优化:分区、维度和物化视图

    转 xiewmang 新浪博客 本文主要介绍了Oracle数据货场中的三种优化:对分区的优化.维度优化和物化视图的优化,并给出了详细的优化代码,希望对您有所帮助. 我们在做数据库的项目时,对数据货场的 ...

  2. Xamarin XAML语言教程构建ControlTemplate控件模板

    Xamarin XAML语言教程构建ControlTemplate控件模板 控件模板ControlTemplate ControlTemplate是从Xamarin.Forms 2.1.0开始被引入的 ...

  3. Eclipse 教程 | 菜鸟教程

    http://www.runoob.com/eclipse/eclipse-charset.html

  4. [BZOJ 1926] 粟粟的书架

    BZOJ 传送门 Luogu 传送门 BZOJ的sillyB评测机各种无故CE,只好去Luogu上A了o(╯□╰)o Solution: 从数据范围可以发现,这其实是2道题: (1)一个$R*C$的矩 ...

  5. POJ 3532 Resistance(高斯消元+基尔霍夫定理)

    [题目链接] http://poj.org/problem?id=3532 [题目大意] 给出n个点,一些点之间有电阻相连,求1~n的等效电阻 [题解] 有基尔霍夫定理:任何一个点(除起点和终点)发出 ...

  6. 【推导】Codeforces Round #402 (Div. 2) A. Pupils Redistribution

    一次交换,会让Group A里面的某个数字的数量-1,另一个数字的数量+1:对Group B恰好相反. 于是答案就是xigma(i=1~5,numA[i]-numB[i]>0)(numA[i]- ...

  7. 【DFS】Codeforces Round #398 (Div. 2) C. Garland

    设sum是所有灯泡的亮度之和 有两种情况: 一种是存在结点U和V,U是V的祖先,并且U的子树权值和为sum/3*2,且U不是根,且V的子树权值和为sum/3. 另一种是存在结点U和V,他们之间没有祖先 ...

  8. IO流--File--properties

    package com.songyan.properties; /** * properties * 是hashtable的子类具备map集合的特点 * 里面存储的键值对都是String而且不需要指定 ...

  9. inline-block 前世今生(转)

    曾几何时,display:inline-block 已经深入「大街小巷」,随处可见 「display:inline-block; *display:inline; *zoom:1; 」这样的代码.如今 ...

  10. 使用hsdis查看jit生成的汇编代码

     http://blog.csdn.net/unei66/article/details/26477629 JVM 有 HotSpot引擎可以对热代码路径进行有效的 JIT优化,大幅度提升计算密集代码 ...