洛谷P2527 Panda的烦恼[搜索]

本题问的是由n个质数构成的第k大数字,nk小于1e7

这个问题本来想的是尬膜,但是写出来发现无论怎么调就是30分,发现有一些不符合贪心的性质在里面,比如数字到了插空比较大的地方就容易出现最优解被贪心挤掉的问题。

所以我们不妨换个思路,如果能够找到最小的k-1个数字,那么第k个数字一定是最小的。

然后就一直90分,无论怎么调就是90,感觉看了一些题解区也就比正解多了个pq啊。怎么会这样

#include <bits/stdc++.h>
using namespace std;
#define limit (200 + 5)//防止溢出
#define INF 0x3f3f3f3f
#define inf 0x3f3f3f3f3f
#define lowbit(i) i&(-i)//一步两步
#define EPS 1e-9
#define FASTIO  ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);
#define ff(a) printf("%d\n",a );
#define pi(a,b) pair<a,b>
#define rep(i, a, b) for(ll i = a; i <= b ; ++i)
#define per(i, a, b) for(ll i = b ; i >= a  ; --i)
#define MOD 998244353
#define traverse(u) for(int i = head[u]; ~i ; i = edge[i].next)
#define FOPEN freopen("C:\\Users\\tiany\\CLionProjects\\akioi\\data.txt", "rt", stdin)
#define FOUT freopen("C:\\Users\\tiany\\CLionProjects\\akioi\\dabiao.txt", "wt", stdout)
#define debug(x) cout<<x<<endl
typedef long long ll;
typedef unsigned long long ull;
char buf[1<<23],*p1=buf,*p2=buf,obuf[1<<23],*O=obuf;
inline ll read(){
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
    ll sign = 1, x = 0;char s = getchar();
    while(s > '9' || s < '0' ){if(s == '-')sign = -1;s = getchar();}
    while(s >= '0' && s <= '9'){x = (x << 3) + (x << 1) + s - '0';s = getchar();}
    return x * sign;
#undef getchar
}//快读
void print(ll x) {
    if(x/ 10) print(x / 10);
    *O++=x % 10+'0';
}
void write(ll x, char c = 't') {
    if(x < 0)putchar('-'),x = -x;
    print(x);
    if(!isalpha(c))*O++ = c;
    fwrite(obuf,O-obuf,1,stdout);
    O = obuf;
}
int kase;
int n, m,k;
int64_t a[limit];
void solve(){
    n = read(), k = read();
    priority_queue<ll, vector<ll>, greater<>>q;
    set<ll>s;
    rep(i,1,n){
        a[i] = read();
        q.push(a[i]);
        s.insert(a[i]);
    }
    sort(a + 1, a + 1 + n);
    if(k <= n){
        cout<<a[k]<<endl;
        return;
    }
    vector<ll>stk;
    ll lim = 1e10;

    while (stk.size() < k - 1){
       auto u = q.top();
       q.pop();
        rep(i,1,n){
            if(u * a[i] > lim)break;
            if(s.count(u * a[i]))continue;
            q.push(u * a[i]);
            s.insert(u * a[i]);
        }
        if(stk.empty())stk.push_back(q.top());
        else if(q.top() > stk.back()){
            stk.push_back(q.top());
        }
    }
    write(q.top());
}
int32_t main() {
#ifdef LOCAL
    // FOPEN;
//    FOUT;
#endif
    FASTIO
//    cin>>kase;
//    while (kase--)
    solve();
    cerr << "Time elapsed: " << 1.0*clock()/CLOCKS_PER_SEC << "s\n";
    return 0;
}

 

posted @ 2021-09-13 02:01  tiany7  阅读(52)  评论(0编辑  收藏  举报