第一行一个正整数 n,代表这篇文章的单词总数。 接下来 n 行每行一个字符串,代表一个单词,单词仅由大小写英文字母组成。 1<= n <= 106 保证所有的字符串长度之和不超过 106
输出描述:
仅一行一个整数表示答案。
输入例子1:
5
I
I
am
a
boy
输出例子1:
4
例子说明1:
单词 I 出现的频率为 40% ,其他单词出现的频率均为 20%。所以都可以作为关键词。
啊? 真的吗 这题纯纯签到了 哈希用得好系列(?
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <unordered_map>
#include <unordered_set>
using namespace std;
int main()
{
int n, res = 0;
cin >> n;
cin.get();
unordered_map<string, int> ha;
for(int i = 1; i <= n; i++)
{
string line;
getline(cin, line);
ha[line]++;
if(100 * ha[line] >= n)
{
res ++;
ha[line] = -1e6;
}
}
cout << res << endl;
return 0;
}
04 - [编程题]牛牛铺路
第一行为一个 n,表示城市数量。 接下来有 n 行,每行有四个整数x0,y0,x1,y1,表示城市坐标。
输出描述:
输出为一行,表示答案。
输入例子1:
3
0 0 1 1
2 2 3 3
4 4 5 5
输出例子1:
4
一看就是最小生成树了 -> 很快啊 就是kruskal算法
这题就建图好麻烦 那个曼哈顿距离捯饬了半天才搞好顺序 矩形的曼哈顿距离就是线段的距离推广
还有数据范围会溢出 记得边权用long long 来存就好了
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
using LL = long long;
const int N = 1010, M = N * N;
int n;
int p[N];
struct Node
{
int x1, y1, x2, y2;
}nodes[N];
struct Edge
{
int a, b;
LL c;
Edge() : a(), b(), c() {}
Edge(int a, int b, LL c) : a(a), b(b), c(c) {}
bool operator< (const Edge& w) const{
return c < w.c;
}
}e[M];
int find(int x)
{
if (p[x] != x)
p[x] = find(p[x]);
return p[x];
}
LL get(Node& n1, Node& n2)
{
LL x1 = n1.x1, x2 = n1.x2, y1 = n1.y1, y2 = n1.y2;
LL x3 = n2.x1, x4 = n2.x2, y3 = n2.y1, y4 = n2.y2;
LL dist_x = max((LL)0, max(x1, x3) - min(x2, x4));
LL dist_y = max((LL)0, max(y1, y3) - min(y2, y4));
return dist_x + dist_y;
}
int main()
{
scanf("%d", &n);
for(int i = 1; i <= n; i++)
{
int x1, y1, x2, y2;
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
nodes[i] = {x1, y1, x2, y2};
}
// 并查集初始化
for(int i = 1; i <= n; i++)
p[i] = i;
// 建边
int cnt = 0;
for(int i = 1; i <= n; i++)
for(int j = 1; j < i; j++)
{
LL dist = get(nodes[i], nodes[j]);
e[cnt++] = {i, j, dist};
}
// kruskal 求最小生成树
LL res = 0;
sort(e, e + cnt);
for(int i = 0; i < cnt; i++)
{
int a = e[i].a, b = e[i].b;
LL c = e[i].c;
if(find(a) != find(b))
{
p[find(a)] = find(b);
res += c;
}
}
cout << res << endl;
return 0;
}
牛牛生活在网格世界中,在网格世界中人们出行只能通过网格上的边来进行。现在牛牛管理着 n 个城市,每个城市在网格世界中都以一个矩形来表示,牛牛想在这 n 个城市之间铺上水泥路方便人们出行,网格上一条边铺上水泥所需要的花费为 1 。但是为了节约预算,牛牛给你这 n 个城市的左下坐标(x0,y0)和右上坐标(x1,y1),牛牛想让你告诉他让这 n 个城市联通所需要的最小花费是多少呢。(如图花费为6) 输入描述: