概率dp+线性dp+最短路判环+二分最短路

E. Wish I Knew How to Sort

思路:在一个只包含01的数组中,要使得数组非递减,求出要执行交换操作的期望值
1.最后的数组一定是0都在前,1都在后面。那么需要需要交换的次数为在0位置上的1.
2.前面cnt个1对用后面cnt个0,因此对于第i次交换的概率为 2*i*i/(n-1)*n,则期望为概率的倒数。
3.期望的线性可加性,可将所有位置的上的期望累加。

#include<bits/stdc++.h>
#define int long long
#define endl '\n'
#define For(i,a,b) for(i=(a);i<=(b);++i)
#define ios (ios::sync_with_stdio(false),cin.tie(0),cout.tie(0))
using namespace std;
const int N=2e5+5;
const int inf=1e18;
const int mod=998244353;
int n,a[N];
int qpow(int x,int y)
{
    int res=1;
    while(y)
    {
        if(y&1) res=res*x%mod;
        x=x*x%mod;y>>=1;
    }
    return res;
}
void solve()
{
    cin>>n;
    int cnt=0,one=0;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        if(!a[i]) cnt++;
    }
    for(int i=1;i<=cnt;i++)
        if(a[i]==1) one++;
    int ans=0;
    for(int i=1;i<=one;i++)
        ans=(ans+n*(n-1)%mod*qpow(2*i*i%mod,mod-2))%mod;
    cout<<ans<<endl;
}
signed main()
{
    //ios;
    int T;cin>>T;
    while(T--)
        solve();
    return 0;
}

C、1919810

思路:要求子序列的长度固定为7,那么我们长度讨论到7即可。
设计状态:dp[k][i]序列长度为k时,到第i为增减性同1919810的序列个数。
1.可观察第一位,长度为1时,可初始化为dp[1][1~n]==1
2.在第二位和第四位时,要求第k位数字比前一个数字大。则要求j<i时,小于a[i]结尾的序列前缀和。
3.其他位和情况2相反。

#include<bits/stdc++.h>
#define int long long
#define endl '\n'
#define For(i,a,b) for(i=(a);i<=(b);++i)
#define ios (ios::sync_with_stdio(false),cin.tie(0),cout.tie(0))
using namespace std;
const int N=2e6+5;
const int inf=1e18;
const int mod=1e9+7;
int n,ans,a[N],dp[2][N];
string s;

void solve()
{
    cin>>s;
    n=s.length();
    for(int i=1;i<=n;i++) a[i]=s[i-1]-'0';
    int f=0;
    for(int k=1;k<=7;k++)  //要求子序列长度为7
    {
        f^=1;
        vector<int>sum(10);
        if(k==1)
        {
            for(int i=1;i<=n;i++) dp[f][i]=1;
        }
        else if(k==2||k==4)
        {
            for(int i=1;i<=n;i++)
            {
                dp[f][i]=0;
                for(int j=0;j<a[i];j++)
                    dp[f][i]=(dp[f][i]+sum[j])%mod;
                sum[a[i]]=(sum[a[i]]+dp[f^1][i])%mod;
            }
        }
        else
        {
            for(int i=1;i<=n;i++)
            {
                dp[f][i]=0;
                for(int j=a[i]+1;j<10;j++)
                    dp[f][i]=(dp[f][i]+sum[j])%mod;
                sum[a[i]]=(sum[a[i]]+dp[f^1][i])%mod;
            }
        }
    }
    for(int i=7;i<=n;i++) ans=(ans+dp[f][i])%mod;
    cout<<ans<<endl;
}
signed main()
{
    //ios;
    //int T;cin>>T;
    //while(T--)
        solve();
    return 0;
}

E. Buy and Delete

1.这题刚开始题意理解错了,想去找个最小环,用所带的钱c去除以这个代价即为能玩几轮,离谱了。
2.不过还好最后队友读出真的题意,正解也是去找个最小环,只需要和所带钱作比较即可,答案只在0、1、2三个之间。
3.能想到最小环的代价这点是对的,但我竟然用深搜去找最小环,怎么都写不对。队友想到dij判环,却没想到0、1、2这个点……

