Rotate It !!(思维)
Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Description
- 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
1 5 1 5 3 2 4
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 !!(思维)的更多相关文章
- 【做题】agc006e - Rotate 3x3——分析&思维
原文链接 https://www.cnblogs.com/cly-none/p/9800105.html 题意:给出一个三行\(n\)列的矩阵.问它能否由满足\(a_{ij} = 3(j-1) + i ...
- 计算机程序的思维逻辑 (53) - 剖析Collections - 算法
之前几节介绍了各种具体容器类和抽象容器类,上节我们提到,Java中有一个类Collections,提供了很多针对容器接口的通用功能,这些功能都是以静态方法的方式提供的. 都有哪些功能呢?大概可以分为两 ...
- 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 ...
- 旋转链表(所有元素往右移) rotate list
[抄题]: 给定一个链表,旋转链表,使得每个节点向右移动k个位置,其中k是一个非负数 样例 给出链表1->2->3->4->5->null和k=2 返回4->5-& ...
- 旋转图像 · Rotate Image
[抄题]: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockw ...
- 189. Rotate Array 从右边开始翻转数组
[抄题]: Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the ...
- 洛谷3721 HNOI2017单旋(LCT+set+思维)
这题难道不是spaly裸题吗? 言归正传QWQ 一看到这个题目,其实第一反应是很懵X的 从来没有见过类似的题目啊,什么\(spaly\),单旋.QWQ很懵逼啊 不过,我们可以注意到这么一件事情,就是我 ...
- Canvas绘图之平移translate、旋转rotate、缩放scale
画布操作介绍 画布绘图的环境通过translate(),scale(),rotate(), setTransform()和transform()来改变,它们会对画布的变换矩阵产生影响. 函数 方法 描 ...
- [C#][算法] 用菜鸟的思维学习算法 -- 马桶排序、冒泡排序和快速排序
用菜鸟的思维学习算法 -- 马桶排序.冒泡排序和快速排序 [博主]反骨仔 [来源]http://www.cnblogs.com/liqingwen/p/4994261.html 目录 马桶排序(令人 ...
随机推荐
- HTML精确定位:scrollLeft,scrollWidth,clientWidth,offsetWidth之全然具体解释
HTML:scrollLeft,scrollWidth,clientWidth,offsetWidth究竟指的哪到哪的距离之全然具体解释scrollHeight: 获取对象的滚动高度. scrol ...
- android设备之间屏幕共享
近期公司在开发一款android的设备把屏幕投射到手机上.同一时候手机还能够触控.键盘操作.这样.就达到了屏幕共享的目的. 思考了一下.主要思路: 1.将截图所获取的位图用ffmpeg编码成视频流. ...
- Android Notification使用及取消
//发送通知 NotificationManager manger = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE ...
- samba服务器详细配置(非域模式)
组成Samba运行的有两个服务,一个是SMB,另一个是NMB:SMB是Samba 的核心启动服务,主要负责建立Samba服务器与Samba客户机之间的对话,验证用户身份并提供对文件和打印系统的访问,只 ...
- DEV GridControl 导出到Excel
SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.Title = "导出Excel"; sa ...
- 使用Dataset
string sqlStr="Select * from Tb_news"; SqlDataAdapter myDa=new SqlDataAdapter(SqlStr,myCon ...
- struts 标签引用出错
几句句话概括 1.检查 web.xml 出现错误自己改 配置后filter jsp-config 2.检查 tld 目录下的东西 是否缺少 3. 将包复制到web的lib目录下后 之后 b ...
- ul li a active jquery.cookie.js
div class="righter nav-navicon" id="admin-nav"> <div class="mainer&qu ...
- Windows单击右键没有共享选项怎么办
文件共享是指在网络环境下文件.文件夹.某个硬盘分区使用时的一种设置属性,一般指多个用户可以同时打开或使用同一个文件或数据.但有时候也会遇到找不到共享选项的情况. Windows单击右键没有共享选项怎么 ...
- [转]shell中 source命令即点空格后面再跟可执行文件的说明
这里记录的是在一个shell脚本里面使用. ./file.sh 和./file.sh 的区别,本文参考了http://www.lslnet.com/linux/dosc1/39/linux-28353 ...