第一次玩div1,大涨~~
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.
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.
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;
}
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.
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.
In a single line print a single integer — the maximum number of points Artem can get.
给定n个数字,要求一个个删掉数字,数字左边或右边没有数字就不得分,否则得分为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;
}