您的位置:首页 > 编程语言 > Go语言

Codeforces Good Bye 2014 A.B.C.D.

2014-12-31 09:31 92 查看
今天终于升到1900了,2014年的一个好的结束!虽然现在是偶尔打打codeforces,虽然没有了功利的诱惑,但是还是一如既往的喜欢,像蜗牛一样慢慢的前进吧,2015继续加油!!!

话不多说,说说这次的题目.

A题比较简单,就是建图,然后dfs一遍即可得到答案。

B题还是稍微花了点时间的。题意是给一个排列,求能得到的‘’较美丽“的排列,其定义类似于字典序。然后给一个nxn矩阵说明哪些元素可以交换位置。用了一个并查集将可交换的元素都放在一个集合里面,然后在这个集合里面排序,最后得到的排列就是所求排列。

C题也不难。题意有点麻烦,就简单说说,就是”拿书问题“求最小耗费。首先我们可以根据将要拿书的顺序贪心的得到书排放最好的顺序:从上到下就是将要拿书的顺序。然后就可以按照拿书的顺序模拟拿书的过程并计算耗费。

D题是组合数学题。一开始其实就想到了过程,但是调试了很久,还是太久没有比赛啊。。。

题意是给出一个树,每条边上有边权,随机选取三个不同的点,求三点中两两距离之和的和的期望,并且每次修改一个边权,再求出其期望。给出我的思路:一开始我想到的是用树形dp来推一推,但是始终建立不了方程,后来换了一个思维:从边的角度来考虑问题。实际上就是考虑每条边被计算的概率。考虑每条边,一个图取三个点,取法是C(n,3),然后这条边被计算的情况数目是C(a,1)xC(b,2)+C(a,2)xC(b,1)其中a+b=n,并且每种情况该边被计算两次(由于三角形的三边之中肯定有两条边包含该边)。最终将所有边权乘上概率得到期望。这里面遇到的一个问题是如何得到a和b?一开始还在纠结,后来想了想其实一遍dfs就行了。。。

A. New Year Transportation

time limit per test
2 seconds

memory limit per test
256 megabytes

input
standard input

output
standard output

New Year is coming in Line World! In this world, there are n cells numbered by integers from 1 to n,
as a 1 × n board. People live in cells. However, it was hard to move between distinct cells, because of the difficulty of escaping the cell. People wanted
to meet people who live in other cells.

So, user tncks0121 has made a transportation system to move between these cells, to celebrate the New Year. First, he thought of n - 1positive integers a1, a2, ..., an - 1.
For every integer i where 1 ≤ i ≤ n - 1 the
condition 1 ≤ ai ≤ n - i holds.
Next, he made n - 1portals, numbered by integers from 1 to n - 1.
The i-th (1 ≤ i ≤ n - 1) portal connects
cell i and cell (i + ai),
and one can travel from cell i to cell (i + ai) using
the i-th portal. Unfortunately, one cannot use the portal backwards, which means one cannot move from cell(i + ai) to
cell i using the i-th portal. It is easy to see
that because of condition 1 ≤ ai ≤ n - i one
can't leave the Line World using portals.

Currently, I am standing at cell 1, and I want to go to cell t.
However, I don't know whether it is possible to go there. Please determine whether I can go to cell t by only using the construted transportation system.

Input

The first line contains two space-separated integers n (3 ≤ n ≤ 3 × 104)
and t (2 ≤ t ≤ n) — the number of cells,
and the index of the cell which I want to go to.

The second line contains n - 1 space-separated integers a1, a2, ..., an - 1 (1 ≤ ai ≤ n - i).
It is guaranteed, that using the given transportation system, one cannot leave the Line World.

Output

If I can go to cell t using the transportation system, print "YES".
Otherwise, print "NO".

Sample test(s)

input
8 4
1 2 1 2 1 2 1


output
YES


input
8 5
1 2 1 2 1 1 1


output
NO


Note

In the first sample, the visited cells are: 1, 2, 4; so we can successfully visit the cell 4.

In the second sample, the possible cells to visit are: 1, 2, 4, 6, 7, 8; so we can't visit the cell 5,
which we want to visit.

#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<set>
#include<stack>
#include<string>
#include<cstdio>
#include<map>
using namespace std;
#define ll  long long
int n,t,x;
vector<int>v[1000000];
bool b[1000000];
void dfs(int u){
for(int i=0;i<v[u].size();i++){
int vv=v[u][i];
if(!b[vv]){
b[vv]=1;
dfs(vv);
}
}
}
int main()
{
cin>>n>>t;
for(int i=1;i<n;i++)
{
cin>>x;
v[i].push_back(i+x);
}
b[1]=1;
dfs(1);
if(b[t])cout<<"YES\n";
else cout<<"NO\n";
}


B. New Year Permutation

time limit per test
2 seconds

memory limit per test
256 megabytes

input
standard input

output
standard output

User ainta has a permutation p1, p2, ..., pn.
As the New Year is coming, he wants to make his permutation as pretty as possible.

