贝壳找房函数最值

题解:考虑两个函数 a * ( c * x + d ) + b = ac * x + ad + b,c * ( a * x + b ) + d = ac * x + cb + d,说明和x的系数没有关系,所以按照 ad + b < cb + d排序求解即可。

哎,第一次爆o,按照提示想了个排序,a小的优先,当a相等时b大的优先,结果一直wa。。。。wa,惨烈的教训告诉我下次排序要仔细推一波。

 1 #include<bits/stdc++.h>
 2 #define maxn (int)1e4 + 7
 3 using namespace std;
 4 
 5 struct node{
 6     int a, b;
 7     bool operator < (const node& i) const { return a * i.b + b < i.a * b + i.b; }
 8 } c[maxn];
 9 
10 int T, n, x;
11 
12 int main()
13 {
14     cin >> T;
15     while(T--){
16         cin >> n >> x;
17         for(int i = 1; i <= n; i++) cin >> c[i].a;
18         for(int i = 1; i <= n; i++) cin >> c[i].b;
19         sort(c + 1, c + n + 1);
20         
21         int res = x;
22         for(int i = 1; i <= n; i++){
23             res = (res * c[i].a + c[i].b) % 10;
24         }
25         cout << res << endl;
26     }
27     return 0;
28 }

猜你喜欢

转载自www.cnblogs.com/zgglj-com/p/9193578.html