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,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...
随机推荐
- codeforces 713C C. Sonya and Problem Wihtout a Legend(dp)(将一个数组变成严格单增数组的最少步骤)
E. Sonya and Problem Wihtout a Legend time limit per test 5 seconds memory limit per test 256 megaby ...
- circusctl命令在ubuntu 上执行,卡住的现象处理。
1. circus介绍 circus是一个进程管理工具,类似于supervisod. 2. circusctl是circusd进程的管理工具 3. circus的安装 pip3 install cir ...
- P3197越狱
花费了好长时间,终于刷掉了这道题. 题目在这里(洛谷) (信息学奥赛一本通) 嗯,没错,这是一道快速幂的题,不会快速幂点这里 好现在开始分析,这道题用小学奥数的思想就可以想到,直接算有多少种可能比较 ...
- MongoError: topology was destroyed解决方法
MongoError: topology was destroyed 分析得出,出现这个问题是因为,当mongodb尝试写入某个数据的时候,连接被中断了! 解决方法:检查代码中是否存在操作数据的过程中 ...
- 【PKUSC 2015】的一道数学题
有9个人,每三个人中至少有两个互相认识,求证这里面至少有4个人互相认识 PKU官方题解: 引理:二染色K6中一定有同色K3. 证明:考虑某一个点,它一定连出至少三条同色边(不妨设为红边),这三条边连的 ...
- POJ 3537 Crosses and Crosses (NEERC)
Crosses and Crosses Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 4 ...
- HDOJ 4903 The only survival
Discription: There is an old country and the king fell in love with a devil. The devil always ask th ...
- [SPOJ]COT2
题意:给一棵带点权的树,多次询问两点间路径上的不同权值数 学习了一下莫队上树(雾 先求出栈入栈序$p_{1\cdots 2n}$,记$st_x$为$x$在$p$中第一次出现的位置,$ed_x$为$x$ ...
- 【莫队算法】【权值分块】bzoj3585 mex
orz PoPoQQQ. 本来蒟蒻以为这种离散化以后就对应不起来的题不能权值分块搞的说. ……结果,实际上>n的权值不会对答案作出贡献. #include<cstdio> #incl ...
- 什么是EPEL 及 Centos上安装EPEL(转)
什么是EPEL 及 Centos上安装EPEL 转自:http://www.unxmail.com/read.php?67 RHEL以及他的衍生发行版如CentOS.Scientific Linux为 ...