codeforces1462D

题意:

给出一个由n个数组成的数组,现在你可以对这个数组进行如下操作:将数组中的一个元素加到这个元素的两边中的一边,然后将这个元素删掉。若该元素在最左边,那么该元素不能加到左边,因为它的左边已经没有元素了,同理最右边。现在问你最少几次这样的操作可以让整个数组的每个元素都相等。

思路:

由于该题目的数据范围并不是很大,只有\(1e3\),所以可以用\(n^2\)的做法得出结果:求出这个数组的前缀和,我们枚举前\(n\)个数作为最终数组每个元素的大小,当找到第一个符合的前缀和,也就是可以通过划分这个数组,让这个数组的每个部分和都是当前枚举的前缀和的时候,可以保证其是最优解,当前的操作数就是答案。由于最差情况也可以通过全部合并得出,所以不存在没有结果的情况。

ac代码:

#include <cstdio>
#include <cstring> const int maxn = 3e3 + 5; int a[maxn], pre[maxn]; int main () {
int T, n;
scanf ("%d", &T);
while (T--) {
scanf ("%d", &n);
for (int i = 0; i < n; ++i) {
scanf ("%d", &a[i]);
}
memset (pre, 0, sizeof pre);
pre[0] = a[0];
for (int i = 1; i < n; i++) {
pre[i] = pre[i - 1] + a[i];
}
int ans = 0;
for (int i = 0; i < n; i++) {
int t = 0;
bool flag = false;
for (int j = 0; j < n; j++) {
t = t + a[j];
ans++;
if (t == pre[i]) {
t = 0;
ans--;
if (j == n - 1) flag = true;
} else if (t > pre[i]) break;
}
if (flag) break;
else ans = 0;
}
printf ("%d\n", ans);
}
return 0;
}

CF1462-D. Add to Neighbour and Remove的更多相关文章

  1. 【总文档】rac增加新节点的方法步骤 How to Add Node/Instance or Remove Node/Instance in 10gR2, 11gR1, 11gR2 and 12c Oracle Clusterware and RAC

    [总文档]How to Add Node/Instance or Remove Node/Instance in 10gR2, 11gR1, 11gR2 and 12c Oracle Clusterw ...

  2. Python可迭代对象中的添加和删除(add,append,pop,remove,insert)

    list: classmates = ['Michael', 'Bob', 'Tracy'] classmates.append('Adam') //添加在末尾,没有add()方法 classmate ...

  3. Codeforces Round #690 (Div. 3)

    第一次 ak cf 的正式比赛,不正式的是寒假里 div4 的 Testing Round,好啦好啦不要问我为什么没有 ak div4 了,差一题差一题 =.= 不知不觉已经咕了一个月了2333. 比 ...

  4. Arrays.asList 为什么不能 add 或者 remove 而 ArrayList 可以

    分析如下例子: 1 import java.util.Arrays; 2 import java.util.List; 3 4 5 public class Test { 6 public stati ...

  5. AutoIt with XML: Add a child/grandchild node or remove any node

    Sometimes, we have to use AutoIt script to edit an xml, add a node or remove a node, to make some de ...

  6. Queue接口分析:add和offer区别,remove和poll方法到底啥区别

    Queue接口: public interface Queue<E> extends Collection<E> { /* * add方法,在不违背队列的容量限制的情况,往队列 ...

  7. 当调用List Remove 失效时 [C#] .

    有没有试过从一个集合里面移除一个对象之后,这个集合仍然留有这个对象?世界之大,无奇不有.稍有疏忽,便会导致这种奇怪的现象.现在让我们看看这个“不死”对象究竟是怎么一回事. 1.“不死”对象现身 这个问 ...

  8. 遍历并remove HashMap中的元素时,遇到ConcurrentModificationException

    遍历并remove HashMap中的元素时,遇到ConcurrentModificationException for (Map.Entry<ImageView, UserConcise> ...

  9. ArrayList Iterator remove java.lang.UnsupportedOperationException

    在使用Arrays.asList()后调用add,remove这些method时出现 java.lang.UnsupportedOperationException异常.这是由于Arrays.asLi ...

随机推荐

  1. rm: cannot remove `/tmp/localhost-mysql_cacti_stats.txt': Operation not permitted

    [root@DBslave tmp]# chown zabbix.zabbix /tmp/localhost-mysql_cacti_stats.txt

  2. Azure App object和Service Principal

    为了把Identity(身份)和Access Management function(访问管理功能)委派给Azure AD,必须向Azure AD tenant注册应用程序.使用Azure AD注册应 ...

  3. 【ORA】ora-39700解决

  4. leetcode 470. 用 Rand7() 实现 Rand10() (数学,优化策略)

    题目链接 https://leetcode-cn.com/problems/implement-rand10-using-rand7/ 题意: 给定一个rand7()的生成器,求解如何产生一个rand ...

  5. ctfshow—web—web5

    打开靶机,代码审计 附上代码 <?php error_reporting(0); ?> <html lang="zh-CN"> <head> & ...

  6. SAP表的锁定与解锁

    表的锁定模式有三种模式. lock mode有三种模式:分别是S,E,X.含义如下:     S (Shared lock, read lock)     E (Exclusive lock, wri ...

  7. SVM 支持向量机算法-实战篇

    公号:码农充电站pro 主页:https://codeshellme.github.io 上一篇介绍了 SVM 的原理和一些基本概念,本篇来介绍如何用 SVM 处理实际问题. 1,SVM 的实现 SV ...

  8. 2.4V升5V芯片,8uA功耗,低功耗升压电路图

    2.4V升5V,可用于USB拔插充电,也可以用于把两节镍氢电池2.4V升压到5V,的固定输出稳压电压值,同时输出电流可达1A,0.5A等 首先是先说下0.5A的这款的话,是比较低功耗的,8uA左右的输 ...

  9. three.js cannon.js物理引擎地形生成器和使用指针锁定控件

    今天郭先生说一说使用cannon.js物理引擎绘制地形和使用指针锁定控件.效果如下图.线案例请点击博客原文. 这里面的生成地形的插件和指针锁定控件也是cannon.js的作者schteppe封装的,当 ...

  10. 太极图HTML+CSS(可旋转)代码记录

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...