zoukankan      html  css  js  c++  java
  • NOIP2017题解

    Day1

    T1 小凯的疑惑

    #include<cstdio>
    int main()
    {
        long long a,b;
        scanf("%lld%lld",&a,&b);
        printf("%lld",a*b-a-b);
        return 0;
    }
    

    T2 时间复杂度

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    using namespace std;
    
    typedef long long ll;
    
    const int Maxn=1100;
    
    int t,n,s[Maxn],top,ans,err;
    
    char a[Maxn],b[Maxn][Maxn];
    
    int rfs(char *a) {
        int ans=0,now=0;
        char ch=a[0];
        while(ch<'0'||ch>'9') ch=a[++now];
        while(ch>='0'&&ch<='9') {
            ans=ans*10+ch-'0';
            ch=a[++now];
        }
        return ans;
    }
    
    int work() {
        int ans=0;
        top=0;
        for(int i=1;i<=n;i++) {
            if(b[i][0]=='F') if(s[top]!=-1) {
                int now=4;
                char ch=b[i][now];
                int temp1=0;
                int temp2=0;
                if(b[i][now]=='n') {
                    if(b[i][now+2]=='n') {
                        top++;
                        s[top]=s[top-1];
                        continue;
                    }
                    else {
                        s[++top]=-1;
                        continue;
                    }
                }
                else {
                    while(ch<'0'||ch>'9') ch=b[i][++now];
                    while(ch>='0'&&ch<='9') {
                        temp1=temp1*10+ch-'0';
                        ch=b[i][++now];
                    }
                    if(b[i][now+1]=='n') {
                        top++;
                        s[top]=s[top-1]+1;
                        ans=max(ans,s[top]);
                        continue;
                    }
                    while(ch<'0'||ch>'9') ch=b[i][++now];
                    while(ch>='0'&&ch<='9') {
                        temp2=temp2*10+ch-'0';
                        ch=b[i][++now];
                    }
                    if(temp1>temp2) {
                        s[++top]=-1;
                        continue;
                    }
                    else {
                        top++;
                        s[top]=s[top-1];
                        continue;
                    }
                }
            }
            else s[++top]=-1;
            else top--;
        }
        return ans;
    }
    
    void getal(char *a) {
        char ch=getchar();
        while(ch!='F'&&ch!='E') ch=getchar();
        int now=0;
        while(ch!='
    ') {
            a[now++]=ch;
            ch=getchar();
        }
    }
    
    int main() {
    // 	freopen("testdata.in","r",stdin);
        scanf("%d",&t);
        while(t--) {
            memset(a,0,sizeof(a));
            memset(b,0,sizeof(b));
            err=0;
            scanf("%d%s",&n,a);
            if(a[2]=='n') ans=rfs(a);
            else ans=0;
            for(int i=1;i<=n;i++) getal(b[i]);
            int temp=0;
            for(int i=1;i<=n;i++) {
                if(b[i][0]=='F') temp++;
                else temp--;
                if(temp<0) {
                    err=1;
                    break;
                }
            }
            if(temp) err=1;
            if(err) {
                puts("ERR");
                continue;
            }
            top=0;
            for(int i=1;i<=n;i++) {
                if(b[i][0]=='F') {
                    s[++top]=b[i][2];
                    for(int j=0;j<top;j++) if(s[j]==s[top]) {
                        err=1;
                        break;
                    }
                }
                else top--;
                if(err) break;
            }
            if(err) {
                puts("ERR");
                continue;
            }
            if(work()==ans) puts("Yes");
            else puts("No");
        }
        return 0;
    }
    
    

    T3 逛公园

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    #include<queue>
    using namespace std;
    
    typedef long long ll;
    
    const int Maxn=210000;
    const int Maxm=51;
    
    int to[Maxn],nxt[Maxn],w[Maxn],first[Maxn],tot=1;
    int n,m,k,p,t,x,y,z,sum,s[Maxn],ans;
    int dis[Maxn],bj[Maxn],f[Maxn][Maxm],d[Maxn];
    
    inline void add(int u,int v,int wi) {
    	to[tot]=v;
    	nxt[tot]=first[u];
    	w[tot]=wi;
    	first[u]=tot++;
    }
    
    void spfa() {
    	memset(dis,0x3f,sizeof(dis));
    	memset(bj,0,sizeof(bj));
    	deque<int>q;
    	q.push_back(1);
    	dis[1]=0;
    	bj[1]=1;
    	while(!q.empty()) {
    		int now=q.front();
    		q.pop_front();
    		for(int i=first[now];i;i=nxt[i])
    			if(dis[to[i]]>dis[now]+w[i]) {
    				dis[to[i]]=dis[now]+w[i];
    				if(bj[to[i]]==0) {
    					bj[to[i]]=1;
    					if(q.empty()||dis[q.front()]<dis[to[i]]) q.push_back(to[i]);
    					else q.push_front(to[i]);
    				}
    			}
    		bj[now]=0;
    	}
    }
    
    int main() {
    	scanf("%d",&t);
    	while(t--) {
    		tot=1;sum=0;ans=0;
    		memset(d,0,sizeof(d));
    		memset(first,0,sizeof(first));
    		scanf("%d%d%d%d",&n,&m,&k,&p);
    		for(int i=1;i<=m;i++) {
    			scanf("%d%d%d",&x,&y,&z);
    			add(x,y,z);
    		}
    		spfa();
    		for(int i=1;i<=n;i++)
    			for(int j=first[i];j;j=nxt[j]) {
    				w[j]=w[j]-dis[to[j]]+dis[i];
    				if(w[j]==0) d[to[j]]++;
    			}
    		queue<int>q;
    		for(int i=1;i<=n;i++) if(d[i]==0) q.push(i);
    		while(!q.empty()) {
    			int now=q.front();
    			q.pop();
    			s[++sum]=now;
    			for(int i=first[now];i;i=nxt[i])
    				if(w[i]==0) {
    					d[to[i]]--;
    					if(d[to[i]]==0) q.push(to[i]);
    				}
    		}
    		if(sum!=n) {
    			puts("-1");
    			continue;
    		}
    		memset(f,0,sizeof(f));
    		f[1][0]=1;
    		for(int i=0;i<=k;i++) {
    			for(int j=1;j<=n;j++) if(f[s[j]][i]) {
    				register int yy=f[s[j]][i];
    				for(register int l=first[s[j]];l;l=nxt[l]) {
    					register int xx=i+w[l];
    					if(xx<=k) {
    						f[to[l]][xx]+=yy;
    						f[to[l]][xx]%=p;
    					}
    				}
    			}
    			ans+=f[n][i];
    			ans%=p;
    		}
    		printf("%d
    ",ans);
    	}
    	return 0;
    }
    
    

    Day2

    T1 奶酪

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    using namespace std;
    
    typedef long long ll;
    
    const int Maxn=1100;
    
    int n,t,f[Maxn];
    
    ll r,x[Maxn],y[Maxn],z[Maxn],h;
    
    int find(int x) {
        if(x!=f[x]) f[x]=find(f[x]);
        return f[x];
    }
    
    int main() {
        scanf("%d",&t);
        while(t--) {
            int flag=0;
            scanf("%d%lld%lld",&n,&h,&r);
            ll r2=4*r*r;
            for(int i=0;i<=n;i++) f[i]=i;
            for(int i=1;i<=n;i++) scanf("%lld%lld%lld",&x[i],&y[i],&z[i]);
            for(int i=1;i<=n;i++) if(z[i]<=r) f[i]=0;
            for(int i=1;i<=n;i++)
                for(int j=i+1;j<=n;j++)
                    if(r2>=(x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j])+(z[i]-z[j])*(z[i]-z[j]))
                        if(find(i)!=find(j))
                            f[f[i]]=f[j];
            for(int i=1;i<=n;i++) if(h-z[i]<=r) if(find(i)==find(0)) {
                flag=1;
                puts("Yes");
                break;
            }
            if(flag==0) puts("No");
        }
        return 0;
    }
    
    

    T2 宝藏

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    using namespace std;
    
    typedef long long ll;
    
    const int Maxn=11000;
    const int Maxm=21;
    const ll inf=0x3f3f3f3f3f3f3f;
    
    ll n,m,b[Maxm][Maxm],x,y,z;
    ll f[Maxn],g[Maxn],q1[Maxn],q2[Maxn],q3[Maxn],c[Maxn],t1,t2,t3;
    int bj[Maxn];
    
    int main() {
        scanf("%lld%lld",&n,&m);
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                b[i][j]=inf;
        for(int i=1;i<=m;i++) {
            scanf("%lld%lld%lld",&x,&y,&z);
            b[x][y]=min(b[x][y],z);
            b[y][x]=b[x][y];
        }
        ll ans=inf;
        int end=(1<<n)-1;
        for(int i=0;i<=end;i++) f[i]=inf;
        for(int i=1,temp=1;i<=n;i++,temp<<=1) f[temp]=0,q1[++t1]=temp;
        int cnt=1;
        while(1) {
            t2=0;
            memset(bj,0,sizeof(bj));
            for(int i=0;i<=end;i++) g[i]=inf;
            for(int i=1;i<=t1;i++) {
                t3=0;
                for(int j=1,temp=1;j<=n;j++,temp<<=1)
                    if((q1[i]&temp)==0) {
                        t3++;
                        c[t3]=inf;
                        q3[t3]=temp;
                        int tempt=t3;
                        for(int k=1,tempp=1;k<=n;k++,tempp<<=1)
                            if(q1[i]&tempp)
                                c[t3]=min(c[t3],b[j][k]);
                        for(int i=1;i<tempt;i++) {
                            t3++;
                            q3[t3]=q3[i]|temp;
                            c[t3]=c[tempt]+c[i];
                        }
                    }
                for(int j=1;j<=t3;j++) {
                    int s=q1[i]|q3[j];
                    if(f[q1[i]]+cnt*c[j]<=f[s]) {
                        if(bj[s]==0) {
                            q2[++t2]=s;
                            bj[s]=1;
                        }
                        g[s]=min(g[s],f[q1[i]]+cnt*c[j]);
                    }
                }
            }
            for(int i=0;i<=end;i++) f[i]=g[i];
            t1=t2;
            for(int i=1;i<=t1;i++) q1[i]=q2[i];
            if(t1==0) break;
            ans=min(ans,f[end]);
            cnt++;
        }
        if(n==1) ans=0;
        printf("%lld
    ",ans);
        return 0;
    }
    
    

    T3 列队

    先咕了
    
  • 相关阅读:
    字体下载大宝库:30款好看的免费英文字体
    jQuery Mapael – 呈现动态的矢量地图
    Qt:用 __thread 关键字让每个线程有自己的全局变量
    从C++到Qt(舍弃IDE或qmake、cmake等工具的束缚,尝试通过几个例子)
    C++11(及现代C++风格)和快速迭代式开发
    EventBus + Redis发布订阅模式
    并发、并行和高并发
    Span<T>和Memory<T>
    Lucene.Net做一个简单的搜索引擎-全文索引
    技术架构演变
  • 原文地址:https://www.cnblogs.com/shanxieng/p/9589991.html
Copyright © 2011-2022 走看看