P1023解题报告

· · 学习·文化课

P1023 [NOIP2000 普及组] 税收与补贴问题

样例:

政府预期 31 元;

成本价为 28 元,销量 130

已知售价为 29 元时,销量 120

又知售价为 30 元时,销量 110

此后售价每增加 1 元,销量减少 15

核心原理:模拟出每个合法单价下的销售量,再解不等式

(合法单价:指的是 单价大于成本价 且 该单价下销售量为正数 的单价,也就是收益为正的情况)

复杂度将近为 O(2n),n 最坏为10^5

我们用数组 f[] 存储单价为 i 时的销售量 f[i]

>在读入时: > >对于每个读入的 $x,y$,记录 $f[x]=y$,接下来补足空缺销售量 > >比如样例中,我们已经记录 $f[28]=130,f[30]=120$,我们就需算出 $f[29]$ 的值 > >>具体方法: >> >>从 $f[x]$ 一位一位跳至第一位 $f[last] \neq 0$ 的$last

算出公差 \Delta=\frac{f[x]-f[last]}{x-last}

再从 last+1 循环到 x-1 填补其中的 f[i]=f[i-1]+\Delta

读入完毕后读入一个数 y ,继续填补剩余的合法销售量

具体方法:

用while遍历,令 f[x+1]=f[x]-y

再令 x 递增

在数据处理时:

从 st 自 x 遍历,对于每一个 i \neq T 的情况,都要满足:

(ans+i-st)f[i] \le (ans+T-st)f[T]

所以我们用 ans_{min},ans_{max} 来保存 ans 答案的区间

那么我们来手推一下解不等式的过程:

\begin{aligned} (ans+i-st)f[i] &\le (ans+T-st)f[T]\\ ans \cdot f[i] + (i-st) \cdot f[i] &\le ans \cdot f[T] +(T-st) \cdot f[T]\\ ans(f[i]-f[T]) &\le (T-st)f[T]-(i-st)f[i]\\ 所以ans &\le \frac{(T-st)f[T]-(i-st)f[i]}{f[i]-f[T]}(f[i]>f[T]时)\\ 或ans &\geq \frac{(T-st)f[T]-(i-st)f[i]}{f[i]-f[T]}(f[i]<f[T]时)\\ \end{aligned}

SBSuper Brilliant不等式

那么我们只需要不断维护区间端点就好了

在输出时:

按要求输出绝对值最小的

所以

若 ans_{max} < ans_{min},则无解

若 [ans_{min},ans_{max}] \subseteq (-\infty,0]时,输出 ans_{max}

若 [ans_{min},ans_{max}] \subseteq [0,\infty)时,输出 ans_{min}

其余的说明 0\in[ans_{min},ans_{max}],输出 0

代码:

#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;
}