Codeforces VK Cup 2015 Wild Card Round 1 (AB)

来源:互联网 发布:跳跃表 java 编辑:程序博客网 时间:2024/04/28 10:05

比赛链接:http://codeforces.com/contest/522


A. Reposts
time limit per test:1 second
memory limit per test:256 megabytes

One day Polycarp published a funny picture in a social network making a poll about the color of his handle. Many of his friends started reposting Polycarp's joke to their news feed. Some of them reposted the reposts and so on.

These events are given as a sequence of strings "name1reposted name2", wherename1 is the name of the person who reposted the joke, andname2 is the name of the person from whose news feed the joke was reposted. It is guaranteed that for each string "name1reposted name2" user "name1" didn't have the joke in his feed yet, and "name2" already had it in his feed by the moment of repost. Polycarp was registered as "Polycarp" and initially the joke was only in his feed.

Polycarp measures the popularity of the joke as the length of the largest repost chain. Print the popularity of Polycarp's joke.

Input

The first line of the input contains integer n (1 ≤ n ≤ 200) — the number of reposts. Next follow the reposts in the order they were made. Each of them is written on a single line and looks as "name1reposted name2". All the names in the input consist of lowercase or uppercase English letters and/or digits and have lengths from 2 to 24 characters, inclusive.

We know that the user names are case-insensitive, that is, two names that only differ in the letter case correspond to the same social network user.

Output

Print a single integer — the maximum length of a repost chain.

Sample test(s)
Input
5tourist reposted PolycarpPetr reposted TouristWJMZBMR reposted Petrsdya reposted wjmzbmrvepifanov reposted sdya
Output
6
Input
6Mike reposted PolycarpMax reposted PolycarpEveryOne reposted Polycarp111 reposted PolycarpVkCup reposted PolycarpCodeforces reposted Polycarp
Output
2
Input
1SoMeStRaNgEgUe reposted PoLyCaRp
Output
2


题目大意:求最长链的节点数

题目分析:用map 字符串hash一下,然后跑下floyd,代码写的丑


#include <cstdio>#include <map>#include <string>#include <algorithm>#include <iostream>using namespace std;map <string, int> mp;map <string, int> :: iterator it;int const MAX = 505;int g[MAX][MAX], cnt;int Floyd(){    int ans = 1;    for(int k = 0; k < cnt; k++)    {        for(int i = 0; i < cnt; i++)        {            for(int j = 0; j < cnt; j++)            {                if(g[i][k] && g[k][j])                {                    g[i][j] = g[i][k] + g[k][j];                    ans = max(ans, g[i][j]);                }            }        }    }    return ans;}void trans(string a){    int i = 0;    while(a[i])    {        if(a[i] >= 'A' && a[i] <= 'Z')            a[i] += 'a' - 'A';        i++;    }}int main(){    int n;    cnt = 0;    scanf("%d", &n);    while(n--)    {        string a, tmp, b;        cin >> a >> tmp >> b;        int i = 0;        while(a[i])        {            if(a[i] >= 'A' && a[i] <= 'Z')                a[i] += 'a' - 'A';            i++;        }        i = 0;        while(b[i])        {            if(b[i] >= 'A' && b[i] <= 'Z')                b[i] += 'a' - 'A';            i++;            }        mp[a] = cnt++;        it = mp.find(a);        if(it == mp.end())            mp[a] = cnt++;        it = mp.find(b);        if(it == mp.end())            mp[b] = cnt++;        g[mp[a]][mp[b]] = 1;    }    int ans = Floyd();    printf("%d\n", ans + 1);}



B. Photo to Remember
time limit per test:2 seconds
memory limit per test:256 megabytes

One day n friends met at a party, they hadn't seen each other for a long time and so they decided to make a group photo together.

Simply speaking, the process of taking photos can be described as follows. On the photo, each photographed friend occupies a rectangle of pixels: thei-th of them occupies the rectangle of widthwi pixels and heighthi pixels. On the group photo everybody stands in a line, thus the minimum pixel size of the photo including all the photographed friends, isW × H, where W is the total sum of all widths and H is the maximum height of all the photographed friends.

As is usually the case, the friends made n photos — the j-th (1 ≤ j ≤ n) photo had everybody except for thej-th friend as he was the photographer.

Print the minimum size of each made photo in pixels.

Input

The first line contains integer n (2 ≤ n ≤ 200 000) — the number of friends.

Then n lines follow: thei-th line contains information about the i-th friend. The line contains a pair of integers wi, hi (1 ≤ wi ≤ 10, 1 ≤ hi ≤ 1000) — the width and height in pixels of the corresponding rectangle.

Output

Print n space-separated numbersb1, b2, ..., bn, wherebi — the total number of pixels on the minimum photo containing all friends expect for thei-th one.

Sample test(s)
Input
31 105 510 1
Output
75 110 60 
Input
32 11 22 1
Output
6 4 6 


题目大意:给n个wi和hi,去删去第i个wi和hi后剩下的wi的和与hi的最大值的积

题目分析:直接模拟出最大和次大值,如果最大值不止一个标记一下,然后就没什么东西了。。。

#include <cstdio>#include <algorithm>#define ll long longusing namespace std;int const MAX = 2 * 1e6 + 5;ll w[MAX], h[MAX];int main(){    int n;    ll sum = 0, ma1 = 0, ma2 = 0, pos = 0;    bool flag = false;    scanf("%d", &n);    for(int i = 0; i < n; i++)    {        scanf("%I64d %I64d", &w[i], &h[i]);        sum += w[i];        if(ma1 < h[i])        {            ma1 = h[i];            pos = i;        }    }    for(int i = 0; i < n; i++)    {        if(i == pos)            continue;        if(h[i] == ma1)        {            flag = true;            continue;        }        ma2 = max(ma2, h[i]);    }    for(int i = 0; i < n; i++)    {        if(h[i] == ma1)        {            if(flag)                printf("%I64d ", (sum - w[i]) * ma1);            else                printf("%I64d ", (sum - w[i]) * ma2);        }           else            printf("%I64d ", (sum - w[i]) * ma1);    }    printf("\n");}


0 0
原创粉丝点击