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. 盘点20款表现出众的HTML5游戏

    不管是对用户还是开发者来说,HTML5和JavaScript游戏这几年的发展真的是件好事.随着浏览器平台的日趋成熟,并开始整合这类型游戏所 要求的技术,我们每天都能在各大应用商店和社交网站中看到越来越 ...

  2. iPhone应用提交流程:如何将App程序发布到App Store?

    对于刚加入iOS应用开发行列的开发者来说,终于经过艰苦的Coding后完成了第一个应用后最重要的历史时刻就是将应用程序提交到iTunes App Store.Xcode 4.2开发工具已经把App提交 ...

  3. windows下批量杀死进程

    有时候因为病毒或其它原因,启动了一系列的进程,而且有时杀了这个,又多了那个.使用命令taskkill可将这些进程一下子所有杀光: C:\Users\NR>taskkill /F /im fron ...

  4. VMware vSphere 5.5的12个更新亮点(2)

    ACPI支持 以前版本的VMware虚拟机的局限性之一,是支持的虚拟设备数量甚少.vSphere 5.5引入了Virtual Hardware 10,这增加了基于SATA的虚拟设备节点,通过AHCI( ...

  5. web请求的处理流程

    web请求的处理流程如下: 1.客户发起请求到服务器网卡:2.服务器网卡接受到请求后转交给内核处理:3.内核根据请求对应的套接字,将请求交给工作在用户空间的Web服务器进程4.Web服务器进程根据用户 ...

  6. java基础之成员变量与局部变量

    成员变量的含义 局部变量的含义 成员变量与局部变量的区别

  7. js数字精度丢失

    http://www.cnblogs.com/snandy/p/4943138.html

  8. oracle 的服务器进程(PMON, SMON,CKPT,DBWn,LGWR,ARCn)

    来着TOM的<oracle 编程艺术 9i,10g,11g> PMON PMON,进程监视.PMON主要有3个用途: 1,在进程非正常中断后,做清理工作.例如:dedicated serv ...

  9. 选中CheckBoxList的值放到TextBox中,再次选中从textBox中删除

    当选中checkboxlist中的值,直接放到文本框中,在checkboxlist的SelectedIndexChanged事件下执行方法, //将选中的值放到文本框中                ...

  10. Qt信号槽写法

    1.标准写法 2.lamda表达式写法 3.地址函数指针写法