做题感悟:在比赛时老马想到了这个方法但是这种方法在无形当中就被忽略了,导致最后十几分钟写二分的方法也没写出来。
解题思路:1)员工在保证钱最多的情况下,剩下的 tokens 尽量多,so ( w * a ) % b 但这并不是最终结果(这样交了一次果断wa),因为 这是 w*a 对 b 取余的结果,这样得到的并不是剩下的 tokens ,还必须除以 a ;
2)二分(补充):你得到的钱最多为 w * a / b ,但是有可能取少于 w 个也能达到这些钱,so 只要不断用二分在 0 ~ w 之间找到最小的 y 使得 y*a/b = w*a / b 即可。
代码:
#include<stdio.h> #include<iostream> #include<map> #include<stack> #include<string> #include<string.h> #include<stdlib.h> #include<math.h> #include<vector> #include<queue> #include<algorithm> using namespace std ; #define lld __int64 const double PI = 3.1415926 ; const double esp = 1e-4 ; const int md= 2810778 ; const int INF = 99999999 ; const int MX = 1000005 ; int main() { lld n,a,b ; while(~scanf("%I64d%I64d%I64d",&n,&a,&b)) { lld x ; for(lld i=0 ;i<n ;i++) { scanf("%I64d",&x) ; if(!i) printf("%I64d",(x*a)%b/a) ; else printf(" %I64d",(x*a)%b/a) ; } printf("\n") ; } return 0 ; }
代码:
#include<stdio.h> #include<iostream> #include<map> #include<stack> #include<string> #include<string.h> #include<stdlib.h> #include<math.h> #include<vector> #include<queue> #include<algorithm> using namespace std ; #define lld __int64 const double PI = 3.1415926 ; const double esp = 1e-4 ; const int md= 2810778 ; const int INF = 99999999 ; const int MX = 1000005 ; lld a,b,n ; lld binary_search(lld le,lld rt,lld n) // 二分找最小的 tokens { lld mid ; while(le<rt) { mid=(le+rt)/2 ; if(mid*a/b==n) rt=mid ; else le=mid+1 ; } return le ; } int main() { while(~scanf("%I64d%I64d%I64d",&n,&a,&b)) { lld x ; for(lld i=0 ;i<n ;i++) { scanf("%I64d",&x) ; lld le=0,rt=x ; lld mx= binary_search(le,rt,(x*a)/b) ;// 返回用了多少tokens if(!i) printf("%I64d",x-mx) ; else printf(" %I64d",x-mx) ; } printf("\n") ; } return 0 ; }