列表

详情


NC54834. 大强的奋斗之路

描述

农民大强经过多年奋斗终于攒下了m块钱,可以购买属于自己的土地。土地有n种,第i种的价格为ai/m2,年收益为bi/m2。每年年初,大强可以决定是否购买土地;所有的土地都在年末给他带来收入。但是年收入太高容易引起他人的嫉妒,因此大强会Q次问你,在年收益不超过ci的情况下,攒足至少di块钱至少要几年。

输入描述

第1行包括三个正整数n,m,Q。

第2行到第n+1行,每行包括两个正整数ai,bi

接下来Q行,每行包括两个正整数ci,di

输出描述

输出Q行,每行一个整数,表示每次询问的答案。若永远无法达到,输出-1。

示例1

输入:

3 3 2
2 1
3 2
6 8
3 9
6 100

输出:

4
20

说明:

对于第一组询问,在第一年年初购进1m2第二种土地,在第二年年初购进1m2第一种土地。此时大强的年收益为3块钱,手中没有钱。到第四年末刚好攒满9块钱。

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++14(g++5.4) 解法, 执行用时: 373ms, 内存消耗: 3680K, 提交时间: 2020-01-17 21:44:40

#include<bits/stdc++.h>

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef double lf;
typedef long double llf;
typedef std::pair<int,int> pii;

#define xx first
#define yy second

template<typename T> inline T max(T a,T b){return a>b?a:b;}
template<typename T> inline T min(T a,T b){return a<b?a:b;}
template<typename T> inline T abs(T a){return a>0?a:-a;}
template<typename T> inline bool repr(T &a,T b){return a<b?a=b,1:0;}
template<typename T> inline bool repl(T &a,T b){return a>b?a=b,1:0;}
template<typename T> inline T gcd(T a,T b){T t;if(a<b){while(a){t=a;a=b%a;b=t;}return b;}else{while(b){t=b;b=a%b;a=t;}return a;}}
template<typename T> inline T sqr(T x){return x*x;}
#define mp(a,b) std::make_pair(a,b)
#define pb push_back
#define I __attribute__((always_inline))inline
#define mset(a,b) memset(a,b,sizeof(a))
#define mcpy(a,b) memcpy(a,b,sizeof(a))

#define fo0(i,n) for(int i=0,i##end=n;i<i##end;i++)
#define fo1(i,n) for(int i=1,i##end=n;i<=i##end;i++)
#define fo(i,a,b) for(int i=a,i##end=b;i<=i##end;i++)
#define fd0(i,n) for(int i=(n)-1;~i;i--)
#define fd1(i,n) for(int i=n;i;i--)
#define fd(i,a,b) for(int i=a,i##end=b;i>=i##end;i--)
#define foe(i,x)for(__typeof((x).end())i=(x).begin();i!=(x).end();++i)
#define fre(i,x)for(__typeof((x).rend())i=(x).rbegin();i!=(x).rend();++i)

