Good Bye 2014(codeforces)

来源:互联网 发布:软件项目管理规范 编辑:程序博客网 时间:2024/05/21 06:40

这次倒序写题解。

D. New Year Santa Network

New Year is coming in Tree World! In this world, as the name implies, there aren 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 ton, and the roads are numbered by integers from1 to n - 1. Let's defined(u, v) as total length of roads on the path between cityu 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 thei-th year, the length of the ri-th road is going to becomewi, which is shorter than its length before. Assume that the current year is year1.

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 citiesc1, c2, c3 and make exactly one warehouse in each city. Thek-th (1 ≤ k ≤ 3) Santa will take charge of the warehouse in cityck.

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 tod(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, c3 randomly uniformly over all triples of distinct numbers from1 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. Thei-th line of them (1 ≤ i ≤ n - 1) contains three space-separated integersai,bi,li (1 ≤ ai, bi ≤ n,ai ≠ bi,1 ≤ li ≤ 103), denoting that thei-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. Thej-th line of them (1 ≤ j ≤ q) contains two space-separated integersrj,wj (1 ≤ rj ≤ n - 1,1 ≤ wj ≤ 103). It means that in thej-th repair, the length of the rj-th road becomes wj. It is guaranteed thatwj is smaller than the current length of therj-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 exceed10 - 6.

Sample test(s)
Input
32 3 51 3 351 42 21 22 11 1
Output
14.000000000012.00000000008.00000000006.00000000004.0000000000
Input
61 5 35 3 26 1 71 4 45 2 351 22 13 54 15 2
Output
19.600000000018.600000000016.600000000013.600000000012.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). Becausen = 3, the cost needed to build the network is alwaysd(1, 2) + d(2, 3) + d(3, 1) for all the triples. So, the expected cost equals tod(1, 2) + d(2, 3) + d(3, 1).


思路:手先预处理出,对于每条边两边的点分别有多少,dfs就可以了,假设对于第i条边,两边的点分别为x,y,那么这条边在总的距离中的贡献就是(x*(x-1)/2*y+y(y-1)/2*x)*2

对于每次的更新,只需要在原来的基础上减去就可以了

#include<iostream>#include<cstdio>#include<string>#include<cstring>#include<vector>#include<cmath>#include<queue>#include<stack>#include<map>#include<set>#include<algorithm>using namespace std;const int maxn=100010;struct node{    int u,v,id;    int left,right;    int len;    node(){}    node(int _u,int _v,int _id,int _left,int _right,int _len)    {        u=_u,v=_v;        id=_id;        left=_left;        right=_right;        len=_len;    }}edge[maxn];int cnt[maxn];vector<pair<int,int> > g[maxn];int N,Q;void dfs(int u,int fa){    int len=g[u].size();    for(int i=0;i<len;i++)    {        int v=g[u][i].first;        if(v==fa)continue;        dfs(v,u);        cnt[u]+=cnt[v];    }    cnt[u]+=1;}void dfs1(int u,int fa){    int len=g[u].size();    for(int i=0;i<len;i++)    {        int v=g[u][i].first;        if(v==fa)continue;        node &tmp=edge[g[u][i].second];        dfs1(v,u);        tmp.left+=cnt[v];        tmp.right+=N-cnt[v];    }}int main(){    scanf("%d",&N);    memset(cnt,0,sizeof(cnt));    for(int i=1;i<N;i++)    {        int a,b,l;        scanf("%d%d%d",&a,&b,&l);        edge[i]=node(a,b,i,0,0,l);        g[a].push_back(make_pair(b,i));        g[b].push_back(make_pair(a,i));    }    dfs(1,0);    dfs1(1,0);    double ans=0;    double C=double(N)*(N-1)*(N-2)/6;    for(int i=1;i<N;i++)    {        double a=edge[i].left,b=edge[i].right;        ans+=(a*(a-1)/2*b+b*(b-1)/2*a)*2*edge[i].len/C;    }    scanf("%d",&Q);    while(Q--)    {        int x,y;        scanf("%d%d",&x,&y);        double a=edge[x].left,b=edge[x].right;        if(edge[x].len==y){printf("%lf\n",ans);continue;}        ans-=(a*(a-1)/2*b+b*(b-1)/2*a)*2*(edge[x].len-y)/C;        edge[x].len=y;        printf("%.10lf\n",ans);    }    return 0;}

