luogu1080 国王游戏(贪心+高精度)

貌似这道题是碰巧蒙对了贪心的方式..就是把ai*bi越小的放在越前面

(不过也符合直觉)

然后统计答案需要用高精度,然后就调了一年

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #include<vector>
 5 #include<queue>
 6 #include<cmath>
 7 #include<ctime>
 8 #define LL long long int
 9 using namespace std;
10 const int maxn=4400;
11 
12 LL rd(){
13    LL x=0;char c=getchar();int neg=1;
14    while(c<'0'||c>'9'){if(c=='-') neg=-1;c=getchar();}
15    while(c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
16    return x*neg;
17 }
18 
19 int N,a0,b0;
20 struct Node{
21     int a,b,c;
22 }p[maxn];
23 int A[maxn],ans[maxn],tmp[maxn];
24 
25 inline bool cmp(Node a,Node b){return a.c<b.c;}
26 
27 void times(int x){
28     memset(tmp,0,sizeof(tmp));
29     for(int i=1;i<=A[0];i++){
30         A[i]=A[i]*x;
31         tmp[i+1]=A[i]/10;
32         A[i]%=10;
33     }for(int i=1;i<=A[0]+5;i++){
34         A[i]+=tmp[i];tmp[i+1]+=A[i]/10;A[i]%=10;
35         if(A[i]) A[0]=max(A[0],i);
36     }
37 }
38 void div(int x){
39     memset(tmp,0,sizeof(tmp));int t=0;
40     bool bigger=0;
41     for(int i=A[0];i;i--){
42         t=t*10+A[i];
43         if(t>=x){    
44             tmp[i]=t/x;t%=x;
45             tmp[0]=max(tmp[0],i);
46             if(tmp[0]<ans[0]) return;
47             if(tmp[i]<ans[i]&&!bigger) return;
48             if(tmp[i]>ans[i]) bigger=1;
49         }if(bigger) ans[i]=tmp[i];
50     }ans[0]=max(tmp[0],ans[0]);
51 }
52 
53 void output(int *a){
54     for(int i=a[0];i;i--) printf("%d",a[i]);
55     if(!a[0]) printf("0");
56     printf("\n");
57 }
58 
59 int main(){
60     int i,j,k;
61     N=rd();a0=rd();b0=rd();
62     for(i=1;i<=N;i++) p[i].a=rd(),p[i].b=rd(),p[i].c=p[i].a*p[i].b;
63     sort(p+1,p+N+1,cmp);
64     
65     A[0]=1;A[1]=1;times(a0);
66     div(p[1].b);times(p[1].a);
67     for(i=2;i<=N;i++){
68         
69         div(p[i].b);
70         times(p[i].a);
71         
72     }output(ans);
73     return 0;
74 }

猜你喜欢

转载自www.cnblogs.com/Ressed/p/9394564.html