Codeforces Round #411 (Div. 2) A-F

时间:2022-09-02 08:20:03

比赛时候切了A-E,fst了A

Standings第一页只有三个人挂了A题,而我就是其中之一,真™开心啊蛤蛤蛤

A. Fake NP

time limit per test  1 second
memory limit per test  256 megabytes
input standard input
output standard output

Tavak and Seyyed are good friends. Seyyed is very funny and he told Tavak to solve the following problem instead of longest-path.

You are given l and r. For all integers from l to r, inclusive, we wrote down all of their integer divisors except 1. Find the integer that we wrote down the maximum number of times.

Solve the problem to show that it's not a NP problem.

Input

The first line contains two integers l and r (2 ≤ l ≤ r ≤ 109).

Output

Print single integer, the integer that appears maximum number of times in the divisors.

If there are multiple answers, print any of them.

Examples
input
19 29
output
2
input
3 6
output
3
Note

Definition of a divisor: https://www.mathsisfun.com/definitions/divisor-of-an-integer-.html

The first example: from 19 to 29 these numbers are divisible by 2: {20, 22, 24, 26, 28}.

The second example: from 3 to 6 these numbers are divisible by 3: {3, 6}.

脑洞题

求被[l,r]范围的数拥有的最多的因数。

如果l==r就输出l,如果l<r就输出2,正确性显然

只要5行就可以AC呢

 #include<iostream>
#include<cstdio>
using namespace std;
int main(){
int l,r;
scanf("%d%d",&l,&r);
if(l==r)printf("%d\n",l);
else printf("2\n");
return ;
}

看到题的时候蠢兮兮枚举sqrt范围内的数,算1~r的贡献减去1~l-1的贡献。

其实这样也可以龟速跑过的吧?但是减的时候减了1~l的贡献,PP以后还自信锁题试图hack别人,这就回天无力了。(PP都是骗人的)

B. 3-palindrome
time limit per test  1 second
memory limit per test  256 megabytes
input standard input
output standard output

In the beginning of the new year Keivan decided to reverse his name. He doesn't like palindromes, so he changed Naviek to Navick.

He is too selfish, so for a given n he wants to obtain a string of n characters, each of which is either 'a', 'b' or 'c', with no palindromes of length 3 appearing in the string as a substring. For example, the strings "abc" and "abca" suit him, while the string "aba" doesn't. He also want the number of letters 'c' in his string to be as little as possible.

Input

The first line contains single integer n (1 ≤ n ≤ 2·105) — the length of the string.

Output

Print the string that satisfies all the constraints.

If there are multiple answers, print any of them.

Examples
input
2
output
aa
input
3
output
bba
Note

A palindrome is a sequence of characters which reads the same backward and forward.

看上去是个贪心递推?假的!

abbaabbaabba无限循环即可

或者"aabb"也可以

 #include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
