G. 机器人 (排序&__int128) (2021牛客寒假算法基础集训营6)

传送门

注意:题目描述的将机器人排序,指机器人之间交换,自身的a、b值依然不变。另外,题目中x=ai*x+bi,x在不断更新,最后得到一个答案x。

思路:

显然我们要将机器人按一定方式进行排序,以得到最大的答案。

嵌套公式可以得到公式 a_{1}(a_{2}x+b_{2})+b_{1} < a_{2}(a_{1}x+b_{1})+b_{2},化简贪心一下就好。

另外需要注意20^{20}爆long long,所以需要用__int128。

代码实现:

#include<bits/stdc++.h>
//#define endl '\n'
#define null NULL
#define ll long long
#define int long long
#define pii pair<int, int>
#define lowbit(x) (x &(-x))
#define ls(x) x<<1
#define rs(x) (x<<1+1)
#define me(ar) memset(ar, 0, sizeof ar)
#define mem(ar,num) memset(ar, num, sizeof ar)
#define rp(i, n) for(int i = 0, i < n; i ++)
#define rep(i, a, n) for(int i = a; i <= n; i ++)
#define pre(i, n, a) for(int i = n; i >= a; i --)
#define IOS ios::sync_with_stdio(0); cin.tie(0);cout.tie(0);
const int way[4][2] = {
   
   {1, 0}, {-1, 0}, {0, 1}, {0, -1}};
using namespace std;
const int  inf = 0x3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-6;
const ll   mod = 1e9+7;
const int  N = 2e5 + 5;

//inline void read(int &x){
//    char t=getchar();
//    while(!isdigit(t)) t=getchar();
//    for(x=t^48,t=getchar();isdigit(t);t=getchar()) x=x*10+(t^48);
//}

inline __int128 read(){
    __int128 x = 0, f = 1;
    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 void write(__int128 x){
    if(x<0){
        putchar('-');
        x = -x;
    }
    if(x>9) write(x/10);
    putchar(x%10+'0');
}

struct node{
    int a, b;
    bool operator < (const node &x) const{
        return a*x.b+b<x.a*b+x.b;
    }
}g[50];

signed main()
{
//    IOS;

    int n; cin >> n;
    __int128 x = read();
    for(int i = 0; i < n; i ++)
        cin >> g[i].a >> g[i].b;
    sort(g, g+n);
    for(int i = 0; i < n; i ++)
        x = g[i].a*x+g[i].b;
    write(x);

    return 0;
}

猜你喜欢

转载自blog.csdn.net/Satur9/article/details/114442653
今日推荐