A2. Oh Sweet Beaverette

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

— Oh my sweet Beaverette, would you fancy a walk along a wonderful woodland belt with me?

— Of course, my Smart Beaver! Let us enjoy the splendid view together. How about Friday night?

At this point the Smart Beaver got rushing. Everything should be perfect by Friday, so he needed to prepare the belt to the upcoming walk. He needed to cut down several trees.

Let's consider the woodland belt as a sequence of trees. Each tree i is described by the esthetic appeal ai — some trees are very esthetically pleasing, others are 'so-so', and some trees are positively ugly!

The Smart Beaver calculated that he needed the following effects to win the Beaverette's heart:

  • The first objective is to please the Beaverette: the sum of esthetic appeal of the remaining trees must be maximum possible;
  • the second objective is to surprise the Beaverette: the esthetic appeal of the first and the last trees in the resulting belt must be the same;
  • and of course, the walk should be successful: there must be at least two trees in the woodland belt left.

Now help the Smart Beaver! Which trees does he need to cut down to win the Beaverette's heart?

Input

The first line contains a single integer n — the initial number of trees in the woodland belt, 2 ≤ n. The second line contains space-separated integers ai — the esthetic appeals of each tree. All esthetic appeals do not exceed 109 in their absolute value.

  • to get 30 points, you need to solve the problem with constraints: n ≤ 100 (subproblem A1);
  • to get 100 points, you need to solve the problem with constraints: n ≤ 3·105 (subproblems A1+A2).

Output

In the first line print two integers — the total esthetic appeal of the woodland belt after the Smart Beaver's intervention and the number of the cut down trees k.

In the next line print k integers — the numbers of the trees the Beaver needs to cut down. Assume that the trees are numbered from 1 ton from left to right.

If there are multiple solutions, print any of them. It is guaranteed that at least two trees have equal esthetic appeal.

题意:有一排树,每个树有美丑值,让你砍出最大的一串,顺便要输出砍树的序号,砍树要求:

剩下的一串最左边和最右边相等,最少剩2棵树

其实这题跟最大子序列差不多,而且还不用按序列砍,随意砍的话我们用dp存到i为止最大能到多少,也就是dp[i]=dp[i-1]+a[i]>0?a[i]:0;

