Rotate It !!

Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Description

standard input/output  Announcement

 
  • Statements

    Dzy and Fox have a sequence A consisting of N numbers [A1...AN]. Dzy starts by taking the first number, then Fox takes the second number, then Dzy takes the third number and so on, they continue taking turns until all of the N numbers are taken. The player with the highest sum of numbers wins.

    Since Dzy is your dear friend, you decided to rotate the sequence (you may rotate it as many times as you like) in order to maximize Dzy's sum of numbers.

    Rotation is defined as removing the first element from the beginning of the sequence and adding it to the end of the sequence.

    So given the sequence A , you have to help Dzy and let him achieve the maximum possible sum.

Input

The first line containts a single integer T, the number of test cases.

Then T testcases are given as follows :

The first line of each testcase contains a single integer N (1 ≤ n ≤ 104).

The second line of each testcase contains N space-separated integers [A1...AN],the elements of the sequence A (1 ≤ i ≤ n) ( - 109 ≤ Ai ≤ 109).

Output

Output T lines , The answer for each testcase which is the maximum achievable sum by Dzy if you help him.

Sample Input

Input
1 5 1 5 3 2 4
Output
12

Hint

Consider all 5 rotations of the sequence:

1 5 3 2 4 (Dzy score = 1 + 3 + 4 = 8)

5 3 2 4 1 (Dzy score = 8)

3 2 4 1 5 (Dzy score = 12)

2 4 1 5 3 (Dzy score = 6)

4 1 5 3 2 (Dzy score = 11)

题解:一个环状数据,Dzy从第一个开始取,每隔一个取一个,取的数最大是多少,注意若数为奇数,Dzy取的数一定最多;

想法是先找到总数,然后枚举每个位置为最后一个位置;

见代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int MAXN = 1e4 + ;
int num[MAXN];
typedef long long LL;
int main(){
int T, N;
scanf("%d", &T);
while(T--){
scanf("%d", &N);
LL sum = , cur;
if(N == ){
scanf("%lld", &cur);
printf("%lld\n", cur);
continue;
}
for(int i = ; i < N; i++){
scanf("%d", num + i);
sum += num[i];
}
if(N % == ){
cur = ;
for(int i = ; i < N; i+= )
cur += num[i];
printf("%lld\n", max(cur, sum - cur));
}
else{
LL sum1 = , sum2 = , cur1 = , cur2 = ;
for(int i = ; i < N; i++){
if(i % == )
sum1 += num[i];
else
sum2 += num[i];
}
LL ans = sum1;
for(int i = ; i < N; i++){
if(i % == )
cur1 += num[i];
else
cur2 += num[i];
if(i % == ){
ans = max(ans, sum2 - cur2 + cur1);
// printf("%lld\n", sum2 - cur2 + cur1);
}
else{
ans = max(ans, sum1 - cur1 + cur2);
// printf("%lld\n", sum1 - cur1 + cur2);
} }
printf("%lld\n", ans);
}
}
return ;
}

