题目出自 Codeforces Round #126 (Div. 2) 的E。

题意大致如下:给定a,b,c,s,求三个非负整数x,y,z,满足0<=x<=y<=z,ax+by+cz=s,使得f(x,y,z)=|ax-by|+|by-cz|最小

思路:枚举z,得到一个方程ax+by=s-cz,用扩展欧几里得求出这个方程的一个解,然后三分通解的整系数,求出最小f值。至于为什么可以三分画画图就清楚了,两个绝对值函数叠加在一起最多只有三种状态(第一维表示临界点较小的那个绝对值函数):(降,降),(升,降),(升,升),无论两个函数哪个变化快,最终趋势都是:降然后升(由于临界点的情况不同,可能变成了单调的,但并不影响我们用三分求解)。

思来想去,决定搞一个三分的框架来避免头疼的临界问题(这是求最小值,求最大值时只需把<=换成>=即可,另外函数值在一段范围内不发生变化可能导致结果出错):

1
2
3
4
5
6
7
int L = ..., R = ...;
while (L < R) {
    int M1 = L + (R - L) / 3, M2 = R - (R - L) / 3;
    if (F(M1) <= F(M2)) R = M2 - 1;
    else L = M1 + 1; 
solve(L);

出while循环后 L=R=目标解

下面是题目的源码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/* ******************************************************************************** */
#include <iostream>                                                                 //
#include <cstdio>                                                                   //
#include <cmath>                                                                    //
#include <cstdlib>                                                                  //
#include <cstring>                                                                  //
#include <vector>                                                                   //
#include <ctime>                                                                    //
#include <deque>                                                                    //
#include <queue>                                                                    //
#include <algorithm>                                                                //
#include <map>                                                                      //
#include <cmath>                                                                    //
using namespace std;                                                                //
                                                                                    //
#define pb push_back                                                                //
#define mp make_pair                                                                //
#define X first                                                                     //
#define Y second                                                                    //
#define all(a) (a).begin(), (a).end()                                               //
#define fillchar(a, x) memset(a, x, sizeof(a))                                      //
                                                                                    //
typedef pair<intint> pii;                                                         //
typedef long long ll;                                                               //
typedef unsigned long long ull;                                                     //
                                                                                    //
#ifndef ONLINE_JUDGE                                                                //
void RI(vector<int>&a,int n){a.resize(n);for(int i=0;i<n;i++)scanf("%d",&a[i]);}    //
void RI(){}void RI(int&X){scanf("%d",&X);}template<typename...R>                    //
void RI(int&f,R&...r){RI(f);RI(r...);}void RI(int*p,int*q){int d=p<q?1:-1;          //
while(p!=q){scanf("%d",p);p+=d;}}void print(){cout<<endl;}template<typename T>      //
void print(const T t){cout<<t<<endl;}template<typename F,typename...R>              //
void print(const F f,const R...r){cout<<f<<", ";print(r...);}template<typename T>   //
void print(T*p, T*q){int d=p<q?1:-1;while(p!=q){cout<<*p<<", ";p+=d;}cout<<endl;}   //
#endif // ONLINE_JUDGE                                                              //
template<typename T>bool umax(T&a, const T&b){return b<=a?false:(a=b,true);}        //
template<typename T>bool umin(T&a, const T&b){return b>=a?false:(a=b,true);}        //
template<typename T>                                                                //
void V2A(T a[],const vector<T>&b){for(int i=0;i<b.size();i++)a[i]=b[i];}            //
template<typename T>                                                                //
void A2V(vector<T>&a,const T b[]){for(int i=0;i<a.size();i++)a[i]=b[i];}            //
                                                                                    //
const double PI = acos(-1.0);                                                       //
const int INF = 1e9 + 7;                                                            //
                                                                                    //
/* -------------------------------------------------------------------------------- */
 
ll x, y, z, a, b, c, a0, b0;
 
ll gcd(ll a, ll b) {
    return b? gcd(b, a % b) : a;
}
void gcd(ll a, ll b, ll &d, ll &x, ll &y) {
    if (!b) {
        d = a; x = 1; y = 0;
    }
    else {
        gcd(b, a % b, d, y, x);
        y -= x * (a / b);
    }
}
ll f(ll k) {
    ll xx = x + k * b0, yy = y - k * a0;
    return abs(xx * a - yy * b) + abs(yy * b - z * c);
}
bool chk(ll k1, ll k2) {
    if (x + k1 * b0 < 0) return false;
    if (x + k1 * b0 > z) return true;
    if (y - k1 * a0 < 0) return true;
    if (y - k1 * a0 > z) return false;
    if (x + k2 * b0 < 0) return false;
    if (x + k2 * b0 > z) return true;
    if (y - k2 * a0 < 0) return true;
    if (y - k2 * a0 > z) return false;
    if (x + k1 * b0 > y - k1 * a0) return true;
    if (x + k2 * b0 > y - k2 * a0) return true;
    return f(k1) <= f(k2);
}
int main() {
#ifndef ONLINE_JUDGE
    freopen("in.txt""r", stdin);
    //freopen("out.txt", "w", stdout);
#endif // ONLINE_JUDGE
    int n, s;
    cin >> n >> s;
    int cnt[3] = {};
    for (int i = 0; i < n; i ++) {
        int x;
        scanf("%d", &x);
        cnt[x - 3] ++;
    }
    a = cnt[0], b = cnt[1], c = cnt[2];
    ll ans = INF, ix, iy, iz;
    for (z = 1; z * c <= s; z ++) {
        ll g;
        gcd(a, b, g, x, y);
        if ((s - z * c) % g) continue;
        ll K = (s - z * c) / g;
        x *= K;
        y *= K;
        a0 = a / g;
        b0 = b / g;
        ll L = -INF, R = INF;
        while (L < R) {
            ll M1 = L + (R - L) / 3, M2 = R - (R - L) / 3;
            if (chk(M1, M2)) R = M2 - 1;
            else L = M1 + 1;
        }
        ll xx = x + L * b0, yy = y - L * a0;
        if (0 <= xx && xx <= yy && yy <= z) {
            if (umin(ans, f(L))) {
                ix = xx;
                iy = yy;
                iz = z;
            }
        }
    }
    if (ans < INF) cout << ix << " " << iy << " " << iz << endl;
    else puts("-1");
    return 0;
}
/* ******************************************************************************** */

[codeforces 200 E Tractor College]枚举,扩展欧几里得,三分的更多相关文章

  1. UVA 12169 Disgruntled Judge 枚举+扩展欧几里得

    题目大意:有3个整数 x[1], a, b 满足递推式x[i]=(a*x[i-1]+b)mod 10001.由这个递推式计算出了长度为2T的数列,现在要求输入x[1],x[3],......x[2T- ...

  2. Codeforces Round #451 (Div. 2) B. Proper Nutrition【枚举/扩展欧几里得/给你n问有没有两个非负整数x,y满足x·a + y·b = n】

    B. Proper Nutrition time limit per test 1 second memory limit per test 256 megabytes input standard ...

  3. UVa 12169 (枚举+扩展欧几里得) Disgruntled Judge

    题意: 给出四个数T, a, b, x1,按公式生成序列 xi = (a*xi-1 + b) % 10001 (2 ≤ i ≤ 2T) 给出T和奇数项xi,输出偶数项xi 分析: 最简单的办法就是直接 ...

  4. BZOJ2800 [Poi2012]Leveling Ground 【扩展欧几里得 + 三分 + 堆】

    题目链接 BZOJ2800 题解 区间加极难操作,差分之后可转化为两点一加一减 那么现在问题就将每个点暂时独立开来 先判定每个点是否被\((A,B)\)整除,否则无解 之后我们先将\(A,B\)化为互 ...

  5. [zoj3593]扩展欧几里得+三分

    题意:给一个数A,有6种操作,+a,-a,+b,-b,+(a+b),-(a+b),每次选择一种,用最少的次数变成B. 思路:由于不同的操作先后顺序对最后的结果没有影响,并且加一个数与减一个相同的数不能 ...

  6. 【扩展欧几里得】BAPC2014 I Interesting Integers (Codeforces GYM 100526)

    题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...

  7. 【数论】【扩展欧几里得】Codeforces 710D Two Arithmetic Progressions

    题目链接: http://codeforces.com/problemset/problem/710/D 题目大意: 两个等差数列a1x+b1和a2x+b2,求L到R区间内重叠的点有几个. 0 < ...

  8. codeforces 1244C (思维 or 扩展欧几里得)

    (点击此处查看原题) 题意分析 已知 n , p , w, d ,求x , y, z的值 ,他们的关系为: x + y + z = n x * w + y * d = p 思维法 当 y < w ...

  9. Codeforces 7C 扩展欧几里得

    扩展欧几里得是计算 ax + by = gcd(a,b) 的 x,y的整数解. 现在是ax + by + c = 0; 只要 -c 是 gcd(a,b) 的整数倍时有整数解,整数解是 x = x*(- ...

随机推荐

  1. redis的安装(ubuntu版本)

    1.使用apt-get命令进行安装 安装gcc依赖 root@yatces-virtual-machine:~# apt-get update root@yatces-virtual-machine: ...

  2. 【LeetCode】 99. Recover Binary Search Tree [Hard] [Morris Traversal] [Tree]

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

  3. Python - Python算法之冒泡算法的超简单实现

    [原创]转载请注明作者Johnthegreat和本文链接 冒泡排序在算法中算是最简单也最容易实现的,这里介绍一个非常简单实现的代码: def bubble_sort(ls): for first in ...

  4. Ansible playbook Vault 加密

    Ansible playbook Vault 加密详解与使用案例 主机规划 添加用户账号 说明: 1. 运维人员使用的登录账号: 2. 所有的业务都放在 /app/ 下「yun用户的家目录」,避免业务 ...

  5. VideoView--简单获取进度条的方法

    使用MediaController类就可以简单的把视频中的进度条加进去 实例: 现在布局哪里放一个VideoView,然后: videoView = (VideoView) findViewById( ...

  6. Python学习16之input函数

    '''''''''Input函数:作用:接受一个标准输入数据返回值:返回为 string 类型使用:input()'''a=input("请输入一个整数")print(a)prin ...

  7. Android:RelativeLayout 内容居中

    Android RelativeLayout 内容居中解决办法: 使用Linearlayout本来利用父控件的gravity属性是很好解决的.但是对应RelativeLayout虽然有gravity属 ...

  8. numpy库的学习笔记

    一.ndarray 1.numpy 库处理的最基础数据类型是由同种元素构成的多维数组(ndarray),简称“数组”. 2.ndarray是一个多维数组的对象,ndarray数组一般要求所有元素类型相 ...

  9. MATLAB学习1 之画图函数

    ezplot适用条件 "ezplot"命令可以用于显函数.隐函数和参数方程作图. 不同函数的使用格式 显函数y=f(x),ezplot函数的调用格式为ezplot(f, [xmin ...

  10. 基于JSR-356实现的Tyrus WebSocket框架的消息传递机制初步了解

    对阻塞.非阻塞,同步.异步初步了解了,不是太明白,其中大多数将的是对于CPU的使用率及对一个事件的处理效率. 阻塞与非阻塞各有优缺点,在知乎上也看到了下面的结论: 在处理 IO 的时候,阻塞和非阻塞都 ...