序列「CSP-S全国排位赛第一场」

题意

给定若干个多项式,将其在正整数域上的所有值放入一个允许重复的序列中,然后进行sort操作。求该序列的第n项。


思路

比较显然的思路。

由于k的值比较小,所以每次求值的时间可以近似于忽略。那么维护一个小根堆,装着每一个多项式当前的值,然后模拟即可。

时间复杂度为(O(mk+nk))

代码

#include <bits/stdc++.h>

using namespace std;

namespace StandardIO {

	template<typename T>inline void read (T &x) {
		x=0;T f=1;char c=getchar();
		for (; c<'0'||c>'9'; c=getchar()) if (c=='-') f=-1;
		for (; c>='0'&&c<='9'; c=getchar()) x=x*10+c-'0';
		x*=f;
	}

	template<typename T>inline void write (T x) {
		if (x<0) putchar('-'),x*=-1;
		if (x>=10) write(x/10);
		putchar(x%10+'0');
	}

}

using namespace StandardIO;

namespace Project {
	#define int long long
	
	const int N=30003;
	
	int n,m,k; 
	int a[N][10];
	struct node {
		int x,y,z;
		node () {}
		node (int _x,int _y,int _z) : x(_x),y(_y),z(_z) {}
	};
	struct cmp {
		inline bool operator () (const node x,const node y) {
			return x.x>y.x;
		}
	};
	priority_queue<node,vector<node>,cmp> q;
	
	inline int ksm (int base,int pow) {
		int res=1;
		while (pow) {
			if (pow&1) res*=base;
			base*=base,pow>>=1;
		}
		return res;
	}
	inline int next (int x,int t) {
		int res=0,mm=1;
		for (register int i=0; i<=k; ++i) {
			res+=a[x][i]*mm,mm*=t;
		}
		return res;
	}

	inline void MAIN () {
		read(n),read(m),read(k);
		for (register int i=1; i<=m; ++i) {
			for (register int j=0; j<=k; ++j) {
				read(a[i][j]);
			}
			q.push(node(next(i,1),i,1));
		}
		while (--n) {
			node now=q.top();q.pop(),q.push(node(next(now.y,now.z+1),now.y,now.z+1));
		}
		write(q.top().x);
	}
	
	#undef int
}

int main () {
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	Project::MAIN();
}

原文地址:https://www.cnblogs.com/ilverene/p/11781230.html