2021 summer Niuke multi school 5

Posted by vladNz on Wed, 29 Dec 2021 20:47:22 +0100

B.Boxes

Meaning:
Is a probability distribution problem to find the minimum expected value.
that is ∑ i = The first one species feeling condition feeling condition number w [ i ] ∗ p \sum_{i = first case} ^ {number of cases}w[i]*p Σ I = number of cases in the first case w[i] * p

Main ideas:
There are two types in the general direction:
① Without asking the number of black balls, open all the boxes directly at the cost of ∑ i − 1 n w [ i ] \sum_{i-1}^{n}w[i] ∑i−1n​w[i].
② First ask how many balls each color has, and then construct the probability of the same suffix code
(open one ball at a time, and the probability of ending is (1/2)^(n-i))
Take the minimum value of two inside and output it.

#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
const int N=1e5+10;
int main()
{
    int n;
    double c;
    cin>>n>>c;
    double w[N]={0};
    for(int i=1;i<=n;i++)
        cin>>w[i];
    sort(w+1,w+n+1);
    double a[N]={0};
    for(int i=1;i<=n;i++)
        a[i]=a[i-1]+w[i];
    double ans=c;
    for(int i=1;i<n;i++)
        ans+=a[i]*pow(0.5,n-i);
    ans=min(ans,(double)a[n]);
    printf("%.6lf\n",ans);
    return 0;
}

D.Double Strings

Meaning:
Given two strings a and B, it is required to construct the subscript array a.b. The construction needs to meet the following two conditions:
∃ i , A [ a [ i ] ] < B [ b [ i ] ] \exist i,A[a[i]]<B[b[i]] ∃i,A[a[i]]<B[b[i]].
∀ j ( 1 ≤ j ≤ i − 1 ) , A [ a [ j ] ] = B [ b [ j ] ] \forall j(1\leq j\leq i-1),A[a[j]]=B[b[j]] ∀j(1≤j≤i−1),A[a[j]]=B[b[j]].
How many construction schemes are there?

Main ideas:
The construction method should generally be:
Same string + A character smaller than B + suffix of any length
① Same string:
Different from the longest common subsequence, the number of common strings is required here, so it should be the state transition of the code type.
② Use the combination number to solve the final answer.

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
const int N=5e3+10,mod=1e9+7;
char a[N],b[N];
int n,m;
ll dp[N][N],f[N<<1],inf[N<<1];
ll qpow(ll x,ll n)
{
    ll ans=1;
    while(n)
    {
        if(n&1) ans=(ans*x)%mod;
        n/=2;
        x=(x*x)%mod;
    }
    return ans;
}
ll c(int a,int b)
{
    return (f[b]*inf[a]%mod)*inf[b-a]%mod;
}
void pre()
{
    f[0]=inf[0]=1;
    for(int i=1;i<=n+m;i++)
    {
        f[i]=f[i-1]*i%mod;
        inf[i]=inf[i-1]*qpow((ll)i,(ll)mod-2)%mod;
    }
}
int main()
{
    cin>>a+1>>b+1;
    n=strlen(a+1),m=strlen(b+1);
    for(int i=0;i<=max(n,m);i++) dp[i][0]=dp[0][i]=1;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            dp[i][j]=((dp[i-1][j]+dp[i][j-1])%mod-(a[i]==b[j]?0:dp[i-1][j-1])+mod)%mod;
        }
    }
    pre();
    ll ans=0;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            if(b[j]>a[i])
                ans=(ans+dp[i-1][j-1]*c(n-i,m-i+n-j)%mod)%mod;
    cout<<ans<<endl;
    return 0;
}

J.Jewels

Each jewel falls at the speed of vm/s at (x,y,z). The cost of fishing a jewel is the distance between his position and (0,0,0). Ask the optimal fishing order.

Main ideas:
This is a typical minimum weight matching problem. Just change the template directly.

K.King of Range

Meaning:
Given an array containing n elements, select a continuous segment so that max min > K.
How many sets of continuous subsequences meet the requirements?

Main ideas:
Due to the limitation of data range, consider linear and nlogn algorithms.
After thinking, we can find that the monotone queue can be used to maintain the linearity, and the answer can be obtained.
Specific implementation:
Define two double ended queues, q1 stores the maximum value and q2 stores the minimum value.
q1.front () represents the maximum value of j-i and q2 is the minimum value.
i. J pointers are not backtracked.
See the code for details

#include<iostream>
#include<algorithm>
#include<deque>
using namespace std;
typedef long long ll;
const int N=1e5+10;
ll a[N];
int main()
{
    ll n,m;
    cin>>n>>m;
    for(int i=0;i<n;i++)
        cin>>a[i];
    while(m--)
    {
        ll k;
        deque<ll> q1,q2;
        cin>>k;
        ll ans=0;
        for(int i=0,j=0;i<n;i++)
        {
            while(q1.size()&&a[q1.back()]<=a[i]) q1.pop_back();
            while(q2.size()&&a[q2.back()]>=a[i]) q2.pop_back();
            q1.push_back(i),q2.push_back(i);
            while(q1.size()&&q2.size()&&a[q1.front()]-a[q2.front()]>k)
            {
                ans+=n-i;
                j++;
                if(q1.front()<j) q1.pop_front();
                if(q2.front()<j) q2.pop_front();
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}