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. zookeeper作配置中心(存储支付信息)

    zookeeper作配置中心(存储敏感信息) 前提:最近在项目中需要用到支付接口,支付宝或者微信支付,根据官方文档,需要配置一些诸如notify-url或者app-private-key等信息,这些信 ...

  2. java中高级并发SPI机制

    Java SPI 实际上是“基于接口的编程+策略模式+配置文件”组合实现的动态加载机制. 适用于:调用者根据实际使用需要,启用.扩展.或者替换框架的实现策略. 要使用Java SPI,需要遵循如下约定 ...

  3. 小白学 Python 爬虫(36):爬虫框架 Scrapy 入门基础(四) Downloader Middleware

    人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Li ...

  4. 欧拉-拉格朗日方程 The Euler-Lagrange Equation

    在 paper: Bounded Biharmonic Weights for Real-Time Deformation 中第一次接触到 Euler-Lagrange 方程,简单记录一下. 泛函的定 ...

  5. C#反射与特性(七):自定义特性以及应用

    目录 1,属性字段的赋值和读值 2,自定义特性和特性查找 2.1 特性规范和自定义特性 2.2 检索特性 3,设计一个数据验证工具 3.1 定义抽象验证特性类 3.2 实现多个自定义验证特性 3.3 ...

  6. css label两端对齐

    上面这种效果很常见,实现的代码如下: html部分 <ul> <li class="detail_item"> <span class="d ...

  7. 【转】在Eclipse下搭建Android开发环境教程

    本文将全程演示Android开发环境的搭建过程,无需配置环境变量.所有软件都是写该文章时最新版本,希望大家喜欢.   一 相关下载 三 Eclipse配置 (1)Java JDK下载 1 安装andr ...

  8. 在浏览器窗口内移动的div

    ------------今天研究了一个最简单的屏保效果----------- 效果图如下:效果很神奇,就是这个div在浏览器窗口不断的灵活移动 代码却很简单 <!DOCTYPE html> ...

  9. Java 循环队列

    传统数组实现的队列有缺陷,当多次入队出队后,队头指针会后移,当队尾指针达到数组末尾时,会提示队列已满,导致数组前部分空间被浪费.如果当队尾和队头指针到达数组末尾时能从数组[0]继续添加数据,可以提升数 ...

  10. 两个大数相乘 - 高精度FFT

    HDU 1402 A * B Problem Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...