天梯赛练习题L2(011-015)
L2-011 玩转二叉树
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> PII;
const LL MAXN=1e18;
const LL N=500200,M=4002;
//unordered_map<LL,LL> um[N];
//priority_queue<LL,vector<LL>,greater<LL>> pq;
LL n,pre[N],in[N],ans[N];
void dfs(LL root,LL beg,LL last,LL id)
{
if(beg>last) return;
ans[id]=pre[root];
LL i=beg;
while(i<last&&in[i]!=pre[root])
i++;//找到中序遍历中的根节点
dfs(root+1,beg,i-1,2*id+2);
dfs(root+(i-beg)+1,i+1,last,2*id+1);
}
int main()
{
cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
LL T=1;
//cin>>T;
while(T--)
{
memset(ans,-1,sizeof(ans));
cin>>n;
for(int i=0;i<n;i++)
cin>>in[i];
for(int i=0;i<n;i++)
cin>>pre[i];
dfs(0,0,n-1,0);
//前序遍历的首,中序遍历的首,尾,答案存放的下标
LL idx=0;
for(int i=0;i<10000;i++)
{
if(ans[i]!=-1)
{
if(idx!=n-1)
{
cout<<ans[i]<<" ";
idx++;
}
else
{
cout<<ans[i]<<endl;
break;
}
}
}
}
return 0;
}
L2-012 关于堆的判断
大顶堆
make_heap(q.begin(), q.end(),less<int>());
小顶堆
make_heap(v.begin(),v.end(),greater<LL>());
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> PII;
const LL MAXN=1e18;
const LL N=500200,M=4002;
//unordered_map<LL,LL> um[N];
//priority_queue<LL,vector<LL>,greater<LL>> pq;
vector<LL> v;
LL a[N],idx[N];
map<LL,LL> mp;
int main()
{
cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
LL T=1;
//cin>>T;
while(T--)
{
LL n,m;
cin>>n>>m;
for(int i=1;i<=n;i++)
{
LL x;
cin>>x;
v.push_back(x);
make_heap(v.begin(),v.end(),greater<LL>());
//建立小根堆操作
}
for(int i=0;i<n;i++)
a[i+1]=v[i];
for(int i=1;i<=n;i++)
mp[a[i]]=i;//标记下标位置
while(m--)
{
LL x;
cin>>x;
string s1;
cin>>s1;
if(s1=="is")
{
string s2;
cin>>s2;
if(s2=="the")
{
string s3;
cin>>s3;
if(s3=="root")
{
//x is the root:x是根结点;
//第一个位置
if(mp[x]==1) cout<<"T"<<endl;
else cout<<"F"<<endl;
}
else if(s3=="parent")
{
string s4;
cin>>s4;
LL y;
cin>>y;
//x is the parent of y:x是y的父结点;
//y的位置砍半就是父节点x
if(mp[x]==mp[y]/2) cout<<"T"<<endl;
else cout<<"F"<<endl;
}
}
else if(s2=="a")
{
string s3,s4;
cin>>s3>>s4;
LL y;
cin>>y;
//x is a child of y:x是y的一个子结点。
//x的位置砍半就是父节点y
if(mp[y]==mp[x]/2) cout<<"T"<<endl;
else cout<<"F"<<endl;
}
}
else if(s1=="and")
{
LL y;
cin>>y;
string s2,s3;
cin>>s2>>s3;
//x and y are siblings:x和y是兄弟结点;
//同一个父节点
if(mp[x]/2==mp[y]/2) cout<<"T"<<endl;
else cout<<"F"<<endl;
}
}
}
return 0;
}
L2-013 红色警报
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> PII;
const LL MAXN=1e18;
const LL N=500200,M=4002;
//unordered_map<LL,LL> um[N];
//priority_queue<LL,vector<LL>,greater<LL>> pq;
LL n,m;
LL vis[N],father[N];
struct node
{
LL x,y;
}road[N];
int find(int x)
{
if(father[x]!=x) father[x]=find(father[x]);
return father[x];
}
int main()
{
cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
LL T=1;
//cin>>T;
while(T--)
{
cin>>n>>m;
for(int i=0;i<n;i++)
father[i]=i;
for(int i=1;i<=m;i++)
{
cin>>road[i].x>>road[i].y;
LL fx=find(road[i].x);
LL fy=find(road[i].y);
if(fx!=fy) father[fy]=fx;
}
LL num=0;
for(int i=0;i<n;i++)
if(father[i]==i) num++;//只有自己
LL q;
cin>>q;
while(q--)
{
for(int i=0;i<n;i++) //!!!
father[i]=i;
LL x;
cin>>x;
vis[x]=1;
for(int i=1;i<=m;i++)
{
if(vis[road[i].x]!=1&&vis[road[i].y]!=1)
{
LL fx=find(road[i].x);
LL fy=find(road[i].y);
if(fx!=fy) father[fy]=fx;
}
}
LL sum=0;
for(int i=0;i<n;i++)
{
if(father[i]==i&&vis[i]==0) sum++;
}
if(sum==num||num==sum+1) cout<<"City "<<x<<" is lost."<<endl;
else cout<<"Red Alert: City "<<x<<" is lost!"<<endl;
num=sum;
}
num=0;
for(int i=0;i<n;i++)
{
if(vis[i]==1) num++;
}
if(num==n) cout<<"Game Over."<<endl;
}
return 0;
}
L2-014 列车调度
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> PII;
const LL MAXN=1e18;
const LL N=500200,M=2002;
int main()
{
cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
LL T=1;
//cin>>T;
while(T--)
{
LL n;
cin>>n;
set<LL> s;
for(int i=1;i<=n;i++)
{
LL x;
cin>>x;
if(s.upper_bound(x)!=s.end()) //找大于它并且最接近它的那个,不能为空
{
s.erase(s.upper_bound(x));//把这个删除
}
s.insert(x);//再把这个加进来
}
cout<<s.size()<<endl;
}
return 0;
}
L2-015 互评成绩
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> PII;
const LL MAXN=1e18;
const LL N=500200,M=2002;
double a[N];
vector<LL> v[N];
int main()
{
cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
LL T=1;
//cin>>T;
while(T--)
{
LL n,m,q;
cin>>n>>m>>q;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
LL x;
cin>>x;
v[i].push_back(x);
}
sort(v[i].begin(),v[i].end());
double sum=0;
for(int k=1;k<v[i].size()-1;k++)
sum+=v[i][k];
a[i]=sum/(m-2);
}
sort(a+1,a+1+n);
for(int i=n-q+1;i<=n;i++)
{
if(i!=n) printf("%.3lf ",a[i]);
else printf("%.3lf",a[i]);
}
}
return 0;
}