const int mxn=;
int read(){
int x=,f=;char ch=getchar();
while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>='' && ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
char s[mxn];
int n;
bool check(int i,char c){
if(s[i-]==c)return ;
return ;
}
int main(){
int i,j;
n=read();
for(i=;i<=n;i++){
if(i%==)printf("a");
else if(i%==)printf("b");
else if(i%==)printf("b");
else if(i%==)printf("a");
}
printf("\n");
return ;
}

B

C. Find Amir

time limit per test  1 second
memory limit per test  256 megabytes
input standard input
output standard output

A few years ago Sajjad left his school and register to another one due to security reasons. Now he wishes to find Amir, one of his schoolmates and good friends.

There are n schools numerated from 1 to n. One can travel between each pair of them, to do so, he needs to buy a ticket. The ticker between schools i and j costs Codeforces Round #411 (Div. 2) A-F and can be used multiple times. Help Sajjad to find the minimum cost he needs to pay for tickets to visit all schools. He can start and finish in any school.

Input

The first line contains a single integer n (1 ≤ n ≤ 105) — the number of schools.

Output

Print single integer: the minimum cost of tickets needed to visit all schools.

Examples
input
2
output
0
input
10
output
4
Note

In the first example we can buy a ticket between the schools that costs Codeforces Round #411 (Div. 2) A-F.

贪心 构造

构造题真开心

代价是$ \mod (n+1) $意义下计算的,脑洞一下可以发现1 to n , 2 to n-1 3 to n-2 这样的走法代价为0,而n to 2,n-1 to 3这样的转移代价是1

于是这样转转转就可以了,再考虑到总共有奇数个点的情况,答案为(n+1)/2-1

 #include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
int read(){
int x=,f=;char ch=getchar();
while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>='' && ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int main(){
int i,j;
int n=read();
if(n==)printf("0\n");
else{
printf("%d\n",(n+)/-);
}
return ;
}

C

D. Minimum number of steps

time limit per test  1 second
memory limit per test  256 megabytes
input standard input
output standard output

We have a string of letters 'a' and 'b'. We want to perform some operations on it. On each step we choose one of substrings "ab" in the string and replace it with the string "bba". If we have no "ab" as a substring, our job is done. Print the minimum number of steps we should perform to make our job done modulo 109 + 7.

The string "ab" appears as a substring if there is a letter 'b' right after the letter 'a' somewhere in the string.

Input

The first line contains the initial string consisting of letters 'a' and 'b' only with length from 1 to 106.

Output

Print the minimum number of steps modulo 109 + 7.

Examples
input
ab
output
1
input
aab
output
3
Note

The first example: "ab"  →  "bba".

The second example: "aab"  →  "abba"  →  "bbaba"  →  "bbbbaa".

找规律

发现如果有连续的一串a后面一个b,它们对答案的贡献是$(2^cnt[a])-1$

cnt是要累积的,因为前面的ab换成bba以后,如果后面还有b,仍然对答案有贡献

 #include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#define LL long long
using namespace std;
const int mod=1e9+;
const int mxn=;
int read(){
int x=,f=;char ch=getchar();
while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>='' && ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int ksm(int a,int k){
int res=;
while(k){
if(k&)res=(LL)res*a%mod;
a=(LL)a*a%mod;
k>>=;
}
return res;
}
char s[mxn];
int main(){
int i,j;
scanf("%s",s+);
int n=strlen(s+);
int ans=,cnt=;
for(i=;i<=n;i++){
if(s[i]=='a'){
cnt++;
}
else if(cnt){
ans=((LL)ans+ksm(,cnt)%mod-)%mod;
// cnt--;
}
}
printf("%d\n",ans);
return ;
}

D

E. Ice cream coloring

time limit per test  2 seconds
memory limit per test  256 megabytes
input standard input
output standard output

Isart and Modsart were trying to solve an interesting problem when suddenly Kasra arrived. Breathless, he asked: "Can you solve a problem I'm stuck at all day?"

We have a tree T with n vertices and m types of ice cream numerated from 1 to m. Each vertex i has a set of si types of ice cream. Vertices which have the i-th (1 ≤ i ≤ m) type of ice cream form a connected subgraph. We build a new graph G with m vertices. We put an edge between the v-th and the u-th (1 ≤ u, v ≤ mu ≠ v) vertices in G if and only if there exists a vertex in T that has both the v-th and the u-th types of ice cream in its set. The problem is to paint the vertices of G with minimum possible number of colors in a way that no adjacent vertices have the same color.

Please note that we consider that empty set of vertices form a connected subgraph in this problem.

As usual, Modsart don't like to abandon the previous problem, so Isart wants you to solve the new problem.

Input

The first line contains two integer n and m (1 ≤ n, m ≤ 3·105) — the number of vertices in T and the number of ice cream types.

n lines follow, the i-th of these lines contain single integer si (0 ≤ si ≤ 3·105) and then si distinct integers, each between 1 and m — the types of ice cream in the i-th vertex. The sum of si doesn't exceed 5·105.

n - 1 lines follow. Each of these lines describes an edge of the tree with two integers u and v (1 ≤ u, v ≤ n) — the indexes of connected by this edge vertices.

Output

Print single integer c in the first line — the minimum number of colors to paint the vertices in graph G.

In the second line print m integers, the i-th of which should be the color of the i-th vertex. The colors should be between 1 and c. If there are some answers, print any of them.

Examples
input
3 3
1 1
2 2 3
1 2
1 2
2 3
output
2
1 1 2
input
4 5
0
1 1
1 3
3 2 4 5
2 1
3 2
4 3
output
3
1 1 1 2 3
Note

In the first example the first type of ice cream is present in the first vertex only, so we can color it in any color. The second and the third ice cream are both presented in the second vertex, so we should paint them in different colors.

In the second example the colors of the second, the fourth and the fifth ice cream should obviously be distinct.

贪心 构造 读题

开这题的时候还有1h10min,看了几遍题面没看懂,觉得接下来一个小时都要砸这题上了。

转去看F,觉得如果做不出E那肯定也做不出F。

这时候我注意到我的A题有BUG,但是我已经把它锁了。

实时rank900+,如果不再肝一道题出来就是大幅掉分预定

单纵就是干!斩杀靠砸桶!(大雾)

懵逼了半小时,然后敲了个乱搞代码居然真的乱搞出来了,蛤蛤蛤

这题卡读题啊……看那个我标成红色的句子,大致意思就是说,同种标号的冰淇淋挤在树的连通块上,它是解题的关键。

注意到每个树结点内包含的冰淇淋构成一个完全图,它们的颜色必定不一样。

脑洞一下可以得出最少需要的颜色数是max{一个结点包含的冰淇淋数}

由于上面那个性质,暴力DFS贪心染色不会有问题

 #include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
using namespace std;
const int mxn=;
int read(){
int x=,f=;char ch=getchar();
while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>='' && ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
struct edge{
int v,nxt;
}e[mxn<<];
int hd[mxn],mct=;
void add_edge(int u,int v){
e[++mct].v=v;e[mct].nxt=hd[u];hd[u]=mct;return;
}
int n,m,ans;
int col[mxn];
vector<int>ve[mxn];
bool vis[mxn];
int st[mxn],top=;
void DFS(int u,int fa){
int cnt=,t;top=;
for(int i=;i<ve[u].size();i++){
t=ve[u][i];
if(col[t])st[++top]=t,vis[col[t]]=;
}
for(int i=;i<ve[u].size();i++){
t=ve[u][i];
if(!col[t]){
while(vis[cnt])cnt++;
col[t]=cnt;
cnt++;
}
}
while(top)vis[col[st[top--]]]=;
for(int i=hd[u];i;i=e[i].nxt){
int v=e[i].v;
if(v==fa)continue;
DFS(v,u);
}
return;
}
int main(){
int i,j,s,u,v;
n=read();m=read();
ans=;
for(i=;i<=n;i++){
s=read();
ans=max(ans,s);
for(j=;j<=s;j++)
ve[i].push_back(read());
}
for(i=;i<n;i++){
u=read();v=read();
add_edge(u,v);add_edge(v,u);
}
DFS(,);
printf("%d\n",ans);
for(i=;i<=m;i++)if(!col[i])col[i]=;
for(i=;i<=m;i++){
printf("%d ",col[i]);
}
return ;
}

E

F. Expected diameter of a tree

time limit per test  3 seconds
memory limit per test  256 megabytes
input standard input
output standard output

Pasha is a good student and one of MoJaK's best friends. He always have a problem to think about. Today they had a talk about the following problem.

We have a forest (acyclic undirected graph) with n vertices and m edges. There are q queries we should answer. In each query two vertices v and u are given. Let V be the set of vertices in the connected component of the graph that contains v, and U be the set of vertices in the connected component of the graph that contains u. Let's add an edge between some vertex Codeforces Round #411 (Div. 2) A-F and some vertex in Codeforces Round #411 (Div. 2) A-F and compute the value d of the resulting component. If the resulting component is a tree, the value d is the diameter of the component, and it is equal to -1 otherwise. What is the expected value of d, if we choose vertices a and b from the sets uniformly at random?

Can you help Pasha to solve this problem?

The diameter of the component is the maximum distance among some pair of vertices in the component. The distance between two vertices is the minimum number of edges on some path between the two vertices.

Note that queries don't add edges to the initial forest.

Input

The first line contains three integers nm and q(1 ≤ n, m, q ≤ 105) — the number of vertices, the number of edges in the graph and the number of queries.

Each of the next m lines contains two integers ui and vi (1 ≤ ui, vi ≤ n), that means there is an edge between vertices ui and vi.

It is guaranteed that the given graph is a forest.

Each of the next q lines contains two integers ui and vi (1 ≤ ui, vi ≤ n) — the vertices given in the i-th query.

Output

For each query print the expected value of d as described in the problem statement.

Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6. Let's assume that your answer is a, and the jury's answer is b. The checker program will consider your answer correct, if Codeforces Round #411 (Div. 2) A-F.

Examples
input
3 1 2
1 3
3 1
2 3
output
-1
2.0000000000
input
5 2 3
2 4
4 3
4 2
4 1
2 5
output
-1
2.6666666667
2.6666666667
Note

In the first example the vertices 1 and 3 are in the same component, so the answer for the first query is -1. For the second query there are two options to add the edge: one option is to add the edge 1 - 2, the other one is 2 - 3. In both ways the resulting diameter is 2, so the answer is 2.

In the second example the answer for the first query is obviously -1. The answer for the second query is the average of three cases: for added edges 1 - 2 or 1 - 3 the diameter is 3, and for added edge 1 - 4 the diameter is 2. Thus, the answer is Codeforces Round #411 (Div. 2) A-F.

这题太菜啦!我只用了整整一上午时间就写出来啦!

数学问题 数学期望 树形DP 并查集

用并查集可以判连通块,并且搞出连通块的size

树形DP可以处理出每个点出发能走的最远距离g[](用于计算直径)

询问两个点的时候,如果在同一个连通块里输出-1 (废话)

否则找到这两个点对应的树。假设在两棵树上分别选一点得到u,v,如果g[u]+g[v]+1大于原树直径,那么贡献就是g[u]+g[v]+1,否则贡献是原树直径。

计算每种方案的贡献res, $ans=res/(size[find(u)]*size[find(v)])$

将同一棵树的g[]从小到大排序以后用two pointers扫描可以优化上述过程,做到$ O(n) $计算,或者排序后二分$O(nlogn)$

再加个记忆化就能过了

 #include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<map>
#include<vector>
using namespace std;
const double eps=1e-;
const int mxn=;
int read(){
int x=,f=;char ch=getchar();
while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>='' && ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
struct edge{
int v,nxt;
}e[mxn<<];
int hd[mxn],mct=;
void add_edge(int u,int v){
e[++mct].v=v;e[mct].nxt=hd[u];hd[u]=mct;return;
}
int fa[mxn],sz[mxn];
int vis[mxn];
int find(int x){
return fa[x]==x?x:fa[x]=find(fa[x]);
}
//
vector<int> D[mxn],smm[mxn];
int dis[mxn],g[mxn];
#define amax(a,b) ((a)>(b))?(a):(b)
void DFS(int u,int ff){
for(int i=hd[u],v;i;i=e[i].nxt){
v=e[i].v; if(v==ff)continue;
DFS(v,u);
dis[u]=amax(dis[u],dis[v]+);
}
return;
}
inline void update(int *a,int x){
for(int i=;i<;i++)
if(x>a[i])swap(a[i],x);
return;
}
void DFS2(int u,int ff,int up,vector<int> &now){
int ano[];//通往u的其他子树的最长/次长链
ano[]=ano[]=-;
vis[u]=;
g[u]=amax(dis[u],up);//从u出发能走的最远距离
now.push_back(g[u]);
//
for(int i=hd[u];i;i=e[i].nxt){
int v=e[i].v;if(v==ff)continue;
update(ano,dis[v]);
}
for(int i=hd[u],v;i;i=e[i].nxt){
v=e[i].v;if(v==ff)continue;
int tmp=(ano[]==dis[v])?ano[]:ano[];
DFS2(v,u,amax(tmp+,up+),now);
}
return;
} #undef amax
int n,m,Q;
map<pair<int,int>,double>mp;
void calc(int u,int v){
if(mp.count(make_pair(u,v))){
printf("%.10f\n",mp[make_pair(u,v)]);
return;
}
if(sz[u]<sz[v])swap(u,v);
int L=max(D[u].back(),D[v].back());
double res=0.0;
int K=D[u].size();
for(int i=;i<D[v].size();i++){
int x=upper_bound(D[u].begin(),D[u].end(),L-D[v][i]-)-D[u].begin();
res+=1.0*x*L+(smm[u][K]-smm[u][x])+1.0*(K-x)*(D[v][i]+);
}
res/=D[u].size();res/=D[v].size();
if(u>v)swap(u,v);
mp[make_pair(u,v)]=res;
printf("%.10f\n",res);
return;
}
int main(){
int i,j,u,v;
n=read();m=read();Q=read();
for(i=;i<=n;i++)fa[i]=i,sz[i]=;
for(i=;i<=m;i++){
u=read();v=read();
add_edge(u,v);add_edge(v,u);
u=find(u);v=find(v);
if(u==v)continue;
fa[v]=u;
sz[u]+=sz[v];
}
for(i=;i<=n;i++)
if(find(i)==i)DFS(i,);
for(i=;i<=n;i++){
if(!vis[i]){
int x=find(i);
DFS2(x,,,D[x]);
sort(D[x].begin(),D[x].end());
int ed=D[x].size();
smm[x].resize(ed+);
for(j=;j<=ed;j++)
smm[x][j]=smm[x][j-]+D[x][j-];
}
}
while(Q--){
u=read();v=read();
u=find(u);v=find(v);
if(u==v){
printf("-1\n");
continue;
}
if(u>v)swap(u,v);
calc(u,v);
}
return ;
}

F

Codeforces Round #411 (Div. 2) A-F的更多相关文章

  1. Codeforces Round &num;573 &lpar;Div&period; 1&rpar; 差F

    Codeforces Round #573 (Div. 1) E 题意:二维平面上有 n 个点,你可以放至多 m 条直线使得 (0,0) 与每个点的连线至少与一条直线相交.求原点与所有直线的距离最小值 ...

  2. Codeforces Round &num;541 &lpar;Div&period; 2&rpar; &lpar;A~F&rpar;

    目录 Codeforces 1131 A.Sea Battle B.Draw! C.Birthday D.Gourmet choice(拓扑排序) E.String Multiplication(思路 ...

  3. Codeforces Round &num;532 &lpar;Div&period; 2&rpar;:F&period; Ivan and Burgers(贪心&plus;异或基)

    F. Ivan and Burgers 题目链接:https://codeforces.com/contest/1100/problem/F 题意: 给出n个数,然后有多个询问,每次回答询问所给出的区 ...

  4. Codeforces Round &num;600 &lpar;Div&period; 2&rpar;E F

    题:https://codeforces.com/contest/1253/problem/E 题意:给定n个信号源,俩个参数x和s,x代表这个信号源的位置,s代表这个信号源的波及长度,即这个信号源可 ...

  5. Codeforces Round &num;346 &lpar;Div&period; 2&rpar; E F

    因为很久没有个人认真做题了 昨天晚上开了场虚拟cf来锻炼个人手速 选的是第一次做cf的场 那时候7出3还被hack...之后也没补题 这次做的时候顺便回忆了一下以前比赛的时候是怎么想的 发现经验还是很 ...

  6. Codeforces Round &num;411 &lpar;Div&period; 2&rpar;&lpar;A&comma;B&comma;C&comma;D 四水题&rpar;

    A. Fake NP time limit per test:1 second memory limit per test:256 megabytes input:standard input out ...

  7. Codeforces Round &num;411 &lpar;Div&period; 1&rpar; D&period; Expected diameter of a tree

    题目大意:给出一个森林,每次询问给出u,v,问从u所在连通块中随机选出一个点与v所在连通块中随机选出一个点相连,连出的树的直径期望(不是树输出-1).(n,q<=10^5) 解法:预处理出各连通 ...

  8. Codeforces Round &num;411 &lpar;Div&period; 2&rpar;

    来自FallDream的博客,未经允许,请勿转载,谢谢. 由于人傻又菜 所以这次又滚去div2了  一堆结论题真的可怕 看见E题不是很有思路   然后就去大力搞F题  T了最后一个点 真的绝望   但 ...

  9. Codeforces Round &num;322 &lpar;Div&period; 2&rpar; E F

    E. Kojiro and Furrari 题意说的是 在一条直线上 有n个加油站, 每加一单位体积的汽油 可以走1km 然后每个加油站只有一种类型的汽油,汽油的种类有3种 求从起点出发到达终点要求使 ...

随机推荐

  1. markdown命令语法

    markDown作为一款很好用的文档编写工具,具体的用法如下: # Markdown syntax---**粗体***斜体****粗体加斜体*** > 引用>> 二级引用 # 一级标 ...

  2. system&lowbar;call中断处理过程分析

    本文所有的分析内容都是基于Linux3.18.6内核,鉴于对应不同内核版本,系统调用的实现不相同.若需要分析其他版本内核的系统调用的实现过程,请谨慎参考. system_call函数的功能是用来响应外 ...

  3. BulkSqlCopy 批量导入数据(Ef支持)

    Ado.net对批量数据的支持相信大家都已经非常熟悉.再此就不在多说,就当是给自己备个份,没办法,这个方法太好用了. public static void BulkCreate( string tab ...

  4. 快速判断素数 --Rabin-Miller算法

    以前我在判断素数上一直只会 sqrt(n) 复杂度的方法和所谓的试除法(预处理出sqrt(n)以内的素数,再用它们来除). (当然筛选法对于判断一个数是否是素数复杂度太高) 现在我发现其实还有一种方法 ...

  5. AutoResetEvent信号锁 waitone set 执行一次线程退出 挺不爽的地方

    下边有个 循环调用线程写奇偶数的程序 class TheadTest { //定义一个Stream对象接收打开文件 private FileStream st; //构造方法 public Thead ...

  6. Kali Linux系列教程之OpenVas安装

    Kali Linux系列教程之OpenVas安装 文 /玄魂 目录 Kali Linux系列教程之OpenVas安装 前言 1.  服务器层组件 2.客户层组件 安装过程 Initial setup ...

  7. &lbrack;刷题codeforces&rsqb;650A&period;637A

    650A Watchmen 637A Voting for Photos 点击查看原题 650A又是一个排序去重的问题,一定要注意数据范围用long long ,而且在写计算组合函数的时候注意也要用l ...

  8. spring init

    DN学院讲师招募     Markdown编辑器轻松写博文     TOP 50 CTO坐镇直招     读文章说感想获好礼 通过Spring @PostConstruct 和 @PreDestroy ...

  9. 你不知道的JavaScript--Item15 prototype原型和原型链详解

    用过JavaScript的同学们肯定都对prototype如雷贯耳,但是这究竟是个什么东西却让初学者莫衷一是,只知道函数都会有一个prototype属性,可以为其添加函数供实例访问,其它的就不清楚了, ...

  10. Your Progress As A Programmer Is All Up To You

    Feb 3, 2014 I read a comment on a post on Hacker News where a young programmer said they didn't want ...