1 条题解
-
0
C++ :
#include<iostream> #include<cmath> #include<algorithm> using namespace std; int main(){ int k,l; int a[100000],b[100000]; int i=0; cin>>k; while(1){ cin>>a[i]>>b[i]; if(a[i]==-1) break; else i++; } for(int j=0;j<i;j++){ int tem1,tem2; for(int q=1;q<i-j;q++){ if(a[q-1]>a[q]){ swap(a[q-1],a[q]); swap(b[q-1],b[q]); } } } cin>>l; int c[10000]; c[0]=b[0]; int x=1,y=a[0],z=b[0]; for(int j=1;j<i;j++){ if(a[j]!=y+1){ int p=abs(b[j]-z)/abs(a[j]-y); int t=1; while(y!=a[j]){ c[x++]=z-p*t; t++; y++; } z=b[j]; } else{ y=a[j]; z=b[j]; c[x++]=b[j]; } } x--; int g=1,h=c[x]; while(c[x]>0){ c[++x]=h-l*g; g++; } int d[10000]; int f=0; for(int j=0;j<x;j++){ d[j]=a[0]++; } int e; for(int j=0;j<x;j++) if(d[j]==k) e=c[j]; int ans1=0,ans2=0; for(int j=0;j<100;j++){ int max; max=(k-d[0]+j)*e; int s; int flag=0; for(int r=0;r<x;r++){ s=(d[r]-d[0]+j)*c[r]; if(s>max){ flag=1; break; } } if(flag==0){ ans1=j; break; } } for(int j=-1;j>-100;j--){ int max=(k-d[0]+j)*e; int s; int flag=0; for(int r=0;r<x;r++){ s=(d[r]-d[0]+j)*c[r]; if(s>max){ flag=1; break; } } if(flag==0){ ans2=j; break; } } if(ans1==ans2&&ans1==0) cout<<"NO SOLUTION"<<endl; if(ans1==0&&ans2!=0) cout<<ans2<<endl; if(ans1!=0&&ans2==0) cout<<ans1<<endl; if(ans1!=0&&ans2!=0&&abs(ans1)>abs(ans2)) cout<<ans2<<endl; if(ans1!=0&&ans2!=0&&abs(ans1)<abs(ans2)) cout<<ans1<<endl; return 0; }
Pascal :
const maxn=50; maxm=10000; var d:array[1..maxm] of longint; price,sell:array[1..maxn] of longint; k1,k2:real; c,r,t,x,y,n,each,i,j,p,most,m,u1,u2:longint; begin readln(n); c:=0; repeat readln(x,y); if (x=-1)and(y=-1) then break; inc(c); price[c]:=x; sell[c]:=y; until false; readln(m); p:=price[1]; most:=price[c]+sell[c] div m; if (n<p)or(n>most) then begin writeln('NO SOLUTION'); halt; end; for i:=1 to c-1 do begin each:=(sell[i]-sell[i+1]) div (price[i+1]-price[i]); for j:=price[i] to price[i+1]-1 do d[j]:=sell[i]-each*(j-price[i]); end; for i:=price[c] to most do d[i]:=sell[c]-m*(i-price[c]); k1:=-1e20; k2:=1e20; for i:=p to most do begin r:=d[n]-d[i]; t:=i*d[i]-n*d[n]-p*(d[i]-d[n]); if (r>0) and (t/r>k1) then k1:=t/r; if (r<0) and (t/r<k2) then k2:=t/r; end; if k1<-1e19 then u1:=-maxlongint else if (k1>0)and(k1-int(k1)>0) then u1:=trunc(k1)+1 else u1:=trunc(k1); if k2>1e19 then u2:=maxlongint else if (k2<0)and(int(k2)-k2>0) then u2:=trunc(k2)-1 else u2:=trunc(k2); if (u1<0)and(u2>0) then begin u1:=0; u2:=0; end; if u1>u2 then writeln('NO SOLUTION') else if abs(u1)<abs(u2) then writeln(u1) else writeln(u2); readln; end.
- 1
信息
- ID
- 200
- 时间
- 1000ms
- 内存
- 125MiB
- 难度
- (无)
- 标签
- 递交数
- 0
- 已通过
- 0
- 上传者