Codeforces VK Cup 2015 Wild Card Round 1 (AB)

54 篇文章 0 订阅
53 篇文章 0 订阅

比赛链接: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 "name1 reposted name2", where name1 is the name of the person who reposted the joke, and name2 is the name of the person from whose news feed the joke was reposted. It is guaranteed that for each string "name1 reposted 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 "name1 reposted 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
5
tourist reposted Polycarp
Petr reposted Tourist
WJMZBMR reposted Petr
sdya reposted wjmzbmr
vepifanov reposted sdya
Output
6
Input
6
Mike reposted Polycarp
Max reposted Polycarp
EveryOne reposted Polycarp
111 reposted Polycarp
VkCup reposted Polycarp
Codeforces reposted Polycarp
Output
2
Input
1
SoMeStRaNgEgUe 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: the i-th of them occupies the rectangle of width wi pixels and height hi pixels. On the group photo everybody stands in a line, thus the minimum pixel size of the photo including all the photographed friends, is W × 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 the j-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: the i-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 numbers b1, b2, ..., bn, where bi — the total number of pixels on the minimum photo containing all friends expect for the i-th one.

Sample test(s)
Input
3
1 10
5 5
10 1
Output
75 110 60 
Input
3
2 1
1 2
2 1
Output
6 4 6 


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

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

#include <cstdio>
#include <algorithm>
#define ll long long
using 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
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值