[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*(- ...
随机推荐
- 【JAVA】并发-基础IO
一.java.io包支持.java的IO流有输入.输出两种,每种输入.输出流又可分为字节流.字符流两大类,字节流以字节为单位处理IO操作,字符流以字符为单位处理IO操作 JDK 1.4以后有java. ...
- Springboot:员工管理之国际化(十(3))
1:IDEA编码设置UTF-8 2:创建国际化文件 i18n\login.properties #默认语言 i18n\login_en_US.properties #英文语言 i18n\login_z ...
- Thymeleaf+SpringBoot+Mybatis实现的齐贤易游网旅游信息管理系统
项目简介 项目来源于:https://github.com/liuyongfei-1998/root 本系统是基于Thymeleaf+SpringBoot+Mybatis.是非常标准的SSM三大框架( ...
- 理解java容器底层原理--手动实现HashMap
HashMap结构 HashMap的底层是数组+链表,百度百科找了张图: 先写个链表节点的类 package com.xzlf.collection2; public class Node { int ...
- Linux下jdk的安装和环境变量的配置
Linux下jdk的安装和环境变量的配置 一.jdk的下载 方式一:在官网下载 http://www.oracle.com/technetwork/java/javase/downloads/inde ...
- 使用openmp进行共享内存编程
预处理指令pragma:在系统中加入预处理器指令一般是用来允许不是基本c语言规范部分的行为.不支持pragma的编译器会忽略pragma指令提示的那些语句,这样就允许使用pragma的程序在不支持它们 ...
- 手机app抓包[小米]
方案一:(手机电脑在同一wifi下) 打开burp设置代理 这里的ip为电脑的ip 手机手动设置代理为电脑的ip+8080 导入证书 电脑上下载下证书(http://burp) 传到手机上
- MySQL的远程链接
安装好我们的mMySQL,是不是也有种无从下手的感觉,不用怕,接下来我们可以使用远程连接来可视化我们的数据库的数据: 1. 打开我们的数据库,帐号是root,密码查看文件就行了 2.使用命令: mys ...
- python学习06循环
'''while''''''while 布尔表达式:冒号不能省略''''''1+2+3+...+10'''i=1sum1=0while i<=10: sum1+=i i+=1print(sum1 ...
- @SessionAttributes 和 @SessionAttribute的区别
@SessionAttributes 和 @SessionAttribute的区别 Spring MVC中有两个长得非常像的注解:@SessionAttributes 和 @SessionAttrib ...