贪心选择概率大的礼物,如果能使得1次惊喜的几率变大则加入,否则结束。(凭感觉莽的,没有仔细证明
#include
#define ll long long
#define lowbit(x) ((x)&(-(x)))
using namespace std;
const int maxn = 1e4 + 50;
double p[maxn];
int n;
int main()
{
int T;cin>>T;
while(T--){
scanf("%d", &n);
for(int i = 0; i < n; ++i){
scanf("%lf", &p[i]);
}
sort(p, p+n);
double p0 = 1.0, p1 = 0.0;
for(int i = n-1; i >= 0; --i){
if(p0-p1 > 0){
p1 = p1 + (p0-p1)*p[i];
p0 = p0*(1-p[i]);
}
else break;
}
printf("%.8f\n", p1);
}
}
按x排序,枚举每一个人作为最大的x,设当前枚举到i,则i后面的人只能选y(因为i后面的人x都大于等于i的x),如果后面的最大的y大于等于当前x,那么差距无法再减少了。如果比当前x小,那么看i前面有没有能缩小差距的y。
#include
#define ll long long
using namespace std;
const int maxn = 1e5 + 50;
struct node{
ll x, y;
bool operator < (const node& a) const{return x < a.x;};
}e[maxn];
set<ll> s;
int n;
ll mx[maxn];
int main()
{
int T;cin>>T;
while(T--){
scanf("%d", &n);
for(int i = 0; i < n; ++i){
scanf("%lld%lld", &e[i].x, &e[i].y);
}
sort(e, e+n);
mx[n] = 0;
for(int i = n-1; i >= 0; --i){
mx[i] = max(e[i].y, mx[i+1]);
}
s.clear();
ll ans = abs(e[0].x - mx[1]);
s.insert(e[0].y);
for(int i = 1; i < n; ++i){
ll t;
if(i < n-1 && e[i].x <= mx[i+1]){
t = mx[i+1] - e[i].x;
}
else{
if(i < n-1) t = e[i].x - mx[i+1];
else t = 2e18;
set<ll>::iterator it = s.lower_bound(e[i].x);
if(it != s.end()){
t = min(t, *it - e[i].x);
}
if(it != s.begin()){
it--;
t = min(t, e[i].x - *it);
}
}
ans = min(ans, t);
s.insert(e[i].y);
}
printf("%lld\n", ans);
}
}
按题意模拟
#include
#define ll long long
#define lowbit(x) ((x)&(-(x)))
using namespace std;
const int maxn = 2e3 + 50;
int mp[maxn][maxn];
int vis[maxn][maxn][2];
int n, m , k;
int cnt;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
bool in(int x, int y){
if(x < 1 || x > n || y < 1 || y > m) return false;
return true;
}
void dfs(int x, int y)
{
//cout<<"x:"<
if(mp[x][y] == 0) return;
mp[x][y] = 0;
cnt++;
for(int i = 0; i < 4; ++i){
int xx = x + dx[i];
int yy = y + dy[i];
if(!in(xx, yy)) continue;
if(i < 2) vis[xx][yy][0] --;
else vis[xx][yy][1]--;
}
for(int i = 0; i < 4; ++i){
int xx = x + dx[i];
int yy = y + dy[i];
if(!in(xx, yy)) continue;
if(vis[xx][yy][0] < 2 && vis[xx][yy][1] < 2){
dfs(xx, yy);
}
}
return;
}
int main()
{
int T;cin>>T;
while(T--){
scanf("%d%d%d", &n, &m, &k);
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= m; ++j){
mp[i][j] = 1;
vis[i][j][0] = vis[i][j][1] = 2;
}
}
while(k--){
cnt = 0;
int x, y;
scanf("%d%d", &x, &y);
dfs(x, y);
printf("%d\n", cnt);
}
}
}