题目:

描述

In Chinese mythology, Pangu is the first living being and the creator of the sky and the earth. He woke up from an egg and split the egg into two parts: the sky and the earth.

At the beginning, there was no mountain on the earth, only stones all over the land.

There were N piles of stones, numbered from 1 to N. Pangu wanted to merge all of them into one pile to build a great mountain. If the sum of stones of some piles was S, Pangu would need S seconds to pile them into one pile, and there would be S stones in the new pile.

Unfortunately, every time Pangu could only merge successive piles into one pile. And the number of piles he merged shouldn't be less than L or greater than R.

Pangu wanted to finish this as soon as possible.

Can you help him? If there was no solution, you should answer '0'.

输入

There are multiple test cases.

The first line of each case contains three integers N,L,R as above mentioned (2<=N<=100,2<=L<=R<=N).

The second line of each case contains N integers a1,a2 …aN (1<= ai  <=1000,i= 1…N ), indicating the number of stones of  pile 1, pile 2 …pile N.

The number of test cases is less than 110 and there are at most 5 test cases in which N >= 50.

输出

For each test case, you should output the minimum time(in seconds) Pangu had to take . If it was impossible for Pangu to do his job, you should output  0.

样例输入

3 2 2
1 2 3
3 2 3
1 2 3
4 3 3
1 2 3 4

样例输出

9
6
0
题目分析:

感觉像事DP问题,后来发现也其中部分可以利用DFS的思想;

依题意,越靠前的数组就会被使用越多的次数,所以从开始就要尽可能长的合并石堆

利用DFS的思想对石堆进行合并,并进行标记的次数

那么就要从后向前进行;

同时比较以标记次数和现在进行次数的大小,如果现在的次数要比之前标记的次数要大,就直接return

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;
#define MAX 10000
int N,L,R;
int pile[1010];
int flag[1010];
bool pos = 0;
void DFS(int n,int t,int s) {for (int i=n-t+2;i<=n;++i) if(flag[i]<s)                    return;if(n-t+1<1)                          return;if(n-t+1==1)  {for (int i=1;i<=n;++i)          flag[i]=s;pos = 1;return ;}for (int i=n-t+2;i<=n;++i)         flag[i]=s;for (int i=L;i<=R;++i)                DFS(n-i+1,i,s+1);return ;
}int main()
{while (cin>>N>>L>>R) {pos = 0;memset(flag,MAX,sizeof(flag));for (int i=1;i<=N;++i)           cin>>pile[i];for (int i=L;i<=R;++i)            DFS(N,i,1);if(pos)      {int sum=0;for (int i=1;i<=N;++i)   {sum+=(pile[i]*flag[i]);}cout<<sum<<endl;}else    cout<<"0"<<endl;} return 0;
}

Pangu and Stones 解题报告相关推荐

  1. HDOJ 1896 Stones 解题报告

    题目分类:优先队列+STL 作者:ACShiryu 做题时间:2011-7-18 Stones Time Limit: 5000/3000 MS (Java/Others)    Memory Lim ...

  2. uscao 线段树成段更新操作及Lazy思想(POJ3468解题报告)

    线段树成段更新操作及Lazy思想(POJ3468解题报告) 标签: treequerybuildn2cstruct 2011-11-03 20:37 5756人阅读 评论(0) 收藏 举报  分类: ...

  3. 解题报告(十八)数论题目泛做(Codeforces 难度:2000 ~ 3000 + )

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 繁凡出品的全新系列:解题报告系列 -- 超高质量算法题单,配套我写的超高质量的题解和代码,题目难度不一 ...

  4. 【解题报告系列】超高质量题单 + 题解(ACM / OI)超高质量题解

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 繁凡出品的全新系列:解题报告系列 -- 超高质量算法题单,配套我新写的超高质量的题解和代码,题目难度不 ...

  5. 解题报告(三)多项式求值与插值(拉格朗日插值)(ACM / OI)

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 繁凡出品的全新系列:解题报告系列 -- 超高质量算法题单,配套我写的超高质量的题解和代码,题目难度不一 ...

  6. 解题报告(十三)中国剩余定理(ACM / OI)

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 繁凡出品的全新系列:解题报告系列 -- 超高质量算法题单,配套我写的超高质量的题解和代码,题目难度不一 ...

  7. 解题报告(四)生成函数(ACM/ OI)

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 繁凡出品的全新系列:解题报告系列 -- 超高质量算法题单,配套我写的超高质量的题解和代码,题目难度不一 ...

  8. 解题报告(八) prufer 序列与 Cayley 公式(ACM / OI)超高质量题解

    繁凡出品的全新系列:解题报告系列 -- 超高质量算法题单,配套我写的超高质量题解和代码,题目难度不一定按照题号排序,我会在每道题后面加上题目难度指数(1∼51 \sim 51∼5),以模板题难度 11 ...

  9. 解题报告(一)E、(BZOJ4589)Hard Nim(博弈论 + FWT)

    繁凡出品的全新系列:解题报告系列 -- 超高质量算法题单,配套我写的超高质量题解和代码,题目难度不一定按照题号排序,我会在每道题后面加上题目难度指数(1∼51 \sim 51∼5),以模板题难度 11 ...

最新文章

  1. 陆奇“入驻” YC,开启新征程
  2. Python 技术篇-用PIL库旋转图片、改变图像尺寸
  3. Delphi项目的构成(Files That Make Up a Delphi Project)
  4. php什么设置前端代码,代码编辑器与PHPSTUDY的安装与配置过程(前端第一课)
  5. 不需要安装max或者xcode的object C开发环境
  6. ai怎么让图片任意变形_想一键提取图片文字,有什么好的文字识别软件/APP推荐吗?...
  7. 解决一个输入框可输入多个条件进行查询时,后端该怎么接收参数以及SQL语句如何写
  8. 我的docker随笔26:制作arm平台的python-pandas镜像
  9. ora-00054:resource busy and acquire with nowait specified
  10. 初学JAVA随记——代码练习(二元一次方程)
  11. Algs4-1.3.10中序表达式转为后序表达式(第二次实现)
  12. Kubernetes 小白学习笔记(32)--kubernetes云原生应用开发-sidecar注入和istio服务治理演示
  13. Activiti 5.16 用户手册
  14. Python官网无法打开解决方案
  15. Hyperledger Fabric 管理链码 peer lifecycle chaincode 指令使用
  16. Python Numpy的数组array和矩阵matrix
  17. 多入库口、出库口的提升系统仿真测试案例
  18. Java实验一—编程实现计算贷款偿还额的程序
  19. 针对Excel表格文件操作的编程实现
  20. ReID:常用损失函数总结

热门文章

  1. Linux重要命令-sar
  2. 腾讯云 AI 视觉产品基于流计算 Oceanus(Flink)的计费数据去重尝试
  3. java ffmpeg视频截图_Java实现对视频进行截图的方法【附ffmpeg下载】
  4. 全球5大云计算厂商的全球部署的节点图
  5. 如何寻找峰值及其位置(matlab)
  6. 关于深圳市住房和建设局《关于建立二手住房成交参考价格发布机制的通知》涉嫌法律依据不足、越权行使银保监人行市监等部门职能、涉嫌限制《物权法》赋予公民合法权益、涉嫌违背国务院及国家发改委上位法规
  7. pycharm使用私钥远程连接服务器
  8. mac 下安装mysql-5.7.16-osx10.11-x86_64
  9. Java面向对象程序思想
  10. 【多人在线游戏架构实战-基于C++的分布式游戏编程】开篇