Codeforces Round #689

本文介绍了两种算法问题的解决方案。第一部分涉及在由'.'和'*'组成的矩阵中统计'塔'的数量,通过从下往上逐层处理,更新每个元素的值。第二部分讨论了在给定序列中通过概率计算确定序列变为上升序列的概率。文章通过实例解释了算法思路并提供了C++实现代码。
摘要由CSDN通过智能技术生成

B. Find the Spruce
题 意 : 在 一 个 由 ′ . ′ 和 ′ ∗ ′ 组 成 的 n ∗ m 的 矩 阵 中 , 存 在 多 少 “ 塔 ” 。 题意:在一个由 ' . ' 和 ' * '组成的 n*m的矩阵中,存在多少“塔” 。 .nm
分 析 : 我 们 可 以 从 下 往 上 统 计 。 分析:我们可以从下往上统计。

#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;
}

C. Random Events (水题)

题 意 : 给 你 一 个 序 列 , 长 度 为 n , 每 个 元 素 是 1 到 n 的 整 数 , 然 后 有 m 次 操 作 题意:给你一个序列,长度为 n,每个元素是1到n 的整数,然后有m次操作 n1nm

每 次 操 作 的 有 p 的 概 率 将 下 标 为 1 到 r 的 元 素 变 成 上 升 序 列 , 问 有 将 整 个 序 列 变 成 上 升 每次操作的有p的概率将下标为1到r的元素变成上升序列,问有将整个序列变成上升 p1r

序 列 的 概 率 为 多 少 序列的概率为多少

分 析 : 我 们 可 以 先 去 掉 没 有 用 的 操 作 , 然 后 就 可 以 简 单 的 求 出 不 能 将 整 个 序 列 变 成 上 分析:我们可以先去掉没有用的操作,然后就可以简单的求出不能将整个序列变成上 :
升 序 列 的 概 率 P , 那 么 a n s = 1 − P 升序列的概率P,那么ans=1-P Pans=1P

#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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值