C. New Year Book Reading

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

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 bookx, he follows the steps described below.

  1. He lifts all the books above book x.
  2. He pushes book x out of the stack.
  3. He puts down the lifted books without changing their order.
  4. After reading book x, he puts book x on the top of the stack.

He decided to read books for m days. In thej-th (1 ≤ j ≤ m) day, he will read the book that is numbered with integerbj (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) andm (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 integersw1, w2, ..., wn (1 ≤ wi ≤ 100) — the weight of each book.

The third line contains m space separated integersb1, 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 51 2 31 3 2 3 1
Output
12
Note

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


其实这个题感觉还挺好的。

对于当前出现的书,只需要找到他先前出现的离他最近的地方,这之间的就是需要加上的,扫一遍就可以了

#include<iostream>#include<cstdio>#include<string>#include<cstring>#include<vector>#include<cmath>#include<queue>#include<stack>#include<map>#include<set>#include<algorithm>using namespace std;const int maxn=1010;int N,M;int w[maxn];int num[maxn];int vis[maxn];int a[maxn];int main(){    while(scanf("%d%d",&N,&M)!=EOF)    {        for(int i=1;i<=N;i++)scanf("%d",&w[i]);        for(int i=1;i<=M;i++)scanf("%d",&a[i]);        int ans=0;        for(int i=1;i<=M;i++)        {            memset(vis,0,sizeof(vis));            for(int j=i-1;j>0;j--)                if(a[j]==a[i])break;                else if(!vis[a[j]])                {                    vis[a[j]]=1;                    ans+=w[a[j]];                }        }        printf("%d\n",ans);    }    return 0;}

B. New Year Permutation

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 isprettier than permutation b1, b2, ..., bn, if and only if there exists an integerk (1 ≤ k ≤ n) wherea1 = 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 ann × n binary matrix A, user ainta can swap the values of pi andpj (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 permutationp.

The second line contains n space-separated integersp1, p2, ..., pn — the permutationp that user ainta has. Each integer between1 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 thei-th row of A. Thej-th character of the i-th line Ai, j is the element on the intersection of thei-th row and the j-th column of A. It is guaranteed that, for all integersi, j where 1 ≤ i < j ≤ n, Ai, j = Aj, i holds. Also, for all integersi 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
75 2 4 3 6 7 10001001000000000000101000001000000000100001001000
Output
1 2 4 3 6 7 5
Input
54 2 1 5 30010000011100100110101010
Output
1 2 3 4 5
Note

In the first sample, the swap needed to obtain the prettiest permutation is: (p1, p7).

In the second sample, the swaps needed to obtain the prettiest permutation is (p1, p3), (p4, p5), (p3, p4).

A permutation p is a sequence of integersp1, p2, ..., pn, consisting ofn distinct positive integers, each of them doesn't exceedn. The i-th element of the permutationp is denoted as pi. The size of the permutationp is denoted as n.

刚开始看错题了,结果耽误了好长时间

#include<iostream>#include<cstdio>#include<string>#include<cstring>#include<vector>#include<cmath>#include<queue>#include<stack>#include<map>#include<set>#include<algorithm>using namespace std;const int INF=1000000000;const int maxn=310;int g[maxn][maxn],a[maxn],n,vis[maxn];char c[maxn];void floyd(){    for(int k=0;k<n;k++)        for(int i=0;i<n;i++)        for(int j=0;j<n;j++)            if(g[i][j]>g[i][k]+g[k][j])                g[i][j]=g[i][k]+g[k][j];}int main(){    while(scanf("%d",&n)!=EOF)    {        for(int i=0;i<n;i++)scanf("%d",&a[i]);        for(int i=0;i<n;i++)        {            scanf("%s",c);            for(int j=0;j<strlen(c);j++)                if(c[j]=='0')                    g[i][j]=INF;                else g[i][j]=1;        }        floyd();        memset(vis,0,sizeof(vis));        for(int i=0;i<n;i++)        {            int min1=INF,pos;            for(int j=0;j<n;j++)                if(g[i][j]!=INF&&!vis[j]&&a[j]<min1)                {                    min1=a[j];                    pos=j;                }            if(min1!=INF)            {                vis[pos]=1;                printf("%d ",min1);            }            else            {                vis[i]=1;                printf("%d ",a[i]);            }        }        printf("\n");    }    return 0;}


0 0
原创粉丝点击