Codeforces_805
A.当l == r时,肯定输出这个数就可以了,当存在两个或两个以上连续的数时,2肯定是最多的数,或最多的数之一。
#include<bits/stdc++.h>
using namespace std; int l,r; int main()
{
ios::sync_with_stdio(false);
cin >> l >> r;
if(l == r) cout << l << endl;
else cout << << endl;
return ;
}
B.aabb这个顺序一定不会有3位的回文串,并且不出现c。
#include<bits/stdc++.h>
using namespace std; int n; int main()
{
ios::sync_with_stdio(false);
cin >> n;
int now = ,cnt = ;
for(int i = ;i <= n;i++)
{
if(now == ) cout << "a";
else cout << "b";
cnt++;
if(cnt == )
{
cnt = ;
now = -now;
}
}
cout << endl; return ;
}
C.1->n->2->n-1...这样的顺序。
#include<bits/stdc++.h>
using namespace std; int n; int main()
{
ios::sync_with_stdio(false);
cin >> n;
cout << (n-)/ << endl;
return ;
}
D.我们发现,每次变换,会把a往后挪动,并且a后面的b翻倍,跑到a前面来。我们从字符串右往左找到每一个a,将其挪到所有b之后。
#include<bits/stdc++.h>
#define MOD 1000000007
using namespace std; string s; int main()
{
ios::sync_with_stdio(false);
cin >> s;
long long ans = ,now = ;
for(int i = s.length()-;i >= ;i--)
{
if(s[i] == 'b') now = (now+)%MOD;
else
{
ans = (ans+now)%MOD;
now = (now+now)%MOD;
}
}
cout << ans << endl;
return ;
}
E.刚开始题意看的一脸懵逼,以为T树是没有用的,但题目中有个条件,含有相同种类的点在树上是连通的,我们用这个特性dfs给G图染色即可,注意未出现的点也要染色。
#include<bits/stdc++.h>
using namespace std; int n,m,cnt = ,color[],a[];
vector<int> v[],s[]; void dfs(int now,int pre)
{
map<int,int> mp;
for(int i = ;i < s[now].size();i++)
{
int t = s[now][i];
if(color[t]) mp[color[t]] = ;
}
int cntt = ;
for(int i = ;i < s[now].size();i++)
{
int t = s[now][i];
if(color[t]) continue;
while(mp.count(++cntt));
color[t] = cntt;
}
for(int i = ;i < v[now].size();i++)
{
int t = v[now][i];
if(t == pre) continue;
dfs(t,now);
}
} int main()
{
ios::sync_with_stdio(false);
cin >> n >> m;
int ans = ;
for(int i = ;i <= n;i++)
{
int x,y,now = ;
cin >> x;
ans = max(x,ans);
for(int j = ;j <= x;j++)
{
cin >> y;
s[i].push_back(y);
}
}
for(int i = ;i < n;i++)
{
int x,y;
cin >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
dfs(,-);
cout << ans << endl;
for(int i = ;i <= m;i++)
{
if(color[i] == ) cout << << " ";
else cout << color[i] << " ";
}
cout << endl;
return ;
}
F.我们求出每个连通子图中每个点的到其他点的最长距离f,每个连通子图的直径d。
对于每一个询问,若两个点在同一个连通子图,则输出-1。
否则,我们考虑两个子图的任意两个点相连后的图的直径为max(fi + fj + 1, max(d1, d2))。
将每个联通子图中所有的d排序后,枚举第一个子图每个点,二分答案为max(d1, d2)的个数,之后就好处理了。
#include<bits/stdc++.h>
using namespace std; int n,m,q,d[] = {},dd[],D[],pre[];
vector<int> v[],vv[];
vector<long long> s[];
map<int,double> mp[]; int findd(int x)
{
return x == pre[x]?x:pre[x] = findd(pre[x]);
} void join(int x,int y)
{
int xx = findd(x),yy = findd(y);
if(xx != yy) pre[xx] = yy;
} void dfs1(int now,int pre)
{
for(int i = ;i < v[now].size();i++)
{
int t = v[now][i];
if(t == pre) continue;
dfs1(t,now);
d[now] = max(d[now],d[t]+);
}
}
void dfs2(int now,int pre)
{
int max1 = -,max2 = -;
dd[now] = d[now];
for(int i = ;i < v[now].size();i++)
{
int t = v[now][i];
if(d[t] > max1)
{
max2 = max1;
max1 = d[t];
}
else max2 = max(max2,d[t]);
}
for(int i = ;i < v[now].size();i++)
{
int t = v[now][i];
if(t == pre) continue;
int x = d[now],y = d[t];
if(d[t] == max1) d[now] = max2+;
else d[now] = max1+;
d[t] = max(d[t],d[now]+);
dfs2(t,now);
d[now] = x;
d[t] = y;
}
}
int main()
{
ios::sync_with_stdio(false);
cin >> n >> m >> q;
for(int i = ;i <= n;i++) pre[i] = i;
while(m--)
{
int x,y;
cin >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
join(x,y);
}
for(int i = ;i <= n;i++)
{
if(findd(i) == i)
{
dfs1(i,-);
dfs2(i,-);
}
}
for(int i = ;i <= n;i++)
{
D[findd(i)] = max(D[findd(i)],dd[i]);
vv[findd(i)].push_back(dd[i]);
}
for(int i = ;i <= n;i++)
{
if(vv[i].size() == ) continue;
sort(vv[i].begin(),vv[i].end());
s[i].resize(vv[i].size());
s[i][] = vv[i][];
for(int j = ;j < s[i].size();j++)
{
s[i][j] = s[i][j-]+vv[i][j];
}
}
while(q--)
{
int xx,yy;
cin >> xx >> yy;
int x = findd(xx),y = findd(yy);
if(x == y)
{
cout << - << endl;
continue;
}
if(vv[x].size() > vv[y].size() || vv[x].size() == vv[y].size() && x > y) swap(x,y);
if(mp[x].count(y))
{
cout << fixed << setprecision() << mp[x][y] << endl;
continue;
}
double ans = ;
int maxx = max(D[x],D[y]);
for(int i = ;i < vv[x].size();i++)
{
int pos = upper_bound(vv[y].begin(),vv[y].end(),maxx-vv[x][i]-)-vv[y].begin();
ans += (long long)pos*maxx;
ans += (long long)(vv[y].size()-pos)*(vv[x][i]+);
ans += (long long)s[y][s[y].size()-];
if(pos > ) ans -= (long long)s[y][pos-];
}
ans = ans/vv[x].size()/vv[y].size();
mp[x][y] = ans;
cout << fixed << setprecision() << ans << endl;
}
return ;
}
Codeforces_805的更多相关文章
随机推荐
- GitHub上的计算机视觉学习资料推荐
9月份将要读研,导师是做cv的,最近学习时找到了不少的计算机视觉的资料,记录一下,同时也分享给需要的朋友 assmdx/ComputerVisionDoc AceCoooool/interview-c ...
- SpringBoot-2.1.1系列二:使用websocket
1.什么是websocket? WebSocket协议是基于TCP的一种新的网络协议.它实现了浏览器与服务器全双工(full-duplex)通信--允许服务器主动发送信息给客户端. 2.为什么需要使用 ...
- 条款03:尽可能使用const
目录 1. 总结 2. const对象 3. const函数返回值和函数参数 4. const成员函数 const成员函数的重要性 bitwise constness logical constnes ...
- TypeScript 源码详细解读(1)总览
TypeScript 由微软在 2012 年 10 月首发,经过几年的发展,已经成为国内外很多前端团队的首选编程语言.前端三大框架中的 Angular 和 Vue 3 也都改用了 TypeScript ...
- 云原生 - 体验Istio的完美入门之旅(一)
作者:justmine 头条号:大数据达摩院 微信公众号:大数据处理系统 创作不易,在满足创作共用版权协议的基础上可以转载,但请以超链接形式注明出处. 为了方便大家阅读,可以关注头条号或微信公众号,后 ...
- P1559 运动员最佳匹配问题 by hyl 天梦
#include<iostream> using namespace std; int n; int maxx[21][21]; int lie[21]; int aa[21]; int ...
- Activiti 手工任务(manualTask)
Activiti 手工任务(manualTask) 作者:Jesai 前言: 手工任务就是一个自动执行的过程.手动任务几乎不在程序中做什么事情,只是在流程的历史中留下一点痕迹,表明流程是走过某些节点的 ...
- Djaingo 随机生成验证码(PIL)
基础: https://www.cnblogs.com/wupeiqi/articles/5812291.html 实例: https://www.cnblogs.com/6324TV/p/88112 ...
- http的异步请求
需要用到的包(包版本应该可能不同): httpcore-4.1.4.jar httpsayncclient-4.0-alpha3.jar httpcore-nio-4.2-alpha3.jar /** ...
- idea初使用之自动编译
原文地址:https://blog.csdn.net/diaomeng11/article/details/73826564/ 因为公司需要,方便使用框架以及代码整合,使用同一开发集成环境idea,因 ...