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,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...
随机推荐
- 29、Django实战第29天:修改密码和头像
修改头像 1.上传头像,我们需要的对它做一个forms验证,编辑users.forms.py ... from .models import UserProfile class UploadImage ...
- small test on 5.29 night T1
可以发现题目的重点是在第一个部分,因为只要信心值我们求出来了,那么第二问就是一个简单的最长上升子序列问题了,所以接下来只讲第一问. #include<iostream> #include& ...
- 【构造】Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) D. Artsem and Saunders
根据那两个式子 g(h(x))=x h(g(x))=f(x) 可以推出来两个新的式子 g(f(x))=g(x) h(x)=f(h(x)) 于是,我们先找到f(x)的所有不动点,有几个不动点,m就是多少 ...
- 显示图案 Exercise06_06
import java.util.Scanner; /** * @author 冰樱梦 * 时间:2018年下半年 * 题目:显示图案 * 输入一个数 5 1 2 1 3 2 1 4 3 2 1 5 ...
- Java高级架构师(一)第26节:测试并调整登录的业务功能
主Index的处理Java: package com.sishuok.architecture1; import org.springframework.beans.factory.annotatio ...
- UWP 程序抛出异常时总是跳到“global::System.Diagnostics.Debugger.Break();”的解决办法
调试 C# 程序时,如果遇到异常,VS 会中断,指出导致异常的语句.但是最近调试 UWP 程序时,发现总是在“global::System.Diagnostics.Debugger.Break();” ...
- pythonGUI编程打开默认浏览器
代码: from tkinter import * import webbrowser root = Tk() text = Text(root,width=30,height = 5) text.p ...
- Linux批量管理工具Ansible
Ansible-批量linux管理工具:https://github.com/ansible/ansible Ansible有如下优点: 1.轻量级,他不需要去客户端安装agent,更新时,只需要在操 ...
- 专访阿里巴巴研究员“赵海平”:Facebook的PHP底层性能优化之路(HipHop,HHVM)
专访阿里巴巴研究员“赵海平”:Facebook的PHP底层性能优化之路 http://www.infoq.com/cn/articles/interview-alibaba-zhaohaiping
- display:flex;多行多列布局学习
从以前的table布局到现在的div布局,再到未来的flex布局,CSS重构方面对展示行和适应性的要求越来越高: 首先来比较一下布局方式的更新意义: table布局: 优点:1.兼容性好,ie6.ie ...