样例:
政府预期 元;
成本价为 元,销量
已知售价为 元时,销量
又知售价为 元时,销量
此后售价每增加 元,销量减少
核心原理:模拟出每个合法单价下的销售量,再解不等式
(合法单价:指的是 单价大于成本价 且 该单价下销售量为正数 的单价,也就是收益为正的情况)
复杂度将近为 , 最坏为
我们用数组 存储单价为 时的销售量
存储成本价; 存储预期价; 就是答案
在读入时:
对于每个读入的 ,记录 ,接下来补足空缺销售量
比如样例中,我们已经记录 ,我们就需算出 的值
具体方法:
从 一位一位跳至第一位 的
算出公差
再从 循环到 填补其中的
读入完毕后读入一个数 ,继续填补剩余的合法销售量
具体方法:
用while遍历,令
再令 递增
在数据处理时:
从 自 遍历,对于每一个 的情况,都要满足:
所以我们用 来保存 答案的区间
那么我们来手推一下解不等式的过程:
SBSuper Brilliant不等式那么我们只需要不断维护区间端点就好了
在输出时:
按要求输出绝对值最小的
所以
若 ,则无解
若 时,输出
若 时,输出
其余的说明 ,输出
代码:
#include<bits/stdc++.h>
namespace chuazen{
#define int long long
#define max(x,y) (x>y?x:y)
#define min(x,y) (x<y?x:y)
#define ufor(i,x,y) for(int i=x;i<=y;i++)
#define dfor(i,x,y) for(int i=x;i>=y;i--)
// #define getchar() ((p1==p2)&&(p2=(p1=in)+fread(in,1,1<<21,stdin),p1==p2)?EOF:*p1++)
#define N 100005
char *p1,*p2,in[1<<21],out[100];
inline int read(){
int x=0,f=1;
char c=getchar();
while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
return f*x;
}
inline void write(int x){
if(x<0)putchar('-'),x=-x;
int top=0;
do{out[++top]=x%10+'0';x/=10;}while(x);
while(top)putchar(out[top--]);
}
}
using namespace std;
using namespace chuazen;
int T,st,x,y;
int last;
double deta,f[N],ans_min=INT_MIN,ans_max=INT_MAX;
signed main(){
T=read();
x=read(),y=read();
st=x,f[x]=y;
while(1){
deta=read(),y=read();
if(deta==-1&&y==-1)break;
x=deta;last=x-1;f[x]=y;
while(f[last]==0)last--;
deta=(double)(f[x]-f[last])/(double)(x-last);
ufor(i,last+1,x-1)f[i]=f[i-1]+deta;
}
y=read();
while(f[x]>0)f[x+1]=f[x]-y,x++;
if(f[x]<0)x--;
ufor(i,st,x){
if(i==T)continue;
if(f[i]<f[T])ans_min=max(ans_min,((T-st)*f[T]-(i-st)*f[i])/(f[i]-f[T]));
if(f[i]>f[T])ans_max=min(ans_max,((T-st)*f[T]-(i-st)*f[i])/(f[i]-f[T]));
}
ans_min=(ans_min<0?floor(ans_min):ceil(ans_min));
ans_max=(ans_max<0?floor(ans_max):ceil(ans_max));
if(ans_min>ans_max)puts("NO SOLUTION");
else if(ans_max<0)write(ans_max);
else if(ans_min>0)write(ans_min);
else puts("0");
return 0;
}