B. Find the Spruce
题
意
:
在
一
个
由
′
.
′
和
′
∗
′
组
成
的
n
∗
m
的
矩
阵
中
,
存
在
多
少
“
塔
”
。
题意:在一个由 ' . ' 和 ' * '组成的 n*m的矩阵中,存在多少“塔” 。
题意:在一个由′.′和′∗′组成的n∗m的矩阵中,存在多少“塔”。
分
析
:
我
们
可
以
从
下
往
上
统
计
。
分析:我们可以从下往上统计。
分析:我们可以从下往上统计。
#include <set>
#include <map>
#include <list>
#include <cmath>
#include <stack>
#include <queue>
#include <string>
#include <bitset>
#include <vector>
#include<cstring>
#include <stdio.h>
#include <iostream>
#include <algorithm>
/*
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);*/
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
inline int read(){int s=0,w=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();return s*w;}
const int maxn= 2e3+5;
int mp[maxn][maxn];
int main()
{
int t=read();
while (t--)
{
memset(mp,0,sizeof(mp));
int n=read(),m=read();
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
char ch;cin>>ch;
if(ch=='.') mp[i][j]=0;
else mp[i][j]=1;
}
for(int i=n;i>=1;i--)
for(int j=m;j>=1;j--)
{
// 如果一个‘*’的左右和上方都是'*',那么就在它上面的方格加的数等于
// 它和左右两个方格的数取Min;
if(mp[i][j]&&mp[i][j-1]&&mp[i][j+1]&&mp[i-1][j])
{
int Min=min(mp[i][j],min(mp[i][j-1],mp[i][j+1]));
mp[i-1][j]+=Min;
}
}
int ans=0;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
if(mp[i][j]) ans+=mp[i][j];
printf("%d\n",ans);
}
return 0;
}
题 意 : 给 你 一 个 序 列 , 长 度 为 n , 每 个 元 素 是 1 到 n 的 整 数 , 然 后 有 m 次 操 作 题意:给你一个序列,长度为 n,每个元素是1到n 的整数,然后有m次操作 题意:给你一个序列,长度为n,每个元素是1到n的整数,然后有m次操作
每 次 操 作 的 有 p 的 概 率 将 下 标 为 1 到 r 的 元 素 变 成 上 升 序 列 , 问 有 将 整 个 序 列 变 成 上 升 每次操作的有p的概率将下标为1到r的元素变成上升序列,问有将整个序列变成上升 每次操作的有p的概率将下标为1到r的元素变成上升序列,问有将整个序列变成上升
序 列 的 概 率 为 多 少 序列的概率为多少 序列的概率为多少
分
析
:
我
们
可
以
先
去
掉
没
有
用
的
操
作
,
然
后
就
可
以
简
单
的
求
出
不
能
将
整
个
序
列
变
成
上
分析:我们可以先去掉没有用的操作,然后就可以简单的求出不能将整个序列变成上
分析:我们可以先去掉没有用的操作,然后就可以简单的求出不能将整个序列变成上
升
序
列
的
概
率
P
,
那
么
a
n
s
=
1
−
P
升序列的概率P,那么ans=1-P
升序列的概率P,那么ans=1−P
#include <set>
#include <map>
#include <list>
#include <cmath>
#include <stack>
#include <queue>
#include <string>
#include <bitset>
#include <vector>
#include<cstring>
#include <stdio.h>
#include <iostream>
#include <algorithm>
/*
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);*/
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
inline int read(){int s=0,w=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();return s*w;}
const int maxn= 1e5+5;
int a[maxn],c[maxn];
int main()
{
int t=read();
while (t--)
{
int n=read(),m=read();
for(int i=1;i<=n;i++) a[i]=read(),c[i]=a[i];
sort(c+1,c+n+1);
int cnt=0;
for(int i=n;i>=1;i--){
if(a[i]==i) cnt++;
else break;
}
double ans=1.0;
int tot=0;
for(int i=1;i<=m;i++){
int r; double p;
cin>>r>>p;
if(r>=(n-cnt)) ans*=(1.0-p),tot++;
}
bool flag=false;
for(int i=1;i<=n;i++)
if(a[i]!=c[i]){
flag=true;
break;
}
if(!flag) {puts("1.000000");continue;}
if(tot==0) puts("0.000000");
else printf("%lf\n",1.0-ans);
}
return 0;
}