[codeforces 200 E Tractor College]枚举,扩展欧几里得,三分
题目出自 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<int, int> 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]枚举,扩展欧几里得,三分的更多相关文章
- 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- ...
- 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 ...
- UVa 12169 (枚举+扩展欧几里得) Disgruntled Judge
题意: 给出四个数T, a, b, x1,按公式生成序列 xi = (a*xi-1 + b) % 10001 (2 ≤ i ≤ 2T) 给出T和奇数项xi,输出偶数项xi 分析: 最简单的办法就是直接 ...
- BZOJ2800 [Poi2012]Leveling Ground 【扩展欧几里得 + 三分 + 堆】
题目链接 BZOJ2800 题解 区间加极难操作,差分之后可转化为两点一加一减 那么现在问题就将每个点暂时独立开来 先判定每个点是否被\((A,B)\)整除,否则无解 之后我们先将\(A,B\)化为互 ...
- [zoj3593]扩展欧几里得+三分
题意:给一个数A,有6种操作,+a,-a,+b,-b,+(a+b),-(a+b),每次选择一种,用最少的次数变成B. 思路:由于不同的操作先后顺序对最后的结果没有影响,并且加一个数与减一个相同的数不能 ...
- 【扩展欧几里得】BAPC2014 I Interesting Integers (Codeforces GYM 100526)
题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...
- 【数论】【扩展欧几里得】Codeforces 710D Two Arithmetic Progressions
题目链接: http://codeforces.com/problemset/problem/710/D 题目大意: 两个等差数列a1x+b1和a2x+b2,求L到R区间内重叠的点有几个. 0 < ...
- codeforces 1244C (思维 or 扩展欧几里得)
(点击此处查看原题) 题意分析 已知 n , p , w, d ,求x , y, z的值 ,他们的关系为: x + y + z = n x * w + y * d = p 思维法 当 y < w ...
- Codeforces 7C 扩展欧几里得
扩展欧几里得是计算 ax + by = gcd(a,b) 的 x,y的整数解. 现在是ax + by + c = 0; 只要 -c 是 gcd(a,b) 的整数倍时有整数解,整数解是 x = x*(- ...
随机推荐
- codeforces Equalizing by Division (easy version)
output standard output The only difference between easy and hard versions is the number of elements ...
- 详解 List接口
本篇博文所讲解的这两个类,都是泛型类(关于泛型,本人在之前的博文中提到过),我们在学习C语言时,对于数据的存储,用的差不多都是数组和链表. 但是,在Java中,链表就相对地失去了它的存在价值,因为Ja ...
- CSS- 一些少用的
1. 字体间距 1.)word-spacing: 2.)letter-spacing: 3.)text-indent 4.)text-align-last: justify; 和 text-align ...
- [linux][MongoDB] mongodb学习(二):命令使用数据库
使用数据库 # 查看数据库 > show dbs admin 0.000GB local 0.000GB # 查看表(集合) > show tables # 删除集合 > db.us ...
- Docker 中如何安装配置 Nginx
拉取 nginx 最新版镜像,然后简单启动一个 nginx 容器: docker pull nginx:latest docker run --name nginx01 -d -p 80:80 ngi ...
- Jmeter系列(7)- 基础线程组Thread Group
如果你想从头学习Jmeter,可以看看这个系列的文章哦 https://www.cnblogs.com/poloyy/category/1746599.html Thread Group基础线程组介绍 ...
- deepin下深度终端使用ssh-agent(xshell中的xagent功能)
背景:从windows10换到deepin后,在连接公司的服务器遇到了问题:windows下用的是xshell,开启xagent后,可直接从公司的跳转板上连接生产服务器:在deepin的深度终端上,从 ...
- thinkphp5.1+layui2.x 时间戳转换为日期格式
layui.use(['table','util'],function(){ var table = layui.table,form = layui.form; table.render({ ele ...
- java中ThreadPool的介绍和使用
文章目录 Thread Pool简介 Executors, Executor 和 ExecutorService ThreadPoolExecutor ScheduledThreadPoolExecu ...
- Scala的存在类型
Scala的存在类型 存在类型也叫existential type,是对类型做抽象的一种方法.可以在你不知道具体类型的情况下,就断言该类型存在. 存在类型用_来表示,你可以把它看成java中的?. 下 ...