#include <bits/stdc++.h>
#define endl '\n'
#define int long long
#define PII pair<int,int>
using namespace std;
const int N = 2e4+100;
const int inf=1e18;
int n,m,c,cnt,fa[N],vis[N],mi=inf,dist[N],ans;
vector<pair<int,int>>e[N];
void dij(int st)
{
    priority_queue<PII,vector<PII>,greater<PII>>q;
    for(int i=1;i<=n;i++) dist[i]=inf,vis[i]=0;
    dist[st]=0;
    q.push({0,st});
    while(!q.empty())
    {
        int u=q.top().second;
        q.pop();
        if(vis[u]) continue;
        vis[u]=1;
        for(auto cur:e[u])
        {
            int v=cur.first,w=cur.second;
            if(v==st) mi=min(mi,dist[u]+w);
            if(dist[v]>dist[u]+w)
                dist[v]=dist[u]+w,q.push({dist[v],v});
        }
    }
}
void solve()
{
    cin>>n>>m>>c;
    int g=inf;
    for(int i=1;i<=m;i++)
    {
        int u,v,w;cin>>u>>v>>w;
        e[u].push_back({v,w});
        g=min(g,w);
    }
    if(c<g)
    {
        cout<<0<<endl;return;
    }
    else
    {
        for(int i=1;i<=n;i++)
            dij(i);
        //cout<<mi<<endl;
        if(c>=mi)
            cout<<2<<endl;
        else
            cout<<1<<endl;
    }
}
signed main()
{
	solve();
	return 0;
}
/*
4 5 30
1 2 1
2 3 2
3 1 10
3 4 3
4 1 3

2 10 100
1 2 1
2 1 2
1 2 1
2 1 2
1 2 1
2 1 2
1 2 1
2 1 2
1 2 1
2 1 2


5 10 100
1 2 2
3 1 2
3 4 1
4 5 1
5 3 1
5 4 1
3 5 2
1 2 1
2 1 2
3 1 4


5 5 100
1 2 1
2 3 2
3 4 3
4 2 3
4 1 4
*/

D.逃亡的贝贝

思路:
二分权值最大的最小值x,根据边权建立新的图,若权值<=x,建边边权为0,若使用药水小于等于x,则建边边权为1。采用dijkstra算法跑最短路,讨论需要的药水数量是否小于等于k

#include <bits/stdc++.h>
#define endl '\n'
#define int long long
#define ios (ios::sync_with_stdio(false),cin.tie(0),cout.tie(0))
#define PII pair<int,int>
using namespace std;
const int N = 2e5+100;
const int inf=1e18;
int n,m,S,T,k;
struct node
{
    int u,v,w;
}edge[N];
int vis[N],dist[N];
vector<pair<int,int>>e[N];
void dij(int st)
{
    priority_queue<PII,vector<PII>,greater<PII>>q;
    for(int i=1;i<=n;i++) dist[i]=inf,vis[i]=0;
    dist[st]=0;
    q.push({0,st});
    while(!q.empty())
    {
        int u=q.top().second;
        q.pop();
        if(vis[u]) continue;
        vis[u]=1;
        for(auto cur:e[u])
        {
            int v=cur.first,w=cur.second;
            if(dist[v]>dist[u]+w)
                dist[v]=dist[u]+w,q.push({dist[v],v});
        }
    }
}
bool check(int x)
{
    for(int i=1;i<=n;i++) e[i].clear();
    for(int i=1;i<=m;i++)
    {
        int u=edge[i].u,v=edge[i].v,w=edge[i].w;
        if(w<=x)
            e[u].push_back({v,0}),e[v].push_back({u,0});
        else if(ceil(114*w*1.0/514)<=x)
            e[u].push_back({v,1}),e[v].push_back({u,1});
    }
    dij(S);
    return dist[T]<=k;
}
void solve()
{
    cin>>n>>m>>S>>T>>k;
    int l=0,r=0,mid,ans=-1;
    for(int i=1;i<=m;i++)
    {
        int u,v,w;cin>>u>>v>>w;
        edge[i]={u,v,w};
        r=max(r,w);
    }
    while(l<=r)
    {
        mid=(l+r)/2;
        if(check(mid))
            r=mid-1,ans=mid;
        else
            l=mid+1;
    }
    if(ans!=-1)
        cout<<ans<<endl;
    else
        cout<<"I really need TS1's time machine again!"<<endl;
}
signed main()
{
    //ios;
    solve();
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值