P1779國王遊戲請 登入 後遞交
標籤:NOIP提高組2012[顯示標籤]
描述
恰逢H國國慶,國王邀請n位大臣來玩一個有獎遊戲。首先,他讓每個大臣在左、右手上面分別寫下一個整數,國王自己也在左、右手上各寫一個整數。然後,讓這n位大臣排成一排,國王站在隊伍的最前面。排好隊後,所有的大臣都會獲得國王獎賞的若干金幣,每位大臣獲得的金幣數分別是:排在該大臣前面的所有人的左手上的數的乘積除以他自己右手上的數,然後向下取整得到的結果。
國王不希望某一個大臣獲得特別多的獎賞,所以他想請你幫他重新安排一下隊伍的順序,使得獲得獎賞最多的大臣,所獲獎賞儘可能的少。注意,國王的位置始終在隊伍的最前面。
格式
輸入格式
第一行包含一個整數n,表示大臣的人數。
第二行包含兩個整數a和b,之間用一個空格隔開,分別表示國王左手和右手上的整數。接下來n行,每行包含兩個整數a和b,之間用一個空格隔開,分別表示每個大臣左手和右手上的整數。
輸出格式
輸出只有一行,包含一個整數,表示重新排列後的隊伍中獲獎賞最多的大臣所獲得的金幣數。
範例1
範例輸入1[複製]
3
1 1
2 3
7 4
4 6
範例輸出1[複製]
2
限制
每個測試點1s
提示
對於20%的資料,有1≤ n≤ 10,0 < a、b < 8;
對於40%的資料,有1≤ n≤20,0 < a、b < 8;
對於60%的資料,有1≤ n≤100;
對於60%的資料,保證答案不超過10^9;
對於100%的資料,有1 ≤ n ≤1,000,0 < a、b < 10000。
來源
Noip2012提高組複賽Day1T2
貪心思路:
相乘貪心
*
*
*
i
j
*
*
如果像這樣排隊,i,j交換不影響*人,前面乘積為sum
如果i在前面,取max(sum/ri,sum*li/rj)
如果j在前面,取max(sum/rj,sum*lj/ri)
當前情況取最值,也就是在max(sum*li/rj,sum*lj/ri)
同乘,可以發現如果li*ri>lj*rj,
也就是如果i在前面時,乘積更大,也就是要把乘積大的往後放。
#include<iostream>#include<cstdio>#include<ctime>#include<cstdlib>#include<cmath>#include<cstring>#include<string>#include<set>#include<map>#include<vector>#include<queue>#include<algorithm>#ifdef WIN32#define AUTO "%I64d"#else#define AUTO "%lld"#endif#define INF 0x3f3f3f3f#define CLOCK CLOCKS_PER_SEC#define cle(x) memset(x,0,sizeof(x))#define maxcle(x) memset(x,0x3f,sizeof(x))#define mincle(x) memset(x,-1,sizeof(x))#define maxx(x1,x2,x3) max(x1,max(x2,x3))#define minn(x1,x2,x3) min(x1,min(x2,x3))#define cop(a,x) memcpy(x,a,sizeof(a))#define FROP "hdu"#define LL long long#define smin(x,tmp) x=min(x,tmp)#define smax(x,tmp) x=max(x,tmp)using namespace std;const int N = 1005;struct bigint{ static const int P =1,M=10; int w[4050]; bigint(){cle(w);w[0]=1;} void read() { string s; cin>>s; int now=1,c1=1,ct=0; for(int i = s.size()-1;i>=0;i--) { w[now]+=(s[i]-'0')*c1; c1*=10; ct++; if(ct==P&&i){c1=1;ct=0;now++;} } w[0]=now; } bigint operator * (const int &b) { bigint a=*this; bigint c; int &len=c.w[0]; len=a.w[0]+5; for(int i = 1; i <= len;i++) { c.w[i]+=a.w[i]*b; c.w[i+1]=c.w[i]/M; c.w[i]%=M; } while(len>1&&!c.w[len])len--; return c; } bigint operator /(const int &b) { bigint a=*this; bigint c; int &len=c.w[0]; len=a.w[0]; int tmp=0; for(int i = len; i ;i--) { tmp+=a.w[i]; if(tmp>=b) { c.w[i]+=tmp/b; tmp%=b; } tmp*=M; } while(len>1&&!c.w[len])len--; return c; } bool operator < (const bigint &b)const { bigint a=*this; if(a.w[0]^b.w[0])return a.w[0]<b.w[0]; for(int i = a.w[0];i;i--) if(a.w[i]^b.w[i])return a.w[i]<b.w[i]; return false; } void print() { printf("%d",w[w[0]]); for(int i = w[0]-1;i;i--) printf("%0*d",P,w[i]); }}L,R;int n;struct ii{ int l,r; ii(int l =0,int r=0):l(l),r(r){ } bool operator < (const ii &b)const { return l*r<b.l*b.r; }}node[N];int main(){ freopen(FROP".in","r",stdin); freopen(FROP".out","w",stdout); scanf("%d",&n); L.read(); R.read(); for(int i = 1;i <= n; i++) { int x,y; scanf("%d%d",&x,&y); node[i]=ii(x,y); } sort(node+1,node+n+1); bigint ans,tmp=L; for(int i = 1; i <= n; i++) { bigint x=tmp/node[i].r; if(ans<x)ans=x; tmp=tmp*node[i].l; } ans.print(); return 0;}