Permutation a1, a2, ..., an is prettier than
permutation b1, b2, ..., bn,
if and only if there exists an integer k (1 ≤ k ≤ n)
where a1 = b1, a2 = b2, ..., ak - 1 = bk - 1 and ak < bk all
holds.

As known, permutation p is so sensitive that it could be only modified by swapping two distinct elements. But swapping two elements is harder than you think.
Given an n × n binary matrix A, user ainta
can swap the values of pi and pj (1 ≤ i, j ≤ n, i ≠ j)
if and only if Ai, j = 1.

Given the permutation p and the matrix A, user ainta
wants to know the prettiest permutation that he can obtain.

Input

The first line contains an integer n (1 ≤ n ≤ 300)
— the size of the permutation p.

The second line contains n space-separated integers p1, p2, ..., pn —
the permutation p that user ainta has. Each integer between 1 and n occurs
exactly once in the given permutation.

Next n lines describe the matrix A. The i-th
line contains n characters '0' or '1'
and describes the i-th row of A. The j-th
character of the i-th lineAi, j is
the element on the intersection of the i-th row and the j-th
column of A. It is guaranteed that, for all integers i, j where 1 ≤ i < j ≤ n, Ai, j = Aj, i holds.
Also, for all integers i where 1 ≤ i ≤ n, Ai, i = 0 holds.

Output

In the first and only line, print n space-separated integers, describing the prettiest permutation that can be obtained.

Sample test(s)

input
7
5 2 4 3 6 7 1
0001001
0000000
0000010
1000001
0000000
0010000
1001000


output
1 2 4 3 6 7 5


input
5
4 2 1 5 3
00100
00011
10010
01101
01010


output
1 2 3 4 5


#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<set>
#include<stack>
#include<string>
#include<cstdio>
#include<map>
using namespace std;
#define ll  long long
int n;
int a[301],fa[301];
int temp[301],p,ans[301];
bool vis[301];
int b[301][301];
char c;
vector<int>v[301];
int Find(int u){
if(u==fa[u])return u;
return fa[u]=Find(fa[u]);
}
void Union(int x,int y){
int fx=Find(x),fy=Find(y);
if(fx==fy)return;
fa[fx]=fy;
}
int main()
{
cin>>n;
for(int i=1;i<=n;i++)cin>>a[i],fa[i]=i;
for(int i=1;i<=n;i++){
getchar();
for(int j=1;j<=n;j++)
cin>>c,b[i][j]=c-'0';
}
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)if(b[i][j]){
Union(i,j);
}
for(int i=1;i<=n;i++){
int f=Find(i);
v[f].push_back(i);
vis[f]=1;
}
for(int i=1;i<=n;i++)
if(vis[i]){
p=0;
for(int j=0;j<v[i].size();j++)
temp[p++]=a[v[i][j]];
sort(temp,temp+p);
for(int j=0;j<v[i].size();j++)
ans[v[i][j]]=temp[j];
}
for(int i=1;i<=n;i++)
cout<<ans[i]<<" ";
cout<<endl;
}


C. New Year Book Reading

time limit per test
2 seconds

memory limit per test
256 megabytes

input
standard input

output
standard output

New Year is coming, and Jaehyun decided to read many books during 2015, unlike this year. He has n books numbered by integers from 1 to n.
The weight of the i-th (1 ≤ i ≤ n) book
is wi.

As Jaehyun's house is not large enough to have a bookshelf, he keeps the n books by stacking them vertically. When he wants to read a certain book x,
he follows the steps described below.

He lifts all the books above book x.

He pushes book x out of the stack.

He puts down the lifted books without changing their order.

After reading book x, he puts book x on the top of
the stack.



He decided to read books for m days. In the j-th
(1 ≤ j ≤ m) day, he will read the book that is numbered with integer bj (1 ≤ bj ≤ n).
To read the book, he has to use the process described in the paragraph above. It is possible that he decides to re-read the same book several times.

After making this plan, he realized that the total weight of books he should lift during m days
would be too heavy. So, he decided to change the order of the stacked books before the New Year comes, and minimize the total weight. You may assume that books can be stacked in any possible order. Note that book that he is going to read on certain step isn't
considered as lifted on that step. Can you help him?

Input

The first line contains two space-separated integers n (2 ≤ n ≤ 500)
and m (1 ≤ m ≤ 1000) — the number of books, and
the number of days for which Jaehyun would read books.

The second line contains n space-separated integers w1, w2, ..., wn (1 ≤ wi ≤ 100)
— the weight of each book.

The third line contains m space separated integers b1, b2, ..., bm (1 ≤ bj ≤ n)
— the order of books that he would read. Note that he can read the same book more than once.

Output

Print the minimum total weight of books he should lift, which can be achieved by rearranging the order of stacked books.

Sample test(s)

input
3 5
1 2 3
1 3 2 3 1


output
12


Note

Here's a picture depicting the example. Each vertical column presents the stacked books.



