Live2d Test Env

【CQ18高一暑假前挑战赛5】标程

【A:暴力】

#include<bits/stdc++.h>
using namespace std;
const int maxn=100010;
int a[maxn],vis[maxn],N,M;
int main()
{
    scanf("%d%d",&N,&M);
    for(int i=1;i<=M;i++){
        scanf("%d",&a[i]);
        vis[a[i]]=1;
    }
    for(int i=1;i<=N;i++)
      if(!vis[i]) printf("%d\n",i);
    return 0;
}

【B:模拟】

#include<bits/stdc++.h>
using namespace std;
const int maxn=20010;
vector<short int>G[maxn];
int main()
{
    int N,M,x,i,j;
    scanf("%d%d",&N,&M);
    for(i=1;i<=N;i++)
      for(j=0;j<M;j++){
        scanf("%d",&x); G[i].push_back(x);
    }
    printf("%d %d\n",M,N);
    for(i=0;i<M;i++){
       for(j=1;j<=N;j++) printf("%d ",G[j][i]);
       printf("\n");
    }
    return 0; 
}

【C:数位DP入门题】

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=100;
int N,L,a[maxn];
ll dp[maxn][2][maxn],num[maxn][2][maxn],ans;
void divede()
{
    while(N){ a[++L]=N%10; N/=10; }
}
ll dfs(int pos,int lim,int x)
{
    if(pos==0){  num[pos][lim][x]=1; return x==1;}
    if(!lim&&dp[pos][lim][x]) return dp[pos][lim][x];
    int ups=lim?a[pos]:9;
    for(int i=0;i<=ups;i++){
        dp[pos][lim][x]+=(dfs(pos-1,lim&(i==ups),i))+(x==1?num[pos-1][lim&(i==ups)][i]:0);
        num[pos][lim][x]+=num[pos-1][lim&(i==ups)][i];
    }
    return dp[pos][lim][x];
}
int main()
{
    scanf("%d",&N);
    divede();
    for(int i=0;i<=a[L];i++) ans+=dfs(L-1,i==a[L],i);
    printf("%lld\n",ans);
    return 0;
}

【D:单调队列】

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=350010;
const int inf=1e9+7;
int a[maxn],L1[maxn],b[maxn],R1[maxn],L2[maxn],R2[maxn],q[maxn],head;
vector<int>G[maxn];
ll get(int u)
{
    int Len=G[b[u]].size(); ll res=0;
    for(int i=0;i<Len;i++){
        if(u>=G[b[u]][i]){
            ll r=min(R1[G[b[u]][i]],R2[u])-u+1;
            ll l=G[b[u]][i]-max(L2[u],L1[G[b[u]][i]])+1;
            if(r>0&&l>0) res+=l*r;
        }
        else {
            ll r=min(R2[u],R1[G[b[u]][i]])-G[b[u]][i]+1;
            ll l=u-max(L2[u],L1[G[b[u]][i]])+1;
            if(r>0&&l>0) res+=l*r;
        }
    }
    return res;
}
int main()
{
    int N,i; ll ans=0;
    scanf("%d",&N);
    for(i=1;i<=N;i++) scanf("%d",&a[i]);
    for(i=1;i<=N;i++) scanf("%d",&b[i]);
    a[0]=inf; head=1; q[head]=0;
     for(i=1;i<=N;i++){ //若有相同,只算左边第一个。 
         while(head&&a[i]>a[q[head]]) head--;
         L1[i]=q[head]+1; q[++head]=i;
    }
    a[N+1]=inf; head=1; q[head]=N+1;
    for(i=N;i>=1;i--){
        while(head&&a[i]>=a[q[head]]) head--;
        R1[i]=q[head]-1; q[++head]=i;
    }
    for(i=1;i<=N;i++) G[a[i]].push_back(i);
    
    b[0]=inf; head=1; q[head]=0;
     for(i=1;i<=N;i++){  
         while(head&&b[i]>b[q[head]]) head--;
         L2[i]=q[head]+1; q[++head]=i;
    }
    b[N+1]=inf; head=1; q[head]=N+1;
    for(i=N;i>=1;i--){
        while(head&&b[i]>=b[q[head]]) head--;
        R2[i]=q[head]-1; q[++head]=i;
    }    
    for(i=1;i<=N;i++) ans+=get(i);
    printf("%lld\n",ans);
    return 0;
}

 

posted @ 2018-06-07 11:31  nimphy  阅读(312)  评论(0编辑  收藏  举报