struct Cg{I char operator()(){return getchar();}};
struct Cp{I void operator()(char x){putchar(x);}};
#define OP operator
#define RT return *this;
#define UC unsigned char
#define RX x=0;UC t=P();while((t<'0'||t>'9')&&t!='-')t=P();bool f=0;\
if(t=='-')t=P(),f=1;x=t-'0';for(t=P();t>='0'&&t<='9';t=P())x=x*10+t-'0'
#define RL if(t=='.'){lf u=0.1;for(t=P();t>='0'&&t<='9';t=P(),u*=0.1)x+=u*(t-'0');}if(f)x=-x
#define RU x=0;UC t=P();while(t<'0'||t>'9')t=P();x=t-'0';for(t=P();t>='0'&&t<='9';t=P())x=x*10+t-'0'
#define TR *this,x;return x;
I bool IS(char x){return x==10||x==13||x==' ';}template<typename T>struct Fr{T P;I Fr&OP,(int&x)
{RX;if(f)x=-x;RT}I OP int(){int x;TR}I Fr&OP,(ll &x){RX;if(f)x=-x;RT}I OP ll(){ll x;TR}I Fr&OP,(char&x)
{for(x=P();IS(x);x=P());RT}I OP char(){char x;TR}I Fr&OP,(char*x){char t=P();for(;IS(t);t=P());if(~t){for(;!IS
(t)&&~t;t=P())*x++=t;}*x++=0;RT}I Fr&OP,(lf&x){RX;RL;RT}I OP lf(){lf x;TR}I Fr&OP,(llf&x){RX;RL;RT}I OP llf()
{llf x;TR}I Fr&OP,(uint&x){RU;RT}I OP uint(){uint x;TR}I Fr&OP,(ull&x){RU;RT}I OP ull(){ull x;TR}};Fr<Cg>in;
#define WI(S) if(x){if(x<0)P('-'),x=-x;UC s[S],c=0;while(x)s[c++]=x%10+'0',x/=10;while(c--)P(s[c]);}else P('0')
#define WL if(y){lf t=0.5;for(int i=y;i--;)t*=0.1;if(x>=0)x+=t;else x-=t,P('-');*this,(ll)(abs(x));P('.');if(x<0)\
x=-x;while(y--){x*=10;x-=floor(x*0.1)*10;P(((int)x)%10+'0');}}else if(x>=0)*this,(ll)(x+0.5);else *this,(ll)(x-0.5);
#define WU(S) if(x){UC s[S],c=0;while(x)s[c++]=x%10+'0',x/=10;while(c--)P(s[c]);}else P('0')
template<typename T>struct Fw{T P;I Fw&OP,(int x){WI(10);RT}I Fw&OP()(int x){WI(10);RT}I Fw&OP,(uint x){WU(10);RT}
I Fw&OP()(uint x){WU(10);RT}I Fw&OP,(ll x){WI(19);RT}I Fw&OP()(ll x){WI(19);RT}I Fw&OP,(ull x){WU(20);RT}I Fw&OP()
(ull x){WU(20);RT}I Fw&OP,(char x){P(x);RT}I Fw&OP()(char x){P(x);RT}I Fw&OP,(const char*x){while(*x)P(*x++);RT}
I Fw&OP()(const char*x){while(*x)P(*x++);RT}I Fw&OP()(lf x,int y){WL;RT}I Fw&OP()(llf x,int y){WL;RT}};Fw<Cp>out;

const int N=1007,M=10007,Q=100007;

std::vector<pii>qs[M];
int n,m,cm,q,a[N],b[N],f[M],qi[M],qa[M],c[Q],d[Q],ans[Q];
pii s[N];

int main()
{
	//freopen("in.txt","r",stdin);
	in,n,m,q;
	fo1(i,n)in,s[i].xx,s[i].yy;
	std::sort(s+1,s+n+1);
	fo1(i,n)a[i]=s[i].xx,b[i]=s[i].yy;
	//fo1(i,n)in,a[i],b[i];
	fo1(i,q)in,c[i],d[i];
	fo1(i,q)repr(cm,c[i]),qs[c[i]].pb(mp(d[i],i));
	//out,'/',cm,'\n';
	fo(i,0,cm)f[i]=-1e9;
	f[0]=m;
	fo1(i,cm)std::sort(qs[i].begin(),qs[i].end());
	fo1(i,q)ans[i]=-1;
	fo(i,0,cm)qa[i]=1;
	//out,qs[0].size(),'\n';
	int rem=q;
	fo(j,0,cm)
	{
		int&qt=qi[j];
		//out,'/',j,' ',qt,' ',qs[j].size(),'\n';
		for(;qt<qs[j].size()&&qs[j][qt].xx<=m;qt++)
			ans[qs[j][qt].yy]=0,rem--;
	}
	fo1(T,M)
	{
		//fo(j,0,cm)fo1(i,n)if(f[j]>=a[i]&&j+b[i]<=cm)repr(f[j+b[i]],f[j]-a[i]);
		fo(j,0,cm)
		{
			int&qt=qa[j];
			for(;qt<=n&&a[qt]<=f[j];qt++)
				if(j+b[qt]<=cm)repr(f[j+b[qt]],f[j]-a[qt]);
		}
		int mxf=0;
		fo(j,0,cm)
		{
			f[j]+=j;
			repr(mxf,f[j]);
			int&qt=qi[j];
			//out,'/',j,' ',qt,' ',qs[j].size(),'\n';
			for(;qt<qs[j].size()&&qs[j][qt].xx<=mxf;qt++)
				ans[qs[j][qt].yy]=T,rem--;
		}
		if(!rem)break;
	}
	fo1(i,q)out,ans[i],'\n';
}