然后因为要头尾相等我们用map记录下每个新出的节点的序号,然后找对应的右端点与之对应,假如a[i]已经访问,就可以求他到左端的最大串,注意a[i]必须区,所以不能直接用dp[i]-dp[visit[a[i]-1],要是a[i]<0就跪了。

这题坑我的地方是,他0 0竟然不输出,我输出了0,就跪了,fuck

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <map>
using namespace std;
struct node{int number;__int64 value;
};
int a[300010];
__int64 dp[300010];
int cut[300010];
node ans;
//__int64 ans[100010];
int main()
{map <int,int> visit;//__int64 sum;int i,j,m,n;//sum=0;int cnt=0;ans.value=-2000000010;memset(dp,0,sizeof(dp));cin>>n;for (i=1;i<=n;i++)cin>>a[i];for (i=1;i<=n;i++){if (a[i]>=0){dp[i]=dp[i-1]+a[i];}else {dp[i]=dp[i-1];}if (!visit[a[i]])      visit[a[i]]=i;else{if (ans.value<dp[i-1]-dp[visit[a[i]]]+2*a[i]){ans.value=dp[i-1]-dp[visit[a[i]]]+2*a[i];ans.number=i;}}}cout<<ans.value<<" ";for (i=1;i<visit[a[ans.number]];i++)cut[cnt++]=i;for (i=visit[a[ans.number]]+1;i<ans.number;i++)if (a[i]<0) cut[cnt++]=i;for (i=ans.number+1;i<=n;i++)cut[cnt++]=i;cout<<cnt<<endl;for (i=0;i<cnt-1;i++)cout<<cut[i]<<" ";if (cnt) cout<<cut[cnt-1]<<endl;
}

下面是poj1062,基本没什么好说的,模板题,我多加了分号,刚好空着一起写了

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#define inf 0x7fffffff
using namespace std;
bool visit[101];
int price[101][101],n;
int dist[102];
int djskl()
{int i,j,te;for (i=1;i<=n;i++)dist[i]=price[0][i];int node=0;for (j=1;j<=n;j++){te=inf;for (i=1;i<=n;i++){//cout<<dist[i]<<endl;if (te>dist[i]&&!visit[i]){te=dist[i];node=i;//cout<<te<<endl;}}if (node==0) break;//cout<<node<<endl;visit[node]=true;for (i=1;i<=n;i++){if (!visit[i]&&price[node][i]>0&&dist[i]>dist[node]+price[node][i])dist[i]=dist[node]+price[node][i];}//cout<<node<<endl;}return dist[1];
}
int main()
{int level[101];int i,j,m,x,t,v;while (cin>>m>>n){memset(price,0,sizeof(price));memset(visit,false,sizeof(visit));memset(level,0,sizeof(level));memset(dist,inf,sizeof(dist));for (i=1;i<=n;i++){cin>>price[0][i]>>level[i]>>x;for (j=1;j<=x;j++){cin>>t>>v;price[t][i]=v;}}//cout<<price[3][1]<<endl;v=inf;for (i=1;i<=n;i++){for (j=1;j<=n;j++){if (level[j]>level[i]||level[i]-level[j]>m)visit[j]=true;else visit[j]=false;}t=djskl();//cout<<dist[1]<<end//cout<<price[3][1]<<endl;if (t<v) v=t;}cout<<v<<endl;//cout<<price[4][1]<<endl;}
}

代码很差,没改的说。。。

abyy a+poj1062相关推荐

  1. poj1062 Bellman 最短路应用

    昂贵的聘礼 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 41066   Accepted: 11959 Descripti ...

  2. poj1062昂贵的聘礼(Dijkstra**)

    1 /* 2 题意: 物主有一个物品,价值为P,地位为L, 以及一系列的替代品Ti和该替代品所对应的"优惠"Vi 3 g[u][i] 表示的是u物品被i物品替换后的优惠价格!(u& ...

  3. 最短路径——Dijkstra算法扩展(hdu2066,poj1062)

    *本文介绍了一些Dijkstra的变型例题,对Dijkstra算法不是很了解了可以移步上篇博客http://blog.csdn.net/sm9sun/article/details/53283791 ...

  4. POJ1062昂贵的聘礼(经典) 枚举区间 +【Dijkstra】

    <题目链接>                   昂贵的聘礼 Description 年轻的探险家来到了一个印第安部落里.在那里他和酋长的女儿相爱了,于是便向酋长去求亲.酋长要他用1000 ...

  5. POJ1062 昂贵的聘礼(最短路径)

    题意: 中文题 要点: 等级比较难处理,注意题目中是间接接触也不行,所以假如酋长等级是5,差距为2,可以交换的等级有(3,4,5),(4,5,6),(5,6,7),也即是区间长度是m,交换中最大的与最 ...

  6. 做acm 需要学的算法

    做acm 需要学的算法 转一个搞ACM需要的掌握的算法.  要注意,ACM的竞赛性强,因此自己应该和自己的实际应用联系起来.  适合自己的才是好的,有的人不适合搞算法,喜欢系统架构,因此不要看到别人什 ...

  7. acm经典题Mark

    著名的北邮ACM推荐50题 POJ推荐50题 1.标记"难"和"稍难"的题目可以看看,思考一下,不做要求,当然有能力的同学可以直接切掉. 2.标记为A and ...

  8. ACM题集以及各种总结大全(转)

    ACM题集以及各种总结大全! 虽然退役了,但是整理一下,供小弟小妹们以后切题方便一些,但由于近来考试太多,顾退役总结延迟一段时间再写!先写一下各种分类和题集,欢迎各位大牛路过指正. 一.ACM入门 关 ...

  9. (牛客腾讯思维编程题)编码编码分组打印下标题目分析

    本题答案在这点击进入 假定一种编码的编码范围是a ~ y的25个字母,从1位到4位的编码,如果我们把该编码按字典序排序,形成一个数组如下: a, aa, aaa,aaaa, aaab, aaac, - ...

  10. 一步一步深入理解Dijkstra算法

    先简单介绍一下最短路径: 最短路径是啥?就是一个带边值的图中从某一个顶点到另外一个顶点的最短路径. 官方定义:对于内网图而言,最短路径是指两顶点之间经过的边上权值之和最小的路径. 并且我们称路径上的第 ...

最新文章

  1. java判断一个对象是否为空_Java中判断对象是否为空的方法的详解
  2. 威尔逊定理 ---- [hdu-6608] Fansblog 威尔逊定理 质数的密度分布 快速乘优化快速幂防止中间爆longlong
  3. Focal Loss和它背后的男人RetinaNet
  4. 【awk】用awk将Fasta文件序列变成一行
  5. 3Delight粒子渲染,真快。
  6. adb常用命令的介绍及使用
  7. es6 async函数的实现原理
  8. ntp协议中 服务器失效怎么办,排除网络时间协议(NTP)故障
  9. paper 134:结构张量structure tensor(二)
  10. Mybatis-01-简介及入门
  11. 为什么房间的 Wi-Fi 信号这么差
  12. MacBook上Wi-Fi抓包权限
  13. 用线性代数解释图论中的一些结论
  14. win10计算机盘符如何,删除win10电脑多余无需使用的盘符教程
  15. 【asm】汇编器yasm使用说明
  16. 战地5未能达到服务器带宽,《战地5》游戏bug汇总以及解决方案介绍
  17. 「Thymeleaf页面在浏览器加载不出来」
  18. 玩客云刷入armbian系统总结
  19. LAB颜色空间各通道的取值范围
  20. 思科ccie和华为hcie中交换机环路的产生原因和解决方法

热门文章

  1. 饮水机和水桶图解RAID方式,强烈推荐
  2. js模仿f11全屏_Js浏览器全屏代码(模仿按F11)
  3. CSAPP第五章家庭作业(原书第二版)
  4. python中访问列表元素具体格式_Python3基础 list 访问列表中的列表的元素
  5. Colorbox 参数设置-中文版
  6. 基于C#的词法分析关键字识别与变色
  7. zabbix监控平台设置报警发送邮件
  8. 天狮集团云函数实践:自定义业务逻辑实现跨境电商全球直播
  9. 峯云5G:纵论AI赋能 聚焦企业联络与协同
  10. Python心法:numpy命令关于axis=0,axis=1,axis=2