天梯赛easy题

2

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#define rep(i,j,k) for(register int i=j;i<=k;i++)
#define rrep(i,j,k) for(register int i=j;i>=k;i--)
 
using namespace std; 
const int maxn = 1e5+11;
typedef long long ll;
inline ll read(){
    ll x=0,f=1;register 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 n,hang,tot,an;
char ch,str[555];
void cal(){
	hang=0;tot=0;
	rep(i,1,maxn<<2){
		if(1+i*6+2*i*(i-1)<=n){
			hang=i+1;
			an=i<<1|1;
		}else{
			return;
		}
	}
}
void pri(int cnt){
	rep(i,1,cnt) putchar(' ');
}
void prii(int cnt){
	rep(i,1,cnt) putchar(ch);
	tot+=cnt;
}
int main(){
//	freopen("ttt.txt","w",stdout);
	while(cin>>n){
		cin>>str;
		ch=str[0];
		if(n==0){
			cout<<0<<endl;
			continue;
		}
		if(n<=6){
			cout<<ch<<endl;
			cout<<n-1<<endl;
			continue; 
		}
		cal();
		int j=0;
		int tmp=an;
		rrep(i,hang,1){
			pri(j);j++;
			prii(tmp);tmp-=2;
			putchar('\n');
		}
		tmp=3;j-=2;
		rep(i,2,hang){
			pri(j);j--;
			prii(tmp);tmp+=2;
			putchar('\n');
		}
		cout<<n-tot<<endl;
	}
	return 0;
}

6

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<map>
#include<vector>
#include<cmath> 
#define rep(i,j,k) for(register int i=j;i<=k;i++)
#define rrep(i,j,k) for(register int i=j;i>=k;i--)
using namespace std; 
const int maxn = 1e5+11;
typedef long long ll;
inline ll read(){
    ll x=0,f=1;register 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;
}
vector<int> vec2,vec;
bool prime(ll n){
	int sq=sqrt(0.5+n);
	rep(i,2,sq){
		if(n%i==0) return 0;
	}
	return 1;
}
ll n;
//int dfs(int num,int mul,len){
//	if(num==1) return len;
//}
int main(){
	while(cin>>n){
		if(prime(n)){
			cout<<1<<endl<<n<<endl;
			continue;
		}
		vec.clear();
		int sq=sqrt(0.5+n);
		rep(i,2,sq){
			if(n%i==0){
				vec.push_back(i);
				if(i!=n/i) vec.push_back(n/i);
			}
		}
		if(vec.size()==0) vec.push_back(n);
		sort(vec.begin(),vec.end());
		ll st=0,mx=1;
		for(int i=0;i<vec.size();i++){
			if(vec[i+1]!=vec[i]+1) continue;
			int len=2;ll tmp=vec[i]*vec[i+1];
			if(n%tmp!=0) continue;
			for(int j=i+2;j<vec.size();j++){
				tmp=tmp*vec[j];
				if(vec[j]==vec[j-1]+1&&(n%tmp)==0)len++;
				else break;
			}
			if(len>mx) mx=len,st=i;
		}
		cout<<mx<<endl;
		for(int i=0;i<mx;i++){
			if(i==mx-1) cout<<vec[st+i]<<endl;
			else cout<<vec[st+i]<<"*";
		}
	}
	return 0;
}

25

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<map>
#include<vector>
#include<cmath> 
#include<cstdlib>
#include<string>
#define rep(i,j,k) for(register int i=j;i<=k;i++)
#define rrep(i,j,k) for(register int i=j;i>=k;i--)
using namespace std; 
const int maxn = 1e4+11;
typedef long long ll;
inline ll read(){
    ll x=0,f=1;register 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;
}
char s1[666],s2[666];
char c[]={"0123456789"};
bool check(char ch){
	bool flag=0;
	for(int i=0;i<10;i++){
		if(c[i]==ch)flag=1;
	}
	return flag;
}
int main(){
//	while(cin>>s1>>s2){
	int now=0;char ch;
	while((ch=getchar())!=' ') s1[now++]=ch;s1[now]=0;now=0;
	while((ch=getchar())!=EOF&&(ch!='\n')) s2[now++]=ch;s2[now]=0;now=0;
		bool flag1=0;
		bool flag2=0;
		int ta=strlen(s1),tb=strlen(s2);
		for(int i=0;i<ta;i++){
			if(!check(s1[i]))flag1=1;
		}
		for(int i=0;i<tb;i++){
			if(!check(s2[i]))flag2=1;
		}
		int a,b;
		if(!flag1) a=atoi(s1);
		if(!flag1&&(a<1||a>1000))flag1=1;
		if(!flag2) b=atoi(s2); 
		if(!flag2&&(b<1||b>1000))flag2=1; 
		if(!flag1) cout<<a;
		else cout<<"?";
		cout<<" + ";
		if(!flag2) cout<<b;
		else cout<<"?";
		cout<<" = ";
		if(flag1||flag2) cout<<"?"<<endl;
		else cout<<a+b<<endl; 
//	}
	return 0;
}

39

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<vector>
#include<queue>
#include<stack>
#include<string>
#define rep(i,j,k) for(register int i=j;i<=k;i++)
#define rrep(i,j,k) for(register int i=j;i>=k;i--)
using namespace std;
const int maxn = 1e5+11;
char str[1120];
int n;
char G[1120][1120];
int main(){
	while(cin>>n){
		char ch;
		int now=1; getchar();
		while((ch=getchar())!=EOF&&ch!='\n')str[now++]=ch;
		rep(i,0,1111)rep(j,0,1111) G[i][j]=' ';//!!!
		str[now]=0;
		int len=strlen(str+1);
		rep(i,now,1111) str[i]=' ';
		int R=1,C=1;now=1;
//		if(len<=n){
//			rep(i,1,len) cout<<str[i]<<endl;
//			continue;
//		} what the fuck??
		while(now<=len){
			G[R][C]=str[now++];
			if(now!=len+1){
				if(R+1>n) R=1,C++;
				else R++;
			}
		}
		rep(i,1,n){
			rep(j,1,C){
				cout<<G[i][C-j+1];
			}
			if(i!=n)cout<<endl;
		}
	}
	return 0;
}

2-6

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<vector>
#include<queue>
#include<stack>
#include<string>
#define rep(i,j,k) for(register int i=j;i<=k;i++)
#define rrep(i,j,k) for(register int i=j;i>=k;i--)
using namespace std;
const int maxn = 1e5+11;
int n,h[666],z[666];
int lc[666],rc[666],f[666];
int haxih[666],haxiz[666];
int cnt;
int dfs(int fa,int hl,int hr,int zl,int zr){
//	if(cnt++>20)return -1;
	if(hl>hr||zl>zr) return -1;
	if(hl==-1||zl==-1||hr==-1||zr==-1) return -1;
	if(hr==100||zr==100||hl==100||hr==100) return -1;
	if(hl==hr){
		f[h[hl]]=fa;
		return h[hl];
	}
	int rt=h[hr]; f[rt]=fa;
	int pivot=haxiz[rt];
	int mn1=100,mx1=-1;
	int mn2=100,mx2=-1;
	rep(i,zl,pivot-1) mn1=min(mn1,haxih[z[i]]),mx1=max(mx1,haxih[z[i]]);
	rep(i,pivot+1,zr) mn2=min(mn2,haxih[z[i]]),mx2=max(mx2,haxih[z[i]]);
//	cout<<"lc["<<rt<<"]=dfs("<<rt<<","<<mn1<<","<<mx1<<","<<zl<<","<<pivot-1<<")"<<endl;
//	cout<<"rc["<<rt<<"]=dfs("<<rt<<","<<mn2<<","<<mx2<<","<<pivot<<","<<zr<<")"<<endl;
	lc[rt]=dfs(rt,mn1,mx1,zl,pivot-1);
	rc[rt]=dfs(rt,mn2,mx2,pivot+1,zr);
	
	return rt;
}
int main(){
	while(cin>>n){
		memset(haxiz,-1,sizeof haxiz);
		memset(haxih,-1,sizeof haxih);
		memset(f,-1,sizeof f);
		memset(lc,-1,sizeof lc);
		memset(rc,-1,sizeof rc);
		rep(i,1,n){
			scanf("%d",&h[i]);
			haxih[h[i]]=i;
		}
		rep(i,1,n){
			scanf("%d",&z[i]);
			haxiz[z[i]]=i;
		}
		dfs(-1,1,n,1,n);
//		rep(i,1,n) cout<<i<<" "<<f[i]<<" "<<lc[i]<<" "<<rc[i]<<endl;
		queue<int> que;que.push(h[n]);
		vector<int> vec;
		while(!que.empty()){
			int q=que.front();que.pop();
			vec.push_back(q);
			if(~lc[q]) que.push(lc[q]);
			if(~rc[q]) que.push(rc[q]);
		}
		for(int i=0;i<vec.size();i++){
			if(i!=vec.size()-1) printf("%d ",vec[i]);
			else printf("%d\n",vec[i]);
		}
	}
	return 0;
}

3-11

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
#include<string>
#include<map>
#define rep(i,j,k) for(register int i=j;i<=k;i++)
#define rrep(i,j,k) for(register int i=j;i>=k;i--)
using namespace std; 
const int maxn = 1e3+11;
typedef long long ll;
int to[maxn<<1],nxt[maxn<<1],cost[maxn<<1],head[maxn],tot;
int diren[maxn];
void init(){
	memset(head,-1,sizeof head);
	tot=0;
} 
void add(int u,int v,int w){
	to[tot]=v;cost[tot]=w;nxt[tot]=head[u];head[u]=tot++;
	swap(u,v);
	to[tot]=v;cost[tot]=w;
	nxt[tot]=head[u];head[u]=tot++;
}
int n,k,st,ed;
ll dis[maxn],vis[maxn],pre[maxn];
ll path[maxn],sum[maxn];
void spfa(int s){
	memset(dis,0x3f,sizeof dis);
	memset(pre,-1,sizeof pre);
	memset(vis,0,sizeof vis);
	memset(sum,0,sizeof sum);
	memset(path,0,sizeof path);
	queue<int> que; que.push(s);
	vis[s]=1;dis[s]=0;
	while(!que.empty()){
		int u=que.front(); que.pop(); vis[s]=0;
		for(int i=head[u];~i;i=nxt[i]){
			int v=to[i],w=cost[i];
			if(dis[v]>dis[u]+w){
				dis[v]=dis[u]+w;
				path[v]=path[u]+1;
				sum[v]=sum[u]+diren[v];
				pre[v]=u;
				if(!vis[v]){
					vis[v]=1;
					que.push(v);
				}
			}else if(dis[v]==dis[u]+w){
				if(path[u]+1>path[v]){
					path[v]=path[u]+1;
					sum[v]=sum[u]+diren[v];
					pre[v]=u;
					if(!vis[v]){
						vis[v]=1;
						que.push(v);
					}
				}else if(path[u]+1==path[v]&&sum[u]+diren[v]>sum[v]){
					sum[v]=sum[u]+diren[v];
					path[v]=path[u]+1;
					pre[v]=u;
					if(!vis[v]){
						vis[v]=1;
						que.push(v);
					}
				}
			}
		}
	}
}
int lu;
int dfs(int u,int f,int totd){
	if(u==2) return lu++;
	for(int i=head[u];~i;i=nxt[i]){
		int v=to[i],w=cost[i];
		if(v==f)continue;
		if(dis[v]==totd+w){
			dfs(v,u,dis[v]);
		}
	}
}
string save[maxn];
int main(){
	while(cin>>n>>k){
		init();
		memset(diren,0,sizeof diren);
		rep(i,0,maxn-1) save[i].clear();
		string str,str2;
		map<string,int> mp;
		int cnt=1;
		cin>>str; mp[str]=cnt++;save[cnt-1]=str;
		cin>>str; mp[str]=cnt++;save[cnt-1]=str;
		rep(i,1,n-1){
			int x;
			cin>>str>>x;
			if(mp.count(str)==0){
				mp[str]=cnt++;
				save[cnt-1]=str;
			}
			diren[mp[str]]=x;
		}
		rep(i,1,k){
			int x;cin>>str>>str2>>x;
			add(mp[str],mp[str2],x);
		}
		spfa(1);
		stack<int> stk;
		for(int i=2;~i;i=pre[i]) stk.push(i);
		while(!stk.empty()){
			int u=stk.top();stk.pop();
			if(stk.empty()) printf("%s\n",save[u].c_str());
			else printf("%s->",save[u].c_str());
		}
		lu=0;dfs(1,-1,0);
		cout<<lu<<" "<<dis[2]<<" "<<sum[2]<<endl;
	}
	return 0;
}
// 25/30

3-11

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<ctime>
#include<cstdlib>
#include<vector>
#include<queue>
#include<stack>
#include<string>
#include<map>
#define rep(i,j,k) for(register int i=j;i<=k;i++)
#define rrep(i,j,k) for(register int i=j;i>=k;i--)
using namespace std; 
const int maxn = 1e5+11;
typedef long long ll;
int fa[maxn],lc[maxn],rc[maxn],sz[maxn],wgt[maxn],a[maxn],pos[maxn],n;
unsigned int xjb=2333333;
int Rand(){
    return (xjb=xjb*12345+23333)%(100000);
}
void build(int u,int f){
	if(a[u]<a[f]&&lc[f]==0) fa[u]=f,lc[f]=u;
	if(a[u]<a[f]&&lc[f]!=0) build(u,lc[f]);
	if(a[u]>a[f]&&rc[f]==0) fa[u]=f,rc[f]=u;
	if(a[u]>a[f]&&rc[f]!=0) build(u,rc[f]);
}
void pu(int u){
	int x=0,y=0;
	if(lc[u]) x=wgt[lc[u]];
	if(rc[u]) y=wgt[rc[u]];
	wgt[u]=x+y+sz[u];
}
void update(int u,int f,int v){
	if(a[u]==a[f]) {sz[f]+=v;/*cout<<"update"<<a[f]<<"to"<<sz[f]<<endl;*/}
	if(a[u]<a[f]) update(u,lc[f],v);
	if(a[u]>a[f]) update(u,rc[f],v);
	pu(f); 
}
int findkth(int u,int k){
//	int lw=0;
//	if(lc[u]) lw=wgt[lc[u]];
//	if(k>lw&&k<=lw+sz[u]) return u;
//	else if(k<=lw) return findkth(lc[u],k);
//	else if(k>lc[u]+lw) return findkth(rc[u],k-lw-sz[u]);
	while(1){
		int lw=0; if(lc[u]) lw=wgt[lc[u]];
		if(k<=lw) u=lc[u];
		else if(k>lw&&k<=lw+sz[u]) return u;
		else{
			k-=lw+sz[u];
			u=rc[u];
		}
	}
} 
int main(){
//	freopen("555.txt","w",stdout);
	while(cin>>n){
		rep(i,1,1e5) a[i]=i;
		rep(i,1,1e5){
			int x=rand()%100000+1;
			int y=Rand()%100000+1;
			swap(a[x],a[y]);
		}
		rep(i,1,100000) pos[a[i]]=i;
		rep(i,2,100000) build(i,1);
		char str[555];
		stack<int> stk;
		rep(i,1,n){
			scanf("%s",str);
			if(str[1]=='o'){
				if(stk.empty()){
					cout<<"Invalid"<<endl;
					continue;
				}
				update(pos[stk.top()],1,-1);
				cout<<stk.top()<<endl; 
				stk.pop();
			}
			if(str[1]=='u'){
				int x;cin>>x;
				stk.push(x);
				update(pos[x],1,1);
			}
			if(str[1]=='e'){
				if(stk.size()==0){
					cout<<"Invalid"<<endl;
					continue;
				}
				int ans=findkth(1,(stk.size()+1)/2);
				cout<<a[ans]<<endl;
			}
		}
	}
	return 0;
}

// 17/30 被卡常数了 

46

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#define rep(i,j,k) for(register int i=j;i<=k;i++)
#define rrep(i,j,k) for(register int i=j;i>=k;i--)
#define erep(i,u) for(register int i=head[u];~i;i=nxt[i])
#define iin(a) scanf("%d",&a)
#define lin(a) scanf("%lld",&a)
#define din(a) scanf("%lf",&a)
#define s0(a) scanf("%s",a)
#define s1(a) scanf("%s",a+1)
#define print(a) printf("%lld",(ll)a)
#define enter putchar('\n')
#define blank putchar(' ')
#define println(a) printf("%lld\n",(ll)a)
#define IOS ios::sync_with_stdio(0)
using namespace std;
const int maxn = 3e4+11;
const int oo = 0x3f3f3f3f;
const double eps = 1e-7;
typedef long long ll;
ll read(){
    ll x=0,f=1;register 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 str[maxn];
// void create(int len){
//     for(int i=len;i>=1;i--) str[i]=1;
// }
// vector<int> ERR;
// vector<int> solve(int x,int len){
//     if(len<(int)log2(x)+1)return ERR;
//     vector<int> vec;
//     bool st=1;
//     rrep(i,len,1){
//         if(str[i]%x!=0){
//             str[i-1]+=(str[i]%x)*10;
//         }
//         if(str[i]/x!=0){
//             st=0;
//             vec.push_back(str[i]/x);
//         }else if(str[i]/x==0&&st==0){
//             vec.push_back(str[i]/x);
//         }
//     }
//     if(str[0]==0) return vec;
//     else return ERR;
// }
vector<ll> solve(int x){
    vector<ll> vec;vec.push_back(0);
    bool st=1;
    ll t=1;
    while(1){
        if(t%x==0){
            vec.push_back(t/x);
            vec[0]++;
            return vec;
        }else{
            if(t/x==0){
                if(!st){
                    vec.push_back(t/x);
                }
            }else{
                st=1;
                vec.push_back(t/x);
            }
            t=(t%x)*10+1;
            vec[0]++;
        }
    }
}
int main(){
    int x;
    while(cin>>x){
        if(x==1){
            cout<<1<<" "<<1<<endl;
        }else if(x==11){
            cout<<1<<" "<<2<<endl;
        }else if(x==111){
            cout<<1<<" "<<3<<endl;
        }else{
            vector<ll> vec=solve(x);
            for(int i=1;i<vec.size();i++){
                cout<<vec[i];
            }
            cout<<" "<<vec[0]<<endl;
        }
    }
    return 0;
}

//  17/20 可能模拟出错了一丢丢?
posted @ 2018-03-27 22:13  Caturra  阅读(178)  评论(0)    收藏  举报