标签:codeforces
比赛链接:http://codeforces.com/contest/522
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.
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.
Print a single integer — the maximum length of a repost chain.
5
tourist reposted Polycarp
Petr reposted Tourist
WJMZBMR reposted Petr
sdya reposted wjmzbmr
vepifanov reposted sdya
6
6
Mike reposted Polycarp
Max reposted Polycarp
EveryOne reposted Polycarp
111 reposted Polycarp
VkCup reposted Polycarp
Codeforces reposted Polycarp
2
1
SoMeStRaNgEgUe reposted PoLyCaRp
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); }
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.
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.
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.
3
1 10
5 5
10 1
75 110 60
3
2 1
1 2
2 1
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"); }
Codeforces VK Cup 2015 Wild Card Round 1 (AB)
标签:codeforces
原文地址:http://blog.csdn.net/tc_to_top/article/details/45049037