#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<set>
#include<stack>
#include<string>
#include<cstdio>
#include<map>
using namespace std;
#define ll  long long
int n,m;
int a[501],b[1001];
int p[501];
bool f[501];
int main()
{
cin>>n>>m;
for(int i=1;i<=n;i++)cin>>a[i];
int id=0,ans=0;
for(int j=1;j<=m;j++){
cin>>b[j];
if(!f[b[j]])f[b[j]]=1,p[++id]=b[j];
}
for(int i=1;i<=m;i++){
int j;
for(j=1;j<=id;j++){
if(p[j]==b[i])break;
ans+=a[p[j]];
}
for(int k=j;k>=2;k--)
p[k]=p[k-1];
p[1]=b[i];
}
cout<<ans<<endl;
}


D. New Year Santa Network

time limit per test
2 seconds

memory limit per test
256 megabytes

input
standard input

output
standard output

New Year is coming in Tree World! In this world, as the name implies, there are n cities connected by n - 1 roads,
and for any two distinct cities there always exists a path between them. The cities are numbered by integers from 1 to n, and the roads are numbered by
integers from 1 to n - 1. Let's define d(u, v) as
total length of roads on the path between city u and city v.

As an annual event, people in Tree World repairs exactly one road per year. As a result, the length of one road decreases. It is already known that in the i-th
year, the length of the ri-th
road is going to become wi,
which is shorter than its length before. Assume that the current year is year 1.

Three Santas are planning to give presents annually to all the children in Tree World. In order to do that, they need some preparation, so they are going to choose three distinct cities c1, c2, c3 and
make exactly one warehouse in each city. The k-th (1 ≤ k ≤ 3)
Santa will take charge of the warehouse in city ck.

It is really boring for the three Santas to keep a warehouse alone. So, they decided to build an only-for-Santa network! The cost needed to build this network equals to d(c1, c2) + d(c2, c3) + d(c3, c1) dollars.
Santas are too busy to find the best place, so they decided to choose c1, c2, c3randomly
uniformly over all triples of distinct numbers from 1 to n.
Santas would like to know the expected value of the cost needed to build the network.

However, as mentioned, each year, the length of exactly one road decreases. So, the Santas want to calculate the expected after each length change. Help them to calculate the value.

Input

The first line contains an integer n (3 ≤ n ≤ 105)
— the number of cities in Tree World.

Next n - 1 lines describe the roads. The i-th line
of them (1 ≤ i ≤ n - 1) contains three space-separated integers ai, bi, li (1 ≤ ai, bi ≤ n, ai ≠ bi,1 ≤ li ≤ 103),
denoting that the i-th road connects cities ai and bi,
and the length of i-th road is li.

The next line contains an integer q (1 ≤ q ≤ 105)
— the number of road length changes.

Next q lines describe the length changes. The j-th
line of them (1 ≤ j ≤ q) contains two space-separated integers rj, wj (1 ≤ rj ≤ n - 1, 1 ≤ wj ≤ 103).
It means that in the j-th repair, the length of the rj-th
road becomes wj.
It is guaranteed that wj is
smaller than the current length of the rj-th
road. The same road can be repaired several times.

Output

Output q numbers. For each given change, print a line containing the expected cost needed to build the network in Tree World. The answer will be considered
correct if its absolute and relative error doesn't exceed 10 - 6.

Sample test(s)

input
3
2 3 5
1 3 3
5
1 4
2 2
1 2
2 1
1 1


output
14.0000000000
12.0000000000
8.0000000000
6.0000000000
4.0000000000


input
6
1 5 3
5 3 2
6 1 7
1 4 4
5 2 3
5
1 2
2 1
3 5
4 1
5 2


output
19.6000000000
18.6000000000
16.6000000000
13.6000000000
12.6000000000


Note

Consider the first sample. There are 6 triples: (1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1). Because n = 3,
the cost needed to build the network is always d(1, 2) + d(2, 3) + d(3, 1) for all the triples. So, the expected cost equals to d(1, 2) + d(2, 3) + d(3, 1).

#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<set>
#include<stack>
#include<string>
#include<cstdio>
#include<map>
using namespace std;
#define ll  long long
struct edge{
int x,y,w;
}nd[100009];
vector<int>ed[100009];
int n,m;
int num[100009];
int dfs(int u,int fa){
int ans=1;
for(int i=0;i<ed[u].size();i++){
edge e=nd[ed[u][i]];
int v;
if(e.x==u)v=e.y;else v=e.x;
if(v==fa)continue;
int f=dfs(v,u);
num[ed[u][i]]=f;
ans+=f;
}
return ans;
}
double cnt(int x){
if(x==1||x==n-1)return 6.0/n;
return 6.0*x*(n-x)/n/(n-1);
}
int main()
{
cin>>n;
for(int i=1;i<n;i++){
cin>>nd[i].x>>nd[i].y>>nd[i].w;
ed[nd[i].x].push_back(i);
ed[nd[i].y].push_back(i);
}
dfs(1,-1);
double an=0;
for(int i=1;i<n;i++){
an+=cnt(num[i])*nd[i].w;
}
cin>>m;
int p,q;
for(int i=1;i<=m;i++){
cin>>p>>q;
an=an+cnt(num[p])*(q-nd[p].w);
nd[p].w=q;
printf("%.10lf\n",an);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: