PAT (Basic Level) Practice (中文)- 1060 爱丁顿数(25 分)

简介: PAT (Basic Level) Practice (中文)- 1060 爱丁顿数(25 分)

题目链接:点击打开链接

题目大意:题目有点拗口,多读几遍那核心语句,注意是当中的“超过”一词,不能加等号。

解题思路:AC1:差分后缀和 + 二分比较; AC2:普通模拟(如果数据再大点可能会TLE)。

AC1 代码

#include<bits/stdc++.h>
#include<cmath>
#define mem(a,b) memset(a,b,sizeof a);
#define INF 0x3f3f3f3f
#define MOD 1000000007
using namespace std;
typedef long long ll;
const int maxn=1e5+10;
unordered_map<int,int> ump;
int a[maxn], w[maxn];
int main()
{
    int n,b,l=0;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%d",&b);
        if(ump[b]==0) a[++l]=b;
        ump[b]++;
    }
    sort(a+1,a+l+1);
    w[l]=ump[a[l]];
    for(int i=l-1;i>0;i--)
        w[i]=ump[a[i]]+w[i+1];
    w[0]=2*w[1];
    int p=l-1, rs=-1, d;
    while(p>=0)
    {
        d=w[p]-ump[a[p]];
        if(d>=a[p])
        {
            rs=a[p];
            int l=a[p]+1, r=a[p+1]-1, m;
            while(l<=r)
            {
                m=l+((r-l)>>1);
                if(d>=m) rs=m, l=m+1;
                else r=m-1;
            }
            break;
        }
        else p--;
    }
    printf("%d\n",rs);
    return 0;
}


AC2 代码

#include<bits/stdc++.h>
#include<cmath>
#define mem(a,b) memset(a,b,sizeof a);
#define INF 0x3f3f3f3f
#define MOD 1000000007
using namespace std;
typedef long long ll;
int main()
{
    int n;
    scanf("%d",&n);
    int a[n+5];
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    sort(a+1,a+n+1,greater<int>());
    int ans=0, p=1;
    while(ans<=n && a[p]>p) ans++, p++; // ans 表示满足的最多天数,p 表示英里数(超过)
    printf("%d\n",ans);
    return 0;
}
目录
相关文章
|
11月前
|
C语言 C++
PAT (Basic Level) Practice (中文)1099 性感素数(20分)
“性感素数”是指形如 (p, p+6) 这样的一对素数。之所以叫这个名字,是因为拉丁语管“六”叫“sex”(即英语的“性感”)。(原文摘自 http://mathworld.wolfram.com/SexyPrimes.html) 现给定一个整数,请你判断其是否为一个性感素数。
94 0
PAT (Basic Level) Practice (中文) 1016 部分A+B (15 分)
PAT (Basic Level) Practice (中文) 1016 部分A+B (15 分)
64 0
|
C语言
PAT (Basic Level) Practice (中文) B1026 程序运行时间 (15 分)
PAT (Basic Level) Practice (中文) B1026 程序运行时间 (15 分)
97 0
|
测试技术
PAT (Basic Level) Practice (中文) B1011 A+B 和 C (15 分)
PAT (Basic Level) Practice (中文) B1011 A+B 和 C (15 分)
76 0
PAT (Basic Level) Practice (中文) B1011 A+B 和 C (15 分)
PAT (Basic Level) Practice (中文) B1046 划拳 (15 分)
PAT (Basic Level) Practice (中文) B1046 划拳 (15 分)
59 0
|
算法
PAT (Basic Level) Practice (中文)1028. 人口普查(20分)
PAT (Basic Level) Practice (中文)1028. 人口普查(20分)
80 0
|
存储 测试技术
PAT (Basic Level) Practice (中文) 1004 成绩排名 (20 分)
PAT (Basic Level) Practice (中文) 1004 成绩排名 (20 分)
64 0
|
测试技术
PAT (Basic Level) Practice (中文)1012 数字分类 (20 分)+易错测试点
PAT (Basic Level) Practice (中文)1012 数字分类 (20 分)+易错测试点
98 0
PAT (Basic Level) Practice (中文)1012 数字分类 (20 分)+易错测试点
|
存储 人工智能
PAT (Basic Level) Practice (中文)- 1030 完美数列(25 分)
PAT (Basic Level) Practice (中文)- 1030 完美数列(25 分)
72 0
PAT (Basic Level) Practice (中文)- 1050 螺旋矩阵(25 分)
PAT (Basic Level) Practice (中文)- 1050 螺旋矩阵(25 分)
81 0