题目链接:http://codeforces.com/gym/100889/problem/B

B. Backward and Forward

time limit per test

2.0 s

memory limit per test

256 MB

input

standard input

output

standard output

An array is called a 'Mirror' if it reads the same backward and forward. For example, [23, 15, 23] is a 'Mirror' but [2, 0, 1] is not.

You are given an array A of size N. Your task is to make a given array a 'Mirror'. The only allowed operation is that you can merge two adjacent elements in the array and replace them with their sum.

Find minimum number of operations required to make given array a 'Mirror' such that it reads the same backward and forward.

Input

First line contains T(1 ≤ T ≤ 20), the number of test cases. Each test case consist of 2 lines. First line contains number N(1 ≤ N ≤ 105), size of the array. Next line contains N space separated integers Ai(0 ≤ Ai ≤ 109) denoting elements in the array A.

Output

For each test case output in one line minimum steps required to make given array a 'Mirror'.

Example

input

Copy

231 0 1510 20 20 10 40

output

Copy

03

Note

Sample test case 1:

Given array is already a 'Mirror' . So, no need to perform any operation.

Sample test case 2:

One possible sequence of operations.

Step 1 : Merge 2nd and 3rd element to get array [10, 40, 10, 40].

Step 2 : Merge 1st and 2nd element to get array [50, 10, 40].

Step 3 : Merge 2nd and 3rd element to get array [50, 50].

这道题有两点需要注意:

①定义两个数组下标变量i,j,分别从数组最左边和最右边向中间遍历,若a[i]<a[j],则令i=i+1;a[i]=a[i]+a[i-1],若a[i]>a[j],则令j=j-1,a[j]=a[j]+a[j+1], 并且终止条件是i>j而不是i>=n/2

②由于a[i]值上限是10^9,所以定义数组a[i]为长整型就不会wa;

代码如下:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
typedef long long ll;
#define maxn 100010
using namespace std;
int main(){ll t,n,sum;ll a[maxn];cin>>t;while(t--){sum=0;cin>>n;for(ll i=0;i<n;i++){cin>>a[i];}for(ll i=0,j=n-1;i<=j;){if(a[i]==a[j]){i++;j--;}else if(a[i]<a[j]){i++;a[i]=a[i]+a[i-1];sum++;}else if(a[i]>a[j]){j--;a[j]=a[j]+a[j+1];sum++;} }printf("%lld\n",sum);}return 0;
}

转载于:https://www.cnblogs.com/jianqiao123/p/11311831.html

Gym-100889B Backward and Forward相关推荐

  1. Backward Elimination, Forward Selection and Stepwise

    Backward Elimination,Forward Selection和Stepwise这三种是特征选择中经常用到的方法.当有时候特征的数量太多的时候,我们除了可以用PCA等方法降维之外,还可以 ...

  2. caffe学习笔记2:net forward与backward

    caffe学习笔记2:Forward and Backward 原网页:http://caffe.berkeleyvision.org/tutorial/forward_backward.html f ...

  3. 2020年 ICLR 国际会议最终接受论文(poster-paper)列表(一)

    来源:AINLPer微信公众号(点击了解一下吧) 编辑: ShuYini 校稿: ShuYini 时间: 2020-01-22     2020年的ICLR会议将于今年的4月26日-4月30日在Mil ...

  4. SDUT 2022 Summer Individual Contest - 12(for 21)

    ------水赛总结 A - Window Gym - 101020A Jerry Smith is Rick's Son-in-Law and Morty's father. He recently ...

  5. pytorch学习笔记(九):PyTorch结构介绍

    PyTorch结构介绍 对PyTorch架构的粗浅理解,不能保证完全正确,但是希望可以从更高层次上对PyTorch上有个整体把握.水平有限,如有错误,欢迎指错,谢谢! 几个重要的类型 和数值相关的 T ...

  6. 机器学习入门(11)— 反向传播的加法节点、乘法节点、加法层代码实现、乘法层代码实现

    1. 加法节点 以 z = x + y 为对象,观察它的反向传播.z = x + y 的导数可由下式(解析性地)计算出来. 计算图如图 5-9 中,反向传播将从上游传过来的导数(本例中是 ∂L∂z\f ...

  7. vim 成“神“之路 (一)

    文章目录 1. 安装 1.1 linux 1.2 MacOs的安装 1.3 Windows的安装 1.4 vim中文帮助文档安装 2. vim基本概念和基础命令 2.1 基本的键位映射如下: 2.2 ...

  8. LeetCode实战:回文数

    题目英文 Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same ...

  9. Determine whether an integer is a palindrome. Do this without extra space.

    看到这个题目的时候,首先不认识 Determine这个单词.英文不好没办法,查了下是确认的意思,然后不懂 palindrome这个单词, 查了下是回文的意思. 问题是 回文是个什么东西,官方解释: A ...

最新文章

  1. CentOS7安装配置VSFTP
  2. ViT(vision transformer)原理快速入门
  3. Storm Trident示例function, filter, projection
  4. 一篇文章搞懂JavaScript运行机制
  5. Developer Express 之 XtraReport如何显示设计窗体,打开已设计过的报表
  6. Cookie与Web Storage的区别
  7. getbean方法找不到bean_?找不到产品卖点?你需要这些方法!
  8. 经典面试题(39):以下代码将输出的结果是什么?
  9. 切换python执行版本
  10. php自定义函数表格,自定义函数table()
  11. 测试环境下将centos6.8升级到centos7的操作记录(转)
  12. JVM系列(十三)——垃圾回收器
  13. 访问ip不在白名单中,请参考FAQ:
  14. 从0开始学习 GitHub 系列之「01.初识 GitHub」----转载自stormzhang 原创文章
  15. 股神问题 - 有股神吗? 有, 小赛就是!
  16. 匿名科创无人机学习心得
  17. Blktrace原理简介及使用
  18. iOS获取ipa及解压Assets.car,兼容M1
  19. 【狂神说JAVA】Java注解note
  20. Object of type type is not JSON serializable

热门文章

  1. C#实现字符串左旋转操作
  2. python的安装教程-python安装教程
  3. python3.6安装scrapy-win7安装python3.6.1及scrapy
  4. python解释器的安装步骤-Python本地及虚拟解释器配置过程解析
  5. python编程课-python编程课---turtle
  6. 编程中python怎么读-编程语言如何在Python中读写文件
  7. python使用lxml及request爬取-python用lxml解析网页为什么不完整?
  8. python输出csv文件-Python之读取与写入CSV文件
  9. 零基础学python视频百度云-零基础入门学习Python 小甲鱼视频教程
  10. 初学者python用哪个版本好-什么是Python?初学者应该学python哪个版本?