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

    题目链接:https://loj.ac/problems/search?keyword=NOIP2013

    D1T1

    (ans=(m*10^k+x)\% n),快速幂求之

    #include<iostream>
    #include<string.h>
    #include<string>
    #include<stdio.h>
    #include<algorithm>
    #include<vector>
    #include<math.h>
    #include<queue>
    #include<set>
    #include<map>
    using namespace std;
    typedef long long ll;
    typedef long double db;
    typedef pair<int,int> pii;
    const int N=10000;
    const db pi=acos(-1.0);
    #define lowbit(x) (x)&(-x)
    #define sqr(x) (x)*(x)
    #define rep(i,a,b) for (register int i=a;i<=b;i++)
    #define per(i,a,b) for (register int i=a;i>=b;i--)
    #define fir first
    #define sec second
    #define mp(a,b) make_pair(a,b)
    #define pb(a) push_back(a)
    #define maxd 998244353
    #define eps 1e-8
    int n,m,k,x;
    
    int read()
    {
        int x=0,f=1;char ch=getchar();
        while ((ch<'0') || (ch>'9')) {if (ch=='-') f=-1;ch=getchar();}
        while ((ch>='0') && (ch<='9')) {x=x*10+(ch-'0');ch=getchar();}
        return x*f;
    }
    
    ll qpow(ll x,int y,int p)
    {
    	ll ans=1;
    	while (y)
    	{
    		if (y&1) ans=ans*x%p;
    		x=x*x%p;y>>=1;
    	}
    	return ans;
    }
    
    int main()
    {
    	n=read();m=read();k=read();x=read();
    	ll ans=m*qpow(10,k,n)%n;
    	ans=(ans+x)%n;
    	printf("%lld",ans);
    	return 0;
    }
    

    D1T2

    原问题等价于最大化(sum_{i=1}^na_ib_i),根据排序不等式可得当({a_i})({b_i})均按照从小到大的顺序排列时两列火柴的距离最小。原序列离散化后构造一个新序列({c_i})表示({b_i})中值为(i)的数将要移动到的位置。于是转化交换({c_i})中的相邻元素以排序,也就是求(c_i)的逆序对。

    #include<iostream>
    #include<string.h>
    #include<string>
    #include<stdio.h>
    #include<algorithm>
    #include<vector>
    #include<math.h>
    #include<queue>
    #include<set>
    #include<map>
    using namespace std;
    typedef long long ll;
    typedef long double db;
    typedef pair<int,int> pii;
    const int N=10000;
    const db pi=acos(-1.0);
    #define lowbit(x) (x)&(-x)
    #define sqr(x) (x)*(x)
    #define rep(i,a,b) for (register int i=a;i<=b;i++)
    #define per(i,a,b) for (register int i=a;i>=b;i--)
    #define fir first
    #define sec second
    #define mp(a,b) make_pair(a,b)
    #define pb(a) push_back(a)
    #define maxd 99999997
    #define eps 1e-8
    int n,a[100100],b[100100],id1[100100],id2[100100],pos[100100],t[100100];
    
    int read()
    {
        int x=0,f=1;char ch=getchar();
        while ((ch<'0') || (ch>'9')) {if (ch=='-') f=-1;ch=getchar();}
        while ((ch>='0') && (ch<='9')) {x=x*10+(ch-'0');ch=getchar();}
        return x*f;
    }
    
    void modify(int p)
    {
    	for (int i=p;i<=n;i+=lowbit(i)) t[i]++;
    }
    
    int query(int p)
    {
    	int ans=0;
    	for (int i=p;i;i-=lowbit(i)) ans+=t[i];
    	return ans;
    }
    
    bool cmp1(int x,int y) {return a[x]<a[y];}
    bool cmp2(int x,int y) {return b[x]<b[y];}
    
    int main()
    {
    	n=read();
    	rep(i,1,n) a[i]=read();
    	rep(i,1,n) b[i]=read();
    	rep(i,1,n) id1[i]=id2[i]=i;
    	sort(id1+1,id1+1+n,cmp1);
    	sort(id2+1,id2+1+n,cmp2);
    	rep(i,1,n) pos[id2[i]]=id1[i];
    	int ans=0;
    	rep(i,1,n)
    	{
    		modify(pos[i]);
    		ans=(ans+i-query(pos[i]))%maxd;
    	}
    	printf("%d",ans);
    	return 0;
    }
    

    D1T3

    符合要求的边一定在图的最大生成树上,倍增维护最小值

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    typedef long double db;
    typedef pair<int,int> pii;
    const int N=10000;
    const db pi=acos(-1.0);
    #define lowbit(x) (x)&(-x)
    #define sqr(x) (x)*(x)
    #define rep(i,a,b) for (register int i=a;i<=b;i++)
    #define per(i,a,b) for (register int i=a;i>=b;i--)
    #define fir first
    #define sec second
    #define mp(a,b) make_pair(a,b)
    #define pb(a) push_back(a)
    #define maxd 998244353
    #define eps 1e-8
    struct node{int to,nxt,cost;}sq[100100];
    int all=0,head[100100];
    int n,m,q,col[10010],fa[10010][16],dis[10010][16],dep[10010];
    struct edgenode{int u,v,w;}edge[50050];
    bool operator<(edgenode p,edgenode q) {return p.w>q.w;}
    
    int read()
    {
        int x=0,f=1;char ch=getchar();
        while ((ch<'0') || (ch>'9')) {if (ch=='-') f=-1;ch=getchar();}
        while ((ch>='0') && (ch<='9')) {x=x*10+(ch-'0');ch=getchar();}
        return x*f;
    }
    
    void add(int u,int v,int w)
    {
    	all++;sq[all].to=v;sq[all].nxt=head[u];sq[all].cost=w;head[u]=all;
    }
    
    int find(int x) {if (col[x]==x) return col[x];col[x]=find(col[x]);return col[x];}
    
    void dfs(int u,int fu)
    {
    	dep[u]=dep[fu]+1;
    	rep(i,1,15) 
    	{
    		fa[u][i]=fa[fa[u][i-1]][i-1];
    		dis[u][i]=min(dis[u][i-1],dis[fa[u][i-1]][i-1]);
    	}
    	for (int i=head[u];i;i=sq[i].nxt)
    	{
    		int v=sq[i].to;
    		if (v==fu) continue;
    		fa[v][0]=u;dis[v][0]=sq[i].cost;
    		dfs(v,u);
    	}
    }
    
    int query(int u,int v)
    {
    	if (dep[u]<dep[v]) swap(u,v);
    	int tmp=dep[u]-dep[v],ans=maxd;
    	per(i,15,0)
    		if ((tmp>>i)&1) {ans=min(ans,dis[u][i]);u=fa[u][i];}
    	if (u==v) return ans;
    	per(i,15,0)
    		if (fa[u][i]!=fa[v][i])
    		{
    			ans=min(ans,min(dis[u][i],dis[v][i]));
    			u=fa[u][i];v=fa[v][i];
    		}
    	ans=min(ans,min(dis[u][0],dis[v][0]));
    	return ans;
    }
    
    int main()
    {
    	n=read();m=read();
    	rep(i,1,m)
    	{
    		edge[i].u=read();edge[i].v=read();edge[i].w=read();
    	}
    	sort(edge+1,edge+1+m);
    	rep(i,1,n) col[i]=i;
    	rep(i,1,m)
    	{
    		int u=edge[i].u,v=edge[i].v,w=edge[i].w;
    		int fx=find(u),fy=find(v);
    		if (fx!=fy)
    		{
    			add(u,v,w);add(v,u,w);
    			col[fx]=fy;
    		}
    	}
    	memset(dis,0x3f,sizeof(dis));
    	rep(i,1,n)
    		if (!dep[i]) dfs(i,0);
    	q=read();
    	while (q--)
    	{
    		int u=read(),v=read();
    		if (find(u)!=find(v)) {puts("-1");continue;}
    		printf("%d
    ",query(u,v));
    	}
    	return 0;
    }
    

    D2T1

    考虑(h_{i-1})(h_i)的影响

    为了覆盖(h_{i-1}),我们有(h_{i-1})个以(i-1)为终点的区间。接下来覆盖(h_i)时,若(h_{i-1}geq h_i),我们可以将之前的某些以(i-1)为终点的区间向右拓展一位以覆盖(h_i)。反之我们则需要新开(h_{i-1}-h_i)个区间来覆盖(h_i)。直接累加即可

    #include<iostream>
    #include<string.h>
    #include<string>
    #include<stdio.h>
    #include<algorithm>
    #include<vector>
    #include<math.h>
    #include<queue>
    #include<set>
    #include<map>
    using namespace std;
    typedef long long ll;
    typedef long double db;
    typedef pair<int,int> pii;
    const int N=10000;
    const db pi=acos(-1.0);
    #define lowbit(x) (x)&(-x)
    #define sqr(x) (x)*(x)
    #define rep(i,a,b) for (register int i=a;i<=b;i++)
    #define per(i,a,b) for (register int i=a;i>=b;i--)
    #define fir first
    #define sec second
    #define mp(a,b) make_pair(a,b)
    #define pb(a) push_back(a)
    #define maxd 998244353
    #define eps 1e-8
    int n,h[100100];
    
    int read()
    {
        int x=0,f=1;char ch=getchar();
        while ((ch<'0') || (ch>'9')) {if (ch=='-') f=-1;ch=getchar();}
        while ((ch>='0') && (ch<='9')) {x=x*10+(ch-'0');ch=getchar();}
        return x*f;
    }
    
    int main()
    {
    	n=read();
    	rep(i,1,n) h[i]=read();
    	int ans=0;
    	rep(i,1,n)
    		if (h[i]>h[i-1]) ans+=(h[i]-h[i-1]);
    	printf("%d",ans);
    	return 0;
    }
    

    D2T2

    蜜汁easy的dp

    (f_{i,0/1})表示(1-i)中,(h_i)作为较大值/较小值的最大合法序列长度。

    (h_i>h_{i-1})时,(f_{i,0}=f_{i-1,1}+1,f_{i,1}=f_{i-1,1})。其它情况同理

    #include<iostream>
    #include<string.h>
    #include<string>
    #include<stdio.h>
    #include<algorithm>
    #include<vector>
    #include<math.h>
    #include<queue>
    #include<set>
    #include<map>
    using namespace std;
    typedef long long ll;
    typedef long double db;
    typedef pair<int,int> pii;
    const int N=10000;
    const db pi=acos(-1.0);
    #define lowbit(x) (x)&(-x)
    #define sqr(x) (x)*(x)
    #define rep(i,a,b) for (register int i=a;i<=b;i++)
    #define per(i,a,b) for (register int i=a;i>=b;i--)
    #define fir first
    #define sec second
    #define mp(a,b) make_pair(a,b)
    #define pb(a) push_back(a)
    #define maxd 998244353
    #define eps 1e-8
    int n,h[2000100],f[2000100][2];
    
    int read()
    {
        int x=0,f=1;char ch=getchar();
        while ((ch<'0') || (ch>'9')) {if (ch=='-') f=-1;ch=getchar();}
        while ((ch>='0') && (ch<='9')) {x=x*10+(ch-'0');ch=getchar();}
        return x*f;
    }
    
    int main()
    {
    	n=read();
    	rep(i,1,n) h[i]=read();
    	f[1][0]=f[1][1]=1;
    	rep(i,2,n)
    	{
    		if (h[i]>h[i-1]) f[i][0]=f[i-1][1]+1;
    		else f[i][0]=f[i-1][0];
    		if (h[i]<h[i-1]) f[i][1]=f[i-1][0]+1;
    		else f[i][1]=f[i-1][1];
    	}
    	//rep(i,1,n) cout << f[i][0] << " " << f[i][1] << endl;
    	printf("%d",max(f[n][0],f[n][1]));
    	return 0;
    }
    

    D2T3

    暴力的话考虑记一个四元组((x_1,y_1,x_2,y_2))表示空格的位置和棋子的位置,大力(bfs)就行了,每次转移空格的位置,当空格的位置和棋子的位置相同时就移动棋子即可。

    这个东西可以通过单组数据,但是无法应对多组数据。同时注意到多组数据对应的网格是相同的,于是可以考虑抠出关键状态建图然后最短路。

    第一个问题是关键状态是什么呢?注意到终止的时候空格一定是在棋子旁边的,并且如果空格跑的离棋子很远也没有什么意义。于是可以将所有的空格在棋子周围的状态作为关键状态,具体的,关键状态可以被刻画为((i,j,k))表示棋子在((i,j))且空格在其的上/下/左/右。

    第二个问题是如何连边?这里还需要考虑的是求边的长度时注意时间。首先显然的是:如果一个棋子在格子的上方,那么可以花一步把它转移到棋子的下方。但是我们发现这么简单的建边无法使这个图联通。接下来还有的是((i,j,k))((i,j,p)(k eq p))连边。这个长度由于((i,j))恒定可以直接对一个点bfs。之后这个图就联通了,可以欢快的跑最短路了

    #include<iostream>
    #include<string.h>
    #include<string>
    #include<stdio.h>
    #include<algorithm>
    #include<vector>
    #include<math.h>
    #include<queue>
    #include<set>
    #include<map>
    using namespace std;
    typedef long long ll;
    typedef long double db;
    typedef pair<int,int> pii;
    const int N=10000;
    const db pi=acos(-1.0);
    #define lowbit(x) (x)&(-x)
    #define sqr(x) (x)*(x)
    #define rep(i,a,b) for (register int i=a;i<=b;i++)
    #define per(i,a,b) for (register int i=a;i>=b;i--)
    #define fir first
    #define sec second
    #define mp(a,b) make_pair(a,b)
    #define pb(a) push_back(a)
    #define maxd 998244353
    #define eps 1e-8
    const int dx[]={1,-1,0,0},dy[]={0,0,1,-1};
    struct node{int x,y,len;};
    struct sqnode{int to,nxt,cost;}sq[200200];
    int all=0,head[4040];
    int n,m,ask,id[31][31][4],dis[4040],a[31][31],tot=0;
    bool in[4040],vis[31][31];
    
    int read()
    {
        int x=0,f=1;char ch=getchar();
        while ((ch<'0') || (ch>'9')) {if (ch=='-') f=-1;ch=getchar();}
        while ((ch>='0') && (ch<='9')) {x=x*10+(ch-'0');ch=getchar();}
        return x*f;
    }
    
    void add(int u,int v,int w)
    {
    	//cout << u << " " << v << " " << w << endl;
    	all++;sq[all].to=v;sq[all].nxt=head[u];sq[all].cost=w;head[u]=all;
    }
    
    int bfs(int sx,int sy,int ex,int ey,int bx,int by)
    {
    	if ((sx==ex) && (sy==ey)) return 0;
    	queue<node> q;
    	while (!q.empty()) q.pop();
    	q.push((node){sx,sy,0});
    	memset(vis,0,sizeof(vis));
    	vis[sx][sy]=1;
    	while (!q.empty())
    	{
    		node now=q.front();q.pop();
    		if ((now.x==ex) && (now.y==ey)) return now.len;
    		rep(i,0,3)
    		{
    			int nx=now.x+dx[i],ny=now.y+dy[i];
    			if ((nx==bx) && (ny==by)) continue;
    			if ((a[nx][ny]) && (!vis[nx][ny]))
    			{
    				vis[nx][ny]=1;
    				if ((nx==ex) && (ny==ey)) return now.len+1;
    				q.push((node){nx,ny,now.len+1});
    			}
    		}
    	}
    	return maxd;
    }
    
    int spfa(int sx,int sy,int ex,int ey,int bx,int by)
    {
    	if ((sx==ex) && (sy==ey)) return 0;
    	queue<int> q;
    	memset(dis,0x3f,sizeof(dis));
    	memset(in,0,sizeof(in));
    	rep(i,0,3)
    		if (id[sx][sy][i]) 
    		{
    			dis[id[sx][sy][i]]=bfs(bx,by,sx+dx[i],sy+dy[i],sx,sy);
    			q.push(id[sx][sy][i]);
    			in[id[sx][sy][i]]=1;
    		}
    	while (!q.empty())
    	{
    		int u=q.front();q.pop();in[u]=0;
    		for (int i=head[u];i;i=sq[i].nxt)
    		{
    			int v=sq[i].to;
    			if (dis[v]>dis[u]+sq[i].cost)
    			{
    				dis[v]=dis[u]+sq[i].cost;
    				if (!in[v]) {q.push(v);in[v]=1;}
    			}
    		}
    	}
    	int ans=maxd;
    	rep(i,0,3) 
    		if (id[ex][ey][i])
    			ans=min(ans,dis[id[ex][ey][i]]);
    	return ans;
    }
    
    void init()
    {
    	n=read();m=read();ask=read();
    	rep(i,1,n) rep(j,1,m) 
    	{
    		a[i][j]=read();
    	}
    	rep(i,1,n) rep(j,1,m)
    		rep(k,0,3)
    			if ((a[i][j]) && (a[i+dx[k]][j+dy[k]]))
    				id[i][j][k]=(++tot);
    	rep(i,1,n)
    		rep(j,1,m)
    			rep(k,0,3)
    				if (id[i][j][k]) 
    					add(id[i][j][k],id[i+dx[k]][j+dy[k]][k^1],1);
    	rep(i,1,n)
    		rep(j,1,m)
    			rep(p,0,3)
    				rep(q,0,3)
    					if ((p!=q) && (id[i][j][p]) && (id[i][j][q]))
    					{
    						int tmp=bfs(i+dx[p],j+dy[p],i+dx[q],j+dy[q],i,j);
    						add(id[i][j][p],id[i][j][q],tmp);
    					}
    }
    
    void work()
    {
    	while (ask--)
    	{
    		int bx=read(),by=read(),sx=read(),sy=read(),ex=read(),ey=read();
    		int ans=spfa(sx,sy,ex,ey,bx,by);
    		if (ans>=maxd) ans=-1;
    		printf("%d
    ",ans);
    	}
    }
    
    int main()
    {
    	init();
    	work();
    	return 0;
    }
    
  • 相关阅读:
    LightOj 1016
    uva 127 "Accordian" Patience 简单模拟
    hdu 1180 诡异的楼梯 BFS + 优先队列
    UVALive 3907 Puzzle AC自动机+DP
    HDU 4001 To Miss Our Children Time DP
    HDU 4000 Fruit Ninja 树状数组
    hdu_1021_Fibonacci Again_201310232237
    hdu_1005_Number Sequence_201310222120
    hdu_1029-Ignatius and the Princess IV_201310180916
    hdu_1020_Encoding_201310172120
  • 原文地址:https://www.cnblogs.com/encodetalker/p/11749733.html
Copyright © 2011-2022 走看看