Rotate It !!(思维)的更多相关文章

  1. 【做题】agc006e - Rotate 3x3——分析&思维

    原文链接 https://www.cnblogs.com/cly-none/p/9800105.html 题意:给出一个三行\(n\)列的矩阵.问它能否由满足\(a_{ij} = 3(j-1) + i ...

  2. 计算机程序的思维逻辑 (53) - 剖析Collections - 算法

    之前几节介绍了各种具体容器类和抽象容器类,上节我们提到,Java中有一个类Collections,提供了很多针对容器接口的通用功能,这些功能都是以静态方法的方式提供的. 都有哪些功能呢?大概可以分为两 ...

  3. Rotate Array leetcode

    Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array  ...

  4. 旋转链表(所有元素往右移) rotate list

    [抄题]: 给定一个链表,旋转链表,使得每个节点向右移动k个位置,其中k是一个非负数 样例 给出链表1->2->3->4->5->null和k=2 返回4->5-& ...

  5. 旋转图像 · Rotate Image

    [抄题]: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockw ...

  6. 189. Rotate Array 从右边开始翻转数组

    [抄题]: Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the ...

  7. 洛谷3721 HNOI2017单旋(LCT+set+思维)

    这题难道不是spaly裸题吗? 言归正传QWQ 一看到这个题目,其实第一反应是很懵X的 从来没有见过类似的题目啊,什么\(spaly\),单旋.QWQ很懵逼啊 不过,我们可以注意到这么一件事情,就是我 ...

  8. Canvas绘图之平移translate、旋转rotate、缩放scale

    画布操作介绍 画布绘图的环境通过translate(),scale(),rotate(), setTransform()和transform()来改变,它们会对画布的变换矩阵产生影响. 函数 方法 描 ...

  9. [C#][算法] 用菜鸟的思维学习算法 -- 马桶排序、冒泡排序和快速排序

    用菜鸟的思维学习算法 -- 马桶排序.冒泡排序和快速排序 [博主]反骨仔 [来源]http://www.cnblogs.com/liqingwen/p/4994261.html  目录 马桶排序(令人 ...

随机推荐

  1. HDU ACM 1078 FatMouse and Cheese 记忆化+DFS

    题意:FatMouse在一个N*N方格上找吃的,每一个点(x,y)有一些吃的,FatMouse从(0,0)的出发去找吃的.每次最多走k步,他走过的位置能够吃掉吃的.保证吃的数量在0-100.规定他仅仅 ...

  2. C++中的对象数组

    类是对象的抽象,我们可以使用一个类来定义很多的对象,然后每个对象都有自己的属性. 当我们使用类来定义很多相同结构的对象的时候,我们可以采取对象数组的方法. 例如,一个班有50个学生,我们定义了一个学生 ...

  3. Android字数限制的EditText实现方案研究

    在应用开发中,有时需要实现有字数限制的EditText,首先来分析下市面上存在的类似实现方案吧,好有个感性的认识. [方案一:腾讯微博] 每个中文字符算一个字数,每两个英文字符算一个字数,当用户输入内 ...

  4. Hacker(21)----密码攻防之加密与解密基础

    密码对于用户而言并不陌生,它是一种用于保护重要信息和文件的工具,只有输入正确的密码才可查看文件和信息的具体内容.黑客为了获取这些信息,会采用各种方式来破解密码,因此用户不仅需要了解黑客破解密码的常用方 ...

  5. this函数的理解

    Javascript的this用法 this是Javascript语言的一个关键字. 它代表函数运行时,自动生成的一个内部对象,只能在函数内部使用.比如, function test(){ this. ...

  6. 安卓状态栏通知Status Bar Notification

    安卓系统通知用户三种方式: 1.Toast Notification 2.Dialog Notification 3.Status Bar Notification Status Bar Notifi ...

  7. shell中的type命令

    type [-aftpP] name [name,...] -a 打印name的所有可能情况,比如type -a ls,会打印ls is aliased to 'ls --color=auto'和ls ...

  8. Android平台之不预览获取照相机预览数据帧及精确时间截

    在android平台上要获取预览数据帧是一件极其容易的事儿,但要获取每帧数据对应的时间截并不那么容易,网络上关于这方面的资料也比较少.之所以要获取时间截,是因为某些情况下需要加入精确时间轴才能解决问题 ...

  9. IIS 7.0 的 ASP.NET 应用程序生命周期概述(转载)

    IIS 7.0 的 ASP.NET 应用程序生命周期概述更新:2007 年 11 月本主题介绍在 IIS 7.0 集成模式下运行以及与 IIS 7.0 或更高版本一起运行的 ASP.NET 应用程序的 ...

  10. FP—Growth算法

    FP_growth算法是韩家炜老师在2000年提出的关联分析算法,该算法和Apriori算法最大的不同有两点: 第一,不产生候选集,第二,只需要两次遍历数据库,大大提高了效率,用31646条测试记录, ...