OJ#抢气球

题目描述

​ 教室的墙上挂满了气球,五颜六色,小朋友们非常喜欢。

​ 刚一下课,小朋友们就打算去抢这些气球。每个气球在墙上都有一定的高度,只有当小朋友跳起来时,手能够到的高度大于等于气球的高度,小朋友才能摘到这个气球。为了公平起见,老师让跳的低的小朋友先摘,跳的高的小朋友后摘。小朋友都很贪心,每个小朋友在摘气球的时候都会把自己能摘的气球都摘掉

​ 很巧的是,小朋友们跳起来手能够着的高度都不一样,这样就不会有跳起来后高度相同的小朋友之间发生争执了。

输入

​ 第一行输入两个空格分隔的整数 n,m(1≤n,m≤100000),其中 n 表示小朋友的数量,mm 表示墙上气球的数量。

​ 第二行输入 nn 个正整数(每两个整数之间用空格隔开),第 i 个数为 ai(1≤ai≤109),表示第 ii 个小朋友跳起来手能够着的高度为 ai 。

​ 第三行输入 m 个正整数(每两个整数之间用空格隔开),第 i 个数为hi ,表示第 i 个气球的高度为 hi。

输出

​ 输出一共 n 行,每行一个整数。​ 第ii 行表示第 i​ 个小朋友摘到的气球数量。

样例输入1

5 6
3 7 9 6 4
1 2 3 4 5 6

样例输出1

3
0
0
2
1

样例解释1

对于第一组样例输入,摘取气球的顺序依次为 1,5,4,2,31,5,4,2,3 号小朋友。11 号小朋友能摘1,2,31,2,3号气球,55 号小朋友能摘 44 号气球,44 号小朋友能摘 5,65,6 号气球, 2,32,3 号小朋友没有气球可摘了。

样例输入2

10 10
1 2 3 4 5 6 7 8 9 10
3 1 4 6 7 8 9 9 4 12

样例输出2

1
0
1
2
0
1
1
1
2
0

数据规模与限定

时间限制:2 s;内存限制:64 M

第一遍:三遍 sort 排序

#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
using namespace std;
const int N = 1e5 + 10;
int tb[N];
struct A {
    int numb;
    int tab;
    int sm;
}a[N];
bool cmp1(A a, A b) {
    return a.tab < b.tab;
}
bool cmp2(A a, A b) {
    return a.numb < b.numb;
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    int n, m; cin >> n >> m;
    for (int i = 0; i < n; i++) {
        cin >> a[i].tab;
        a[i].numb = i, a[i].sm = 0;
    }
    sort(a, a + n, cmp1);
    for (int i = 0; i < m; i++) {
        cin >> tb[i];
    }
    sort(tb, tb + m, less<int>());
    for (int i = 0, j = 0; i < n && j < m; i++) {
        int t = 0;
        for (; a[i].tab >= tb[j] && j < m; j++) {
            t++;
            tb[j] = 1e9 + 1;
        }
        a[i].sm = t;
    }
    sort(a, a + n, cmp2);
    for (int i = 0; i < n; i++) {
        cout << a[i].sm << endl;
    }
    return 0;
}

第二遍:三遍 stable_sort 排序

PS:过的两个点数据量在5e5,5e5左右

 第三遍:去掉最后一重 stable_sort 改用一个数组存答案

#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
using namespace std;
const int N = 1e5 + 10;
int tb[N];
struct A {
    int numb;
    int tab;
    int sm;
}a[N];
bool cmp1(A a, A b) {
    return a.tab < b.tab;
}
bool cmp2(A a, A b) {
    return a.numb < b.numb;
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    int n, m; cin >> n >> m;
    int abv[n]; 
    for (int i = 0; i < n; i++) {
        cin >> a[i].tab;
        a[i].numb = i, a[i].sm = 0;
    }
    stable_sort(a, a + n, cmp1);
    for (int i = 0; i < m; i++) {
        cin >> tb[i];
    }
    stable_sort(tb, tb + m, less<int>());
    for (int i = 0, j = 0; i < n && j < m; i++) {
        int t = 0;
        for (; a[i].tab >= tb[j] && j < m; j++) {
            t++;
            tb[j] = 1e9 + 1;
        }
        //a[i].sm = t;
        abv[a[i].numb]=t;
    }
    //stable_sort(a, a + n, cmp2);
    //for (int i = 0; i < n; i++) {
    //    cout << a[i].sm << endl;
    //}
    for(int i=0; i<n; i++){
    	cout<<abv[i]<<endl;
    }
    return 0;
}

 第四遍:快读read() 与 print() 模板

#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
using namespace std;

template <typename T> inline void read(T& x)
{
    x = 0; int f = 1; char c = getchar();
    for (; c > '9' || c < '0'; c = getchar()) if (c == '-') f = -1;
    for (; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
    x *= f;
}
template <typename T> inline void print(T x)
{
    if (x < 0) putchar('-'), x = -x;
    if (x > 9) print(x / 10);
    putchar(x % 10 + 48);
}
const int N = 1e5 + 10;
int tb[N], abv[N]{ 0 };
struct A {
    int numb;
    int tab;
}a[N];
bool cmp1(A a, A b) {
    return a.tab < b.tab;
}
bool cmp2(A a, A b) {
    return a.numb < b.numb;
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    int n, m; read(n); read(m);
    
    for (int i = 0; i < n; i++) {
        read(a[i].tab);
        a[i].numb = i;
    }
    for (int i = 0; i < m; i++) {
        read(tb[i]);
    }
    stable_sort(a, a + n, cmp1);
    stable_sort(tb, tb + m);
    for (int i = 0, j = 0; i < n && j < m; i++) {
        int t = 0;
        for (; a[i].tab >= tb[j] && j < m; j++) {
            t++;
            tb[j] = 1e9 + 1;
        }
        abv[a[i].numb] = t;
    }
    for (int i = 0; i < n; i++) {
        //cout << abv[i] << endl;
        print(abv[i]);
        putchar('\n');
    }
    return 0;
}

 作为一个初涉竞赛的人,我是真的麻了啊!

还是要准备一些模板的。。。

快读,快输模板:

template <typename T> inline void read(T& x)
{
    x = 0; int f = 1; char c = getchar();
    for (; c > '9' || c < '0'; c = getchar()) if (c == '-') f = -1;
    for (; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
    x *= f;
}
template <typename T> inline void print(T x)
{
    if (x < 0) putchar('-'), x = -x;
    if (x > 9) print(x / 10);
    putchar(x % 10 + 48);
}

完了,仅作为学习路上的一些遇见。。。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值