天梯赛练习题L2(021-025)
L2-021 点赞狂魔
如果有并列,则输出标签出现次数平均值最小的那个
#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> a[N];
//priority_queue<LL> pq;
//priority_queue<LL,vector<LL>,greater<LL>> pq2;
struct node
{
string s;
LL sum;
double avg;
}a[N];
bool cmp(node l,node r)
{
if(l.sum!=r.sum) return l.sum>r.sum;
else return l.avg<r.avg;
}
int main()
{
cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
LL T=1;
//cin>>T;
while(T--)
{
LL n;
cin>>n;
map<LL,LL> mp;
for(int i=1;i<=n;i++)
{
cin>>a[i].s;
LL op;
cin>>op;
mp.clear();
for(int j=1;j<=op;j++)
{
LL x;
cin>>x;
mp[x]++;
if(mp[x]==1) a[i].sum++;
}
a[i].avg=(double)op/a[i].sum;
}
sort(a+1,a+1+n,cmp);
if(n==0) cout<<"- - -";
else if(n==1) cout<<a[1].s<<" - -";
else if(n==2) cout<<a[1].s<<" "<<a[2].s<<" -";
else if(n>=3) cout<<a[1].s<<" "<<a[2].s<<" "<<a[3].s;
}
return 0;
}
L2-022 重排链表
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> PII;
const LL MAXN=1e18;
const LL N=5002000,M=4002;
//unordered_map<LL,LL> a[N];
//priority_queue<LL> pq;
//priority_queue<LL,vector<LL>,greater<LL>> pq2;
LL address,e[N],ne[N];
int main()
{
cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
LL T=1;
//cin>>T;
while(T--)
{
LL head,n;
cin>>head>>n;
for(int i=1;i<=n;i++)
{
cin>>address>>e[address]>>ne[address];
}
deque<LL> d;
for(int i=head;i!=-1;i=ne[i])
{
d.push_back(i);
//cout<<i<<" "<<e[i]<<" "<<ne[i]<<endl;
}
vector<LL> v;
while(d.size())
{
v.push_back(d.back());
d.pop_back();
if(d.size()==0) break;
v.push_back(d.front());
d.pop_front();
}
for(int i=0;i<v.size();i++)
{
printf("%05d %d ",v[i],e[v[i]]);
if(i==v.size()-1) printf("-1\n");
else printf("%05d\n",v[i+1]);
}
}
return 0;
}
L2-023 图着色问题
注意颜色一定只能存在k种,不可以多也可以少
#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> a[N];
//priority_queue<LL> pq;
//priority_queue<LL,vector<LL>,greater<LL>> pq2;
LL n,m,k,q;
LL st[N];
vector<LL> v[N];
LL check()
{
for(int i=1;i<=n;i++)
{
for(int j=0;j<v[i].size();j++)
if(st[i]==st[v[i][j]]) return 0;
}
return 1;
}
int main()
{
//cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
LL T=1;
//cin>>T;
while(T--)
{
cin>>n>>m>>k;
for(int i=1;i<=m;i++)
{
LL x,y;
cin>>x>>y;
v[x].push_back(y);
v[y].push_back(x);
}
map<LL,LL> mp;
cin>>q;
while(q--)
{
mp.clear();
LL sum=0;
for(int i=1;i<=n;i++)
{
LL x;
cin>>x;
mp[x]++;
if(mp[x]==1) sum++;
st[i]=x;
}
if(sum!=k) cout<<"No"<<endl;
else
{
if(check()==1) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
}
}
return 0;
}
L2-024 部落
#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> a[N];
//priority_queue<LL> pq;
//priority_queue<LL,vector<LL>,greater<LL>> pq2;
LL n,father[N];
LL find(LL x)
{
if(father[x]!=x) father[x]=find(father[x]);
return father[x];
}
void init()
{
for(LL i=0;i<=10010;i++)
father[i]=i;
}
void merge(LL x,LL head)
{
LL fx=find(x),fh=find(head);
if(fx!=fh) father[fx]=fh;
}
int main()
{
//cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
LL T=1;
//cin>>T;
while(T--)
{
init();
cin>>n;
map<LL,LL> mp;
LL sum=0;
for(LL i=1;i<=n;i++)
{
LL op;
cin>>op;
LL head;
cin>>head;
mp[head]++;
if(mp[head]==1) sum++;
for(LL j=2;j<=op;j++)
{
LL x;
cin>>x;
mp[x]++;
if(mp[x]==1) sum++;
merge(x,head);
}
}
LL ans=0;
map<LL,LL> pm;
for(LL i=0;i<=10010;i++)
{
if(mp[i]!=0)
{
if(pm[find(i)]==0) pm[find(i)]++,ans++;
else ;
}
}
cout<<sum<<" "<<ans<<endl;
LL q;
cin>>q;
while(q--)
{
LL a,b;
cin>>a>>b;
if(find(a)!=find(b)) cout<<"N"<<endl;
else cout<<"Y"<<endl;
}
}
return 0;
}
L2-025 分而治之
#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> a[N];
//priority_queue<LL> pq;
//priority_queue<LL,vector<LL>,greater<LL>> pq2;
LL n,m,q,k;
LL vis[N];
vector<LL> v[N];
LL check()
{
for(int i=1;i<=n;i++)
{
for(int j=0;j<v[i].size();j++)
{
//这个地方没有被攻下并且叶子节点没有被标记
//就说明这两个点还是处于连线状态
if(vis[i]==0&&vis[v[i][j]]==0) return 1;
}
}
return 0;
}
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=1;i<=m;i++)
{
LL x,y;
cin>>x>>y;
v[x].push_back(y);
v[y].push_back(x);
}
cin>>q;
while(q--)
{
memset(vis,0,sizeof vis);
cin>>k;
while(k--)
{
LL x;
cin>>x;
vis[x]=1;//标记地点
}
if(check()==1) cout<<"NO"<<endl;
else cout<<"YES"<<endl;
}
}
return 0;
}