title: P1023税收与补贴问题 categories:


#include<stdio.h>
#include<string.h>
#include<math.h>
using namespace std;
struct hh{
    int price,sale;
}G[10001];
int main(){
    int n,m;
    scanf("%d",&n);
    int p=1;
    int chengben,ssale;
    int g=0;
    scanf("%d %d",&chengben,&ssale);
    while(scanf("%d %d",&G[p].price,&G[p].sale)&&G[p].price!=-1){
        if(G[p].price==n)
           g=p,p++;
        else{
            p++;
        }
    }
    p--;
    scanf("%d",&m);
    int f=p;
    f++;
    if(G[p].price>n&&!g){
        int e=0;
        for(int i=1;i<=p;i++){
            if(G[i].price<n)
            {
                G[p].price=G[i].price;
                G[p].sale=G[i].sale;
                e++;
            }
        }
        if(!e){
            G[p].price=chengben;
            G[p].sale=ssale;
        }
    }
    for(int i=G[p].price+1;i<=10001;i++){
         G[f].price=i;
         G[f].sale=G[p].sale-(m*(G[f].price-G[p].price));
         f++;
    }
    if(g==0){
        for(int i=p;i<=10001;i++){
            if(G[i].price==n){
                g=i;
                break;
            }
        }
    }
    int h=0;
    for(int i=1;i<=10001;i++){
        int a=(G[g].price-chengben+i)*G[g].sale;
        int b=(G[g-1].price-chengben+i)*G[g-1].sale;
        int c=(G[g+1].price-chengben+i)*G[g+1].sale;
        if(a>b&&a>c){
           printf("%d",i);
           h++;
            return 0;
        }
    }
    if(h==0){
        for(int i=1;i<=10001;i++){
            int a=(G[g].price-chengben-i)*G[g].sale;
            int b=(G[g-1].price-chengben-i)*G[g-1].sale;
            int c=(G[g+1].price-chengben-i)*G[g+1].sale;
            if(a>=b&&a>=c){
                printf("-%d",i);
                h++;
                return 0;
            }
        }
        if(h==0){
            printf("NO SOLUTION");
        }
        return 0;
    }
}