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:
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.
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
#include
#include
#include
#include
下面是poj1062,基本没什么好说的,模板题,我多加了分号,刚好空着一起写了
#include
#include
#include
#include
#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]&&!visit[i])
{
te=dist[i];
node=i;
//cout<0&&dist[i]>dist[node]+price[node][i])
dist[i]=dist[node]+price[node][i];
}
//cout<>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<level[i]||level[i]-level[j]>m)
visit[j]=true;
else visit[j]=false;}
t=djskl();
//cout<
代码很差,没改的说。。。