luogu P1311 Select Inn explanations

\ (luogu \) P1311 Select Inn explanations

Topic Link

This question is at a loss to see first, a look at the statistics program, what number theory or mathematical knowledge to be sure of it, then I called the violence, found the answer right. So I looked at a sample explanation, found that the number of statistics programs only need to choose the inn program does not require the number of pipe solutions café. Then changed to change, pay up, successfully took \ (60 \) minutesIt took more than 10 minutes

#include<iostream>
#include<climits>
#include<cstdio>
#define ll long long
using namespace std;
const int N=1e3+100;
int n,k,p;
int color[N],cost[N];
int minn[N][N];
ll ans;
inline void pre()
{
    for (int i=1;i<=n;i++)
    {
        int minx=INT_MAX;
        for (int j=i;j<=n;j++)
        {
            minx=min(minx,cost[j]);
            minn[i][j]=minx;
        }
    }
    return ;
}
int main()
{
    scanf("%d%d%d",&n,&k,&p);
    for (int i=1;i<=n;i++)
    scanf("%d%d",color+i,cost+i);
    pre();
    for (int i=1;i<=n;i++)
    for (int j=i+1;j<=n;j++)
    if (color[i]==color[j]&&minn[i][j]<=p)
    ans++;
    printf("%lld\n",ans);
    return 0;
}

Lesson: write some function or variable have to look at the header file, or easy to use universal head

If the meaning of the questions is found, this question is relatively simple, for \ ([l, r] \ ) only need to check whether the minimum interval to meet it. Then think of \ (ST \) table, in fact, for every fixed \ (L \) , \ (r \) is legitimate is monotone, if \ ([l, r] \ ) legitimate, \ ( [l, [r, n] ] \) it must also be legitimate. So we can separate the two smallest \ (r \) , and then use the suffix or prefix and maintenance \ ([r, n] \ ) a total of how many legitimate solution, the accumulation is the answer.

// luogu-judger-enable-o2
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=2e5+100;
int f[21][N],color[N],sum[N][52];
int n,k,p;
ll ans;
inline void RMQ()
{
    for (int j=1;(1<<j)<=n;j++)
    for (int i=1;i+(1<<j)-1<=n;i++)
    f[j][i]=min(f[j-1][i],f[j-1][i+(1<<(j-1))]);
    return ;
}
inline int query(int l,int r)
{
    int k=log2(r-l+1);
    return min(f[k][l],f[k][r-(1<<k)+1]);
}
inline int midfind(int x)
{
    int l=x+1,r=n,mid=(l+r)>>1,ans=n+1;
    while (l<=r)
    {
        if (query(x,mid)<=p)
        {
            ans=mid;
            r=mid-1;
        }
        else l=mid+1;
        mid=(l+r)>>1;
    }
    return ans;
}
int main()
{
    scanf("%d%d%d",&n,&k,&p);
    for (int i=1;i<=n;i++)
    {
        scanf("%d%d",&color[i],&f[0][i]);
        color[i]++;
        for (int j=1;j<=51;j++)
        if (j==color[i]) sum[i][j]=sum[i-1][j]+1;
        else sum[i][j]=sum[i-1][j];
    }
    RMQ();
    for (int i=1;i<=n-1;i++)
    {
        int now=midfind(i)-1;
        ans+=sum[n][color[i]]-sum[now][color[i]];
    }
    printf("%lld\n",ans);
    return 0;
}

Lesson: half begins \ ([l, r] \ ) must determine a good, \ (ans \) practical significance should be determined, the second with assigned \ (r \) and without lawful solution \ (ans \) assignment properly handle.

Guess you like

Origin www.cnblogs.com/last-diary/p/11454811.html