Codeforces Round #253 (Div. 1)

第一次玩div1,大涨~~

B. Andrey and Problem
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Andrey needs one more problem to conduct a programming contest. He has n friends who are always willing to help. He can ask some of them to come up with a contest problem. Andrey knows one value for each of his fiends — the probability that this friend will come up with a problem if Andrey asks him.

Help Andrey choose people to ask. As he needs only one problem, Andrey is going to be really upset if no one comes up with a problem or if he gets more than one problem from his friends. You need to choose such a set of people that maximizes the chances of Andrey not getting upset.

Input

The first line contains a single integer n (1 ≤ n ≤ 100) — the number of Andrey's friends. The second line contains n real numbers pi(0.0 ≤ pi ≤ 1.0) — the probability that the i-th friend can come up with a problem. The probabilities are given with at most 6 digits after decimal point.

Output

Print a single real number — the probability that Andrey won't get upset at the optimal choice of friends. The answer will be considered valid if it differs from the correct one by at most 10 - 9.

题解:贪心+递推,可以发现我们如果选x个朋友的话,那么肯定得选前x个大的1,而且前x个的概率可以由之前的x-1个递推出来

具体代码如下

/**
 * @author neko01
 */
//#pragma comment(linker, "/STACK:102400000,102400000")
#include <cstdio>
#include <cstring>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
#include <math.h>
using namespace std;
typedef long long LL;
#define INF 0x7fffffff
const double eps=1e-8;
const double pi=acos(-1.0);
double a[101];
double dp[101];
bool cmp(double x,double y)
{
    return x>y;
}
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%lf",&a[i]);
    }
    sort(a,a+n,cmp);
    dp[0]=a[0];
    double ans=a[0];
    for(int i=1;i<n;i++)
    {
        double x=1;
        for(int j=0;j<i;j++)
            x*=(1-a[j]);
        dp[i]=(dp[i-1]*(1-a[i])+x*a[i]);
        ans=max(ans,dp[i]);
    }
    printf("%.9lf\n",ans);
    return 0;
}

C. Artem and Array
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Artem has an array of n positive integers. Artem decided to play with it. The game consists of n moves. Each move goes like this. Artem chooses some element of the array and removes it. For that, he gets min(a, b) points, where a and b are numbers that were adjacent with the removed number. If the number doesn't have an adjacent number to the left or right, Artem doesn't get any points.

After the element is removed, the two parts of the array glue together resulting in the new array that Artem continues playing with. Borya wondered what maximum total number of points Artem can get as he plays this game.

Input

The first line contains a single integer n (1 ≤ n ≤ 5·105) — the number of elements in the array. The next line contains n integers ai (1 ≤ ai ≤ 106) — the values of the array elements.

Output

In a single line print a single integer — the maximum number of points Artem can get.

给定n个数字,要求一个个删掉数字,数字左边或右边没有数字就不得分,否则得分为min(左边数字,右边数字)

题解:如果数列中有先减后增的这种,那么肯定先去这里面的最小的,依次取完,可以用一个单调栈维护,每次加入栈内的数都比前一个小,否则从栈顶取出比当前数小的,ans加上min(左边数字,右边数字)。这样处理完,栈内的数有三种情况,单调增,单调减,先增后减,可以发现这种情况的最大值是除了栈内的最大两值剩下所有值之和。

代码如下:

/**
 * @author neko01
 */
//#pragma comment(linker, "/STACK:102400000,102400000")
#include <cstdio>
#include <cstring>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
#include <math.h>
using namespace std;
typedef long long LL;
#define INF 0x7fffffff
const double eps=1e-8;
const double pi=acos(-1.0);
const int N=500005;
int s[N];
int main()
{
    int n,top=0;
    LL ans=0;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        int x;
        scanf("%d",&x);
        while(top>1&&s[top-2]>=s[top-1]&&x>=s[top-1])
        {
            ans+=min(x,s[top-2]);
            top--;
        }
        s[top++]=x;
    }
    sort(s,s+top);
    for(int i=0;i<top-2;i++)
        ans+=s[i];
    cout<<ans<<endl;
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值