2018 计蒜之道 复赛 A

题目链接

题面:

贝壳找房的攻城狮最近在研究一次函数 f(x) = ax + bf(x)=ax+b

现在有 nn 个一次函数,f_i(x) = a_ix+b_i,\ i = \{1 \mathellipsis n\}fi(x)=aix+bi, i={1n}

容易发现,一次函数嵌套一次函数,还是一次函数。

\displaystyle f_{i}(f_{j}(x)) = a_{i} ( a_{j}x + b_{j}) + b_{i}fi(fj(x))=ai(ajx+bj)+bi

给定 xx,并且对于所有的 f_i(x)fi(x) 可以任意改变顺序嵌套函数,求 f(f(f(…f(x))…)f(f(f(f(x))) 的最大值。

思路:

其实就是一个排序问题,一开始我直接按b从大到小排序,交上去wa了,发现有点问题,仔细想了想,其实应该是一个按贡献排序,ai*bj>aj*bi逻辑较大的在内层那么之后所有的ai乘起来肯定最大

代码:

#include<algorithm>
#include<typeinfo>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<iomanip>
#include<stdio.h>
#include<math.h>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
#define pi acos(-1)
#define mod (1e9+7)
#define fin(num,n) for(int aaa=0;aaa<n;cin>>num[aaa++])
#define fout(num,n) for(int aaa=0;aaa<n;cout<<num[aaa++]<<(aaa==n-1?'\n':' '))
#define ffin(num,n,m) for(int aaa=0;aaa<n;aaa++)for(int bbb=0;bbb<m;cin>>num[aaa][bbb++])
#define ffout(num,n,m) for(int aaa=0;aaa<n;aaa++)for(int bbb=0;bbb<m;cout<<num[aaa][bbb++]<<(bbb==m-1?'\n':' '))
ll gcd(ll x, ll y) { return x ? gcd(y%x, x) : y; }
ll lcm(ll x, ll y) { return x * y / gcd(x, y); }

int t, n, x;
pair<int, int>num[100005];
int cmp(pair<int, int> x, pair<int, int> y) {
	return x.first*y.second + x.second < y.first*x.second + y.second;
}

int main() {
	ios::sync_with_stdio(false);
	cout << fixed << setprecision(0);

	while (cin >> t)
		while (t--) {
			cin >> n >> x;
			for (int a = 0; a < n; a++)
				cin >> num[a].first;
			for (int a = 0; a < n; a++)
				cin >> num[a].second;
			sort(num, num + n, cmp);
			for (int a = 0; a < n; a++)
				x *= num[a].first, x += num[a].second, x %= 10;
			cout << x << endl;
		}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值