从大到小不断组合,如果开心1次的概率大于开心0次概率就可以break了
因为x会越来越小
#include
#define fo(i,a,b) for(i=a;i<=b;i++)
#define fd(i,a,b) for(i=a;i>=b;i--)
using namespace std;
int T,n,i,k;
double a[10005],b[10005],c[10005];
double s,p,x,y,t;
int main()
{
scanf("%d",&T);
while (T--)
{
scanf("%d",&n);
fo(i,1,n) scanf("%lf",&a[i]);
sort(a+1,a+n+1);
x = a[n]; y = 1 - a[n];
fd(i,n-1,1)
{
if (y < x) break;
x = x + a[i] * (y - x);
y = y * (1 - a[i]);
}
printf("%.12lf\n",x);
}
return 0;
}
先对x从大到小排序,枚举x,那么比x大的那一部分显然都要扔到另一边,比x小的那一部分挑一个和x最接近的扔到另一边
#include
#define fo(i,a,b) for(i=a;i<=b;i++)
#define fd(i,a,b) for(i=a;i>=b;i--)
#define N 100005
using namespace std;
struct www{long long a,b;} f[N];
int T,n,i;
long long pre,res,s1,s2,A,B;
bool cmp(const www &x,const www &y) {return x.a > y.a;}
int main()
{
//freopen("1.in","r",stdin);
//freopen("mine.out","w",stdout);
scanf("%d",&T);
while (T--)
{
scanf("%d",&n);
fo(i,1,n) scanf("%lld%lld",&f[i].a,&f[i].b);
sort(f+1,f+n+1,cmp);
pre = -1;
multiset<long long> s;
fo(i,1,n) s.insert(f[i].b);
res = 1e18;
fo(i,1,n)
{
s1 = f[i].a;
s.erase(s.lower_bound(f[i].b));
auto it=s.lower_bound(f[i].a);
if (it!=s.end()) A=*it; else A = -1;
if(it!=s.begin()) B=*(--it); else B = -1;
if (A == -1 && B == -1) s2 = 0;
if (B == -1) s2 = A; else
if (A == -1) s2 = B; else
if (abs(f[i].a-A) < abs(f[i].a-B)) s2 = A; else s2 = B;
if (pre > s2 && pre != -1) s2 = pre;
if (abs(f[i].a-pre) < abs(f[i].a-s2) && pre != -1) s2 = pre;
res = min(res,abs(s1-s2));
pre = max(pre,f[i].b);
}
cout<<res<<endl;
}
}
每次敲掉一个块之后往四个方向搜索即可
#include
#define fo(i,a,b) for(i=a;i<=b;i++)
#define fd(i,a,b) for(i=a;i>=b;i--)
#define N 2005
using namespace std;
int a[N][N];
int res,T,n,m,q,i,j,x,y;
void dfs(int x,int y)
{
int numx,numy;
if (a[x][y] == -1) return;
if (a[x][y] == 0) return;
numx = numy = 0;
if (a[x-1][y] == 0 || a[x+1][y] == 0) numx++;
if (a[x][y+1] == 0 || a[x][y-1] == 0) numy++;
if (numx && numy)
{
a[x][y] = 0;
res++;
dfs(x+1,y); dfs(x-1,y);
dfs(x,y+1); dfs(x,y-1);
}
}
int main()
{
scanf("%d",&T);
while (T--)
{
scanf("%d%d%d",&n,&m,&q);
fo(i,1,n) fo(j,1,m) a[i][j] = 1;
fo(i,0,n+1) a[i][0] = a[i][m+1] = -1;
fo(j,0,m+1) a[0][j] = a[n+1][j] = -1;
while (q--)
{
res = 0;
scanf("%d%d",&x,&y);
if (a[x][y] == 0) {printf("%d\n",res); continue;}
a[x][y] = 0; res++;
dfs(x+1,y); dfs(x-1,y);
dfs(x,y+1); dfs(x,y-1);
printf("%d\n",res);
}
}
}