抗击疫情 从我做起 训练赛四

B: 众数

题目描述
小明最近在上数学课,老师给小明布置了个作业:在 n 个数里找出所有的众数。
众数的定义是这样的:在所有数当中出现次数最多的数被称为众数。并且根据定义,众 数有可能有多个。
你能解决这个问题吗?

输入
第一行一个整数 n。
第二行有 n 个整数,ai 表示第 i 个数。

输出
输出一行,包括一个整数 k,表示众数的个数。
接下来一行包括 k 个整数,每个整数都表示一个众数,并且从小到大输出。

样例输入

10
3 3 3 2 3 1 2 2 1 2

样例输出
2
2 3
提示
40%的数据,1<=n<=400
100%的数据,1<=n<=1000000,1<=ai<=1000000000

题意很简单,找众数,比赛的时候用数组写的,今天补题的时候交上去一直Wa,后来发现是for(i = 0; i < n;i++)写错了,应该是 for(int i = 1; i <= n; i++);假设每个数出现的次数都是1,模拟一下,就知道为什么不对了

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxn = 1e6 + 10;
int n,a[maxn],cnt,ans,b[maxn];

signed main(){
    ios::sync_with_stdio(0);
    cin >> n;
    for(int i = 0; i < n; i++) cin >> a[i];
    sort(a,a+n);
    for(int i = 1; i <= n; i++){
        cnt = 0;
        while(a[i - 1] == a[i]){
            cnt++;
            i++;
        }
        ans = max(ans,cnt + 1);
    }
    int m = 0;
    for(int i = 1; i <= n; i++){
        cnt = 0;
        while(a[i - 1] == a[i]){
            cnt++;
            i++;
        }
        if(ans == (cnt + 1)) b[m++] = a[i - 1];
    }
    cout << m << endl;
    cout << b[0];
    for(int i = 1; i < m; i++) cout << " " << b[i];
    return 0;
}

还有一种简单的写法

#include <bits/stdc++.h>
#define int long long
using namespace std;
map<int,int> mp;
set<int> s;
int n,t,ans;
signed main(){
    ios::sync_with_stdio(0);
    cin >> n;
    for(int i = 0; i < n; i++){
        cin >> t;
        mp[t]++;
    }
    for(auto it : mp) ans = max(it.second,ans);
    for(auto it : mp) {
        if(ans == it.second){
            s.insert(it.first);
        }
    }
    cout << s.size() << endl;
    for(auto it : s){
        cout << it << " ";
    }
    return 0;
}

C: NH树

题目描述
小明终于忙玩了各种各样的课程,终于可以继续学习算法了。
他在图论书上看到了树,树有许许多多特殊的性质。小明一下子就喜欢上了这种特殊的树。
于是,他发明了自己的对于无向图的评分方法。
一个无向图的分数定义为,各个连通块是树的数量。
现在给定一个 n 个点 m 条边的无向图,问在小明的评分方法下,分数为多少。
一个连通块是树,当且仅当边数比点数少 1。

输入
第一行两个整数 n 和 m,表示图的点数和边数。
第二行有 m 对整数,u 和 v 表示,结点 u 和节点 v 之间有边。
给出的无向图不存在重边。

输出
输出一行包括一个整数,表示无向图的评分,也就是树的数量。
样例输入

8 6
1 2
1 3
2 4
5 6
6 7
5 7

样例输出
2
提示
20%的数据,1<=n<=2000
100%的数据,1<=n<=100000,0<=m<=min(n*(n-1)/2,200000)

用并查集写的不对

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 10;
int pre[MAXN], n, m, ans;
int Find(int x){
    int root = x;
    while (pre[root] != root && pre[root] != -1) root = pre[root];
    while (pre[x] != x && pre[x] != -1) {
        int y = pre[x];
        pre[x] = root;
        x = y;
    }
    return root;
}

signed main(){
    ios::sync_with_stdio(false);
    cin >> n >> m;
    for (int i = 0;i <= n; i++)	pre[i] = i;
    for (int i = 1, u, v; i <= m; i++){
        cin >> u >> v;
        u = Find(u), v = Find(v);
        if (u != v)	pre[v] = u;
        else	pre[u] = -1;
    }
    for (int i = 1;i <= n; i++)
        if (pre[i] == i)	ans++;
    cout << ans << endl;
    return 0;
}

D: NH字符串

给一个字符串 T,问在字符串 T 中可以包含最多多少个不重叠的字符串 S。
字符串中的每个字符为小写或者大写字母。

输入
第一行输入一个字符串 S。
第二行输入一个字符串 T。

输出
输出一行,包括一个整数表示答案。
样例输入 Copy
Aba
Abababa
样例输出 Copy
1
提示
50%的数据,1<=字符串T长度<=20000,1<=字符串S长度<=100
100%的数据,1<=字符串T长度<=1000000,1<=字符串S长度<=100000。其中多数是随机产生。
在这里插入图片描述

#include <bits/stdc++.h>
#define int long long
using namespace std;
string s,t;
int cnt,id;
signed main(){
    ios::sync_with_stdio(0);
    cin >> s >> t;
    int sl = s.size();
    for(int i = 0; i < t.size();i++){
        if(t[i] == s[id]) id++;
        else if(t[i] == s[0]) id = 1;
        else id = 0;
        if(id == sl){
            cnt++;
            id = 0;
        }
    }
    cout << cnt;
    return 0;
}

I: Rectangle Cutting

题目描述
There is a rectangle in a coordinate plane. The coordinates of the four vertices are (0,0), (W,0),
(W,H), and (0,H). You are given a point (x,y) which is within the rectangle or on its border. We will draw a straight line passing through (x,y) to cut the rectangle into two parts. Find the maximum possible area of the part whose area is not larger than that of the other. Additionally, determine if there are multiple ways to cut the rectangle and achieve that maximum.

Constraints
·1≤W,H≤109
·0≤x≤W0≤y≤H
·All values in input are integers.
输入
Input is given from Standard Input in the following format:

W H x y

输出
Print the maximum possible area of the part whose area is not larger than that of the other, followed by 1 if there are multiple ways to cut the rectangle and achieve that maximum, and 0 otherwise.

The area printed will be judged correct when its absolute or relative error is at most 10−9.
样例输入
【样例1】
2 3 1 2
【样例2】
2 2 1 1
样例输出
【样例1】
3.000000 0
【样例2】
2.000000 1
提示
The line x=1 gives the optimal cut, and no other line does.
过点(x,y)分成相等的两部分,这部分面积是几?
分法是否唯一,(不唯一的话,说明在中心)其他唯一

#include <bits/stdc++.h>

#define int long long
using namespace std;
int w, h, x, y;

signed main(){

    cin >> w >> h >> x >> y;
    printf("%.6f ",w * h / 2.0);
    if(w == 2 * x && h == 2 * y) cout << 1;
    else cout << 0;
    return 0;
}



发布了90 篇原创文章 · 获赞 4 · 访问量 9473

猜你喜欢

转载自blog.csdn.net/xcfkaixin/article/details/104185244