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 目录 马桶排序(令人 ...
随机推荐
- cobol语言基础培训教程
COBOL 是Common Business Oriented Language 的缩写.它不仅是商业数据处理的理想语言,而且广泛用于数据管理领域,因此COBOL 语言也被称为”用于管理的语言”. 一 ...
- C语言--返回局部变量的地址
我们可能会经常写出这样的代码: int add(int a , int b) { return a + b; } 当然,这是合理的写法,使函数的返回值为 int ,所以,调用函数后会返回一个i ...
- 数据库中的记录通过servlet回显到jsp页面中(连接数据库或者查询參照:对数据进行增删改查)
我们常常会用到通过图书的名称来查询图书那么这种话我们也就会使用到从数据库中搜索出数据而且载入到自己的Jsp页面中 这种话我们须要将从数据库中获取到的数据放进响应中然后通过%=request.getAt ...
- Visual Studio 2008项目中WinForm窗口图标显示为类图标,仅仅能打开代码而无法打开视图问题解决
背景: 今天打开一个Winform项目的时候.图标显示为类文件的样子而不是窗口的样子,在代码中右键也没有View Designer选项.双击图标打开的是代码而非窗口设计界面,百度后也没 ...
- unity3d 学习笔记(一)
操作:按下shit 点击坐标轴中心 切换透视图 动画烘焙的概念:相当于把原来的控制器动画或者IK(骨骼)动画所有塌陷为逐帧动画,导出的时候必须选这一项 着色器:从技术的角度来看,着色器是渲染器的一个部 ...
- javascript之求最值
求最值: var selections = $("#deliveryGridSalesOrGoods").datagrid('getRows'); var costPrice = ...
- DEV SIT UAT
DEV环境:DEV顾名思义就是develop,即代码开发的环境.SIT环境:System Integration Test系统集成测试,开发人员自己测试流程是否走通.UAT环境:User Accept ...
- 第一次用IIS发布网站时遇到的两个问题
1. 配置错误 说明: 在处理向该请求提供服务所需的配置文件时出错.请检查下面的特定错误详细信息并适当地修改配置文件. 分析器错误消息: 无法识别的属性“targetFramework”.请注意属性 ...
- Gradle 编译时选择不同的 google-services.json
在做的安卓应用需要在 debug 和 release build中使用不同的谷歌服务账号,要用到不同的google-serivces.json ,手动替换的话太费时费力,好在万能的gradle可以完成 ...
- Silverlight 设置颜色
透明色:00ff00ff //设置柱状图的颜色 ColorSet cs = new ColorSet(); cs.Id = "co ...