P1023 税收与补贴问题 (模拟)

题目链接


Solution

比较恶心的模拟题(主要是难看懂题意其实)
题意戳这里
然后根据一些简单的数学常识,可以知道这是一个二次函数.
所以我们每次枚举一个值,然后判定*给出的价格是否是顶点即可.
与其少一块钱和多一块钱比较即可.

Code

#include<bits/stdc++.h>
using namespace std;
const int maxn=110008;
int w[maxn],c[maxn];
int to,num,v,maxx;;

int read()
{
	int f=1,w=0; char ch=getchar(); 
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){w=w*10+ch-'0';ch=getchar();}
	return f*w;
}

void pre()
{
	int fuck=(c[w[2]]-c[w[1]])/(w[2]-w[1]);
	for(int i=w[1];i<=to+100;i++)
	{
		if(!c[i])
		{
			if(i<=maxx)
				c[i]=c[w[1]]+fuck*(i-w[1]);		
			else
				c[i]=c[maxx]-v*(i-maxx);
		}
		//cout<<i<<' '<<c[i]<<endl;		
	}
}

bool jud(int x)
{
	int fuck1=c[to-1]*(to-1-w[1]+x);
	int fuck2=c[to]*(to-w[1]+x);
	int fuck3=c[to+1]*(to+1-w[1]+x);
	if(fuck2>=fuck1&&fuck2>=fuck3)return 1;
	else return 0;
}

int main()
{
	scanf("%d",&to);
	while(1)
	{
		int x,y;
		x=read(); y=read();
		if(x==-1&&y==-1)break;
		w[++num]=x;
		c[x]=y;
		maxx=max(maxx,w[num]);
	}
	v=read();
	pre();
	if(jud(0)){cout<<0<<endl;return 0;}
	int ans1=0,ans2=0,fuck1=0,fuck2=0;
	for(int i=1;i<=maxn;i++)
	if(jud(i)){fuck1=1;ans1=i;break;}
	for(int i=-1;i>=-maxn;i--)
	if(jud(i)){fuck2=1;ans2=i;break;}
	if(fuck1||fuck2)
	{
		if(fuck1)
		{
			if(fuck2)
			if(abs(ans2)<ans1)
			{cout<<ans2<<endl;return 0;}
			cout<<ans1<<endl;
		}
		else
		if(fuck2)
		{cout<<ans2<<endl;return 0;}
		else
		cout<<"No Solution"<<endl;
	}
}