[PAT] Class 1085 Perfect Sequence (25 points)

Meaning of the questions:

Enter two positive integers N and P (N <= 1e5, P <= 1e9), and then enter the N positive integers. The maximum output of such a set of numbers where the number does not exceed the maximum number of times the minimum number of P.

trick:

5 test points will burst int, because P is too big. . .

Code:

#define HAVE_STRUCT_TIMESPEC
#include<bits/stdc++.h>
using namespace std;
typedef struct node{
long long val;
bool flag;
};
bool cmp(node a,node b){
if(a.val!=b.val)
return a.val<b.val;
return a.flag>b.flag;
}
node a[200007];
map<int,int>pos;
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n,p;
cin>>n>>p;
for(int i=1;i<=n;++i){
cin>>a[i].val;
}
for(int i=1;i<=n;++i){
a[i+n].val=a[i].val*p+1;
a[i+n].flag=1;
}
sort(a+1,a+1+n+n,cmp);
int cnt=0;
int ans=0;
for(int i=1;i<=n+n;++i){
if(a[i].flag==0){
++cnt;
if(!pos[a[i].val])
pos[a[i].val]=cnt;
}
else{
int x=(a[i].val-1)/p;
int tamp=cnt-pos[x]+1;
ans=max(ans,tamp);
}
}
cout<<ans;
return 0;
}

Guess you like

Origin www.cnblogs.com/ldudxy/p/11878078.html
Recommended