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的更多相关文章

随机推荐

  1. IDEA错误: 找不到或无法加载主类

    错误: 找不到或无法加载主类 idea本身缓存问题 解决:清理缓存重启IDEA file-->invalidate Cache/restart 之后再重新build.

  2. springboot整合apache ftpserver详细教程(看这一篇就够了)

    原创不易,如需转载,请注明出处https://www.cnblogs.com/baixianlong/p/12192425.html,否则将追究法律责任!!! 一.Apache ftpserver相关 ...

  3. 随机算法 - Miller_Rabin pollard_rho

    #include<stdio.h> #include<string.h> #include<stdlib.h> #include<time.h> #in ...

  4. play framework 相关

    1.下载 官网下载解压,安装有jkd即可使用 2.helloworld $ activator new my-first-app play-java https://www.playframework ...

  5. C++数值计算

    1.序 (1)程序设计分两种: 1.结构化设计(面向过程)——分解算法为模块,将算法的步骤分解为模块. 2.面向对象程序设计——主要是“类”与“对象”. (2)进制的转换 1.二进制转十进制 整数部分 ...

  6. LeetCode动画 | 1038. 从二叉搜索树到更大和树

    今天分享一个LeetCode题,题号是1038,标题是:从二分搜索树到更大和数. 题目描述 给出二叉搜索树的根节点,该二叉树的节点值各不相同,修改二叉树,使每个节点 node 的新值等于原树中大于或等 ...

  7. [bzoj3527] [洛谷P3338] [Zjoi2014]力

    Description 给出n个数qi,给出Fj的定义如下: \[ F_j=\sum\limits_{i<j} \frac{q_iq_j}{(i-j)^2} - \sum\limits_{i&g ...

  8. Qt Installer Framework翻译(4)

    教程:创建安装程序 本教程描述如何为一个小项目创建一个简单的安装程序: 本节描述创建安装程序所必须完成的步骤: 创建一个包文件夹,其中将包含所有配置文件和可安装的包. 创建一个配置文件,其中包含有关如 ...

  9. 4、python基础语法

    前言:本文主要介绍python的一些基础语法,包括标识符的定义.行和缩进.引号和注释.输入输出.变量的定义. 一.标识符 1.凡是我们自己取的名字,都是标识符. 2.在Python里,标识符由字母.下 ...

  10. Irrelevant Elements UVA-1635 (二项式定理)

    vjudge链接 原题链接 乍一看似乎没什么思路,但是写几个简单的例子之后规律就变得很明显. 比如当 n=5 时,每一步计算后的结果如下: a1 a1+a2 a1+2a2+a3 a1+3a2+3a3+ ...