#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<ctime>
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<vector>
#include<stack>
#include<queue>
#include<map>
using namespace std;
#define ll long long
#define N 10010
#define inf 1100000000
#define linf 999999999999999LL
inline const int read(){
register int x=0,f=1;
register char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
inline const char in(){
for(register char ch=getchar();;ch=getchar()) if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')) return ch;
}
int n,a[N],sum[N];
int res,ans=0x7fffffff;
bool vis[N];
struct node{
int l,r;
}d[N];
int top,s[N];
/*void check(){
res=0;
memset(sum,0,sizeof sum);
sum[1]=d[0].l;
for(int i=2;i<=n;i++) sum[i]=sum[i-1]*d[a[i-1]].l;
for(int i=1;i<=n;i++) res=max(res,sum[i]/d[a[i]].r);
}
void dfs(int x){
if(x==n+1){
check();
ans=min(ans,res);
return ;
}
for(int i=1;i<=n;i++){
if(!vis[i]){
vis[i]=1;
a[x]=i;
dfs(x+1);
vis[i]=0;
}
}
}*/
void check(){
res=0;
memset(sum,0,sizeof sum);
sum[1]=d[0].l;
for(int i=2;i<=n;i++) sum[i]=sum[i-1]*d[s[i-1]].l;
for(int i=1;i<=n;i++) res=max(res,sum[i]/d[s[i]].r);
}
void dfs(){
s[top=1]=0;
while(top>=1){
if(top==n+1){
/*for(int i=1;i<=n;i++)
printf("%d ",s[i]);
putchar('\n');*/
check();
ans=min(ans,res);
vis[s[--top]]=0;
continue;
}
do
s[top]++;
while(vis[s[top]]&&s[top]<=n);
if(s[top]<=n)
vis[s[top]]=1,s[++top]=0;
else
vis[s[--top]]=0;
}
}
inline bool cmp1(const node &a,const node &b){
return a.l<b.l;
}
inline bool cmp2(const node &a,const node &b){
return a.l>b.l;
}
int main(){
srand(time(0));
n=read();
d[0].l=read();d[0].r=read();
for(int i=1;i<=n;i++) d[i].l=read(),d[i].r=read();
if(n<=10){
//dfs(1);
dfs();
printf("%d\n",ans);
return 0;
}
sum[1]=d[0].l;
sort(d+1,d+n+1,cmp1);
for(int i=2;i<=n;i++) sum[i]=sum[i-1]*d[i-1].l;
for(int i=1;i<=n;i++) res=max(res,sum[i]/d[i].r);
ans=min(ans,res);res=0;
sort(d+1,d+n+1,cmp2);
for(int i=2;i<=n;i++) sum[i]=sum[i-1]*d[i-1].l;
for(int i=1;i<=n;i++) res=max(res,sum[i]/d[i].r);
ans=min(ans,res);
for(int T=4;T--;){
res=0;
for(int i=1;i<=n;i++) a[i]=rand()%n+1;
for(int i=2;i<=n;i++) sum[i]=sum[i-1]*d[a[i-1]].l;
for(int i=1;i<=n;i++) res=max(res,sum[i]/d[a[i]].r);
ans=min(ans,res);
}
printf("%d\n",ans);
return 0;
}
假 设任意一种排列是{P1,P2,P3,P4....Pn},称为目标序列。{1,2,3,4,...n},称为原始序列。我们观察目标序列,从后往前,每 次将Pi从原始序列中往后交换直到它位于目标序列的位置。不难发现,序列在任意时刻都是由单调增的一段和排列好的一段组成,且每次交换都能保证结果更差。
#include<cstdio>
#include<algorithm>
using namespace std;
#define ll long long
#define N 10010
struct node{
ll a,b;
bool operator < (const node &t) const{
return a*b<t.a*t.b;
}
}d[N];
ll n,ans,tot=1;
int main(){
scanf("%lld",&n);
scanf("%lld%lld",&d[0].a,&d[0].b);
for(int i=1;i<=n;i++) scanf("%lld%lld",&d[i].a,&d[i].b);
sort(d+1,d+n+1);
for(int i=1;i<=n;i++){
tot*=d[i-1].a;
ans=max(ans,tot/d[i].b);
}
printf("%lld\n",ans);
return 0;
}
输入数据 (显示前20行)
100
70 94
43 9
92 18
18 9
86 31
24 32
46 49
23 69
40 56
27 75
28 85
37 29
99 80
44 70
14 9
30 38
46 32
93 87
42 49
35 60
99 73
57 8
38 35
73 33
6 32
10 36
78 75
49 98
50 48
91 78
18 3
86 24
18 84
27 28
83 25
15 95
38 18
50 89
79 9
3 17
1 52
74 32
76 99
24 36
9 43
93 7
65 27
36 84
75 31
94 44
33 2
85 5
42 18
4 33
45 84
92 87
86 34
36 44
61 59
59 28
1 97
60 23
9 64
96 47
57 100
90 7
54 93
17 30
71 23
72 32
14 95
48 40
27 15
92 78
52 11
93 21
56 60
22 47
21 58
89 11
29 13
36 14
95 91
47 12
16 36
19 80
19 92
73 68
66 1
53 97
13 60
83 5
63 99
98 37
2 67
84 95
26 60
63 33
2 78
91 38
9 31
你的答案
< 1622037442854406963
正确答案
> 2166489661101032350678866897536628698296804147316726878162441737980268621335310233327258927458239967674879428851028800069063620140885606400000000000000000
#include<cstdio>
#include<cstring>
#include<algorithm>
#define ref(i,a,b) for(int i=a;i<=b;i++)
#define def(i,a,b) for(int i=a;i>=b;i--)
#define ll long long
using namespace std;
typedef int arr[6005];
struct note{
ll a,b,c;
}a[1005];
bool cmp(note x,note y){
return x.c<y.c;
}
int n,bz;
arr sum,ans,t;
char s[5];
void divv(int x){
int yu=0;
memset(t,0,sizeof(t));
def(i,sum[0],1){
yu=yu*10+sum[i];
if(yu>=x){
if(!t[0]) t[0]=i;
t[i]=yu/x;yu%=x;
}
}
}
void mx(){
if(t[0]>ans[0]) memcpy(ans,t,sizeof(ans));
else if(ans[0]==t[0]){
def(i,ans[0],1)
if(t[i]>ans[i]){
memcpy(ans,t,sizeof(ans));
return;
}
else if(ans[i]<t[i]) return;
}
}
void cheng(int x){
arr t;
memset(t,0,sizeof(t));
ref(i,1,sum[0]){
t[i]=t[i]+sum[i]*x;t[i+1]+=t[i]/10;t[i]%=10;
}
for(t[0]=sum[0];t[t[0]+1];) t[++t[0]+1]+=t[t[0]]/10,t[t[0]]%=10;
memcpy(sum,t,sizeof(sum));
}
int main(){
freopen("sh.txt","r",stdin);
scanf("%d",&n);scanf("%s",s+1);
def(i,strlen(s+1),1) sum[++sum[0]]=s[i]-'0';
scanf("%d",&bz);
ref(i,1,n) scanf("%lld%lld",&a[i].a,&a[i].b),a[i].c=a[i].a*a[i].b;
sort(a+1,a+n+1,cmp);
ref(i,1,n){
divv(a[i].b);
mx();
cheng(a[i].a);
}
def(i,ans[0],1) printf("%d",ans[i]);
return 0;
}