C++(clang++ 11.0.1) 解法, 执行用时: 76ms, 内存消耗: 2180K, 提交时间: 2022-11-19 21:37:14

#include<cstdio>
#include<algorithm>
using namespace std;
const int N=200010;
int n,m,nn,x,top,a[N],b[N],f[N],s[N],ans[N];
struct que {int lim,aim,id;} c[N];
struct poi {double x,y;} p[N];
bool operator < (que a,que b) {return a.lim<b.lim;}
inline int read() {
	int tmp=0, fh=1; char c=getchar();
	while (c<'0'||c>'9') {if (c=='-') fh=-1; c=getchar();}
	while (c>='0'&&c<='9') tmp=tmp*10+c-48, c=getchar();
	return tmp*fh;
}
inline bool below (poi a,int l) {return a.y<=a.x*l+f[l];}
inline poi jiao(int l1,int l2) {
	double x=1.0*(f[l2]-f[l1])/(l1-l2);
	double y=l1*x+f[l1];
	return (poi){x,y}; 
}
int main() {
	n=read(); f[0]=read(); m=read();
	for (int i=1;i<=n;i++) a[i]=read(), b[i]=read();
	for (int i=1;i<=m;i++) {
		c[i].lim=read(); c[i].aim=read(); c[i].id=i;
		nn=max(nn,c[i].lim);
	}
	for (int i=1;i<=m;i++) ans[i]=1e9; 
	sort(c+1,c+m+1);
	for (int i=1;i<=nn;i++) f[i]=-1e9;
	for (int i=1;i<=n;i++) if (a[i]<=f[0]) f[b[i]]=max(f[b[i]],f[0]-a[i]);
	//for (int i=0;i<=nn;i++) printf("%d\n",f[i]);
	for (int i=1,k=1;i<=nn;i++) {
		if (f[i]>-1e9) {
			while (top>1&&below(p[top-1],i)) top--;
			if (top) p[top]=jiao(s[top],i); s[++top]=i;
		}
		while (k<=m&&c[k].lim==i) {
			//printf("QAQ %d\n",c[k].id);
			if (c[k].aim<=f[0]) {ans[c[k].id]=0; k++; continue;}
			if (!top) {k++; continue;}
			//printf("QAQ %d %d\n",c[k].id,f[i]);
			int l,r,mid; l=0; r=top;
			while (l+1<r) {
				mid=(l+r)>>1;
				if (p[mid].y>=c[k].aim) r=mid;
				else l=mid;
			}
			//printf(" %d %d\n",r,s[r]);
			x=(c[k].aim-f[s[r]])/s[r];
			if (s[r]*x+f[s[r]]<c[k].aim) x++;
			ans[c[k].id]=x;
			k++;
		}
		if (f[i]>-1e9) {
			for (int j=1;j<=n;j++) if (i+b[j]<=nn) {
				x=(i==0?0:max((a[j]-f[i])/i,0)); if (i*x+f[i]<a[j]) x++;
				f[i+b[j]]=max(f[i+b[j]],f[i]-a[j]-b[j]*x);
			}
		}
	}
	//for (int i=0;i<=nn;i++) printf("%d %d\n",i,f[i]); puts("");
	for (int i=1;i<=m;i++) printf("%d\n",ans[i]<1e9?ans[i]:-1);
	return 0;
}

上一题