Codeforces Round #206 (Div. 2) E. Vasya and Beautiful Arrays
来源:互联网 发布:win10下载软件打不开 编辑:程序博客网 时间:2024/04/29 10:14
比赛时貌似出了挺多的,我根本不会,最怕这种需要智商的题目了,得好好分析才能做,看了一个AC代码觉得很神学习一下。
显然我们要从所有已知的最小值开始测试,但是每次检查最坏是n的时间,显然要超时,怎么加速?
有一行关键代码是这样的,x = c[i] / (c[i] / x + 1); 设X是当前测试其是否可行的值,如果c[i] % x > k 则x将不可行,但是下一个有可能的可行的数是什么呢?x-1?有没有可能每步跨度大一些? 我们分析一下,假设 c[i] = t * x + l, l > k, 下一个可能可行的设为y, y < x 如果 c[i] = t * y + s, 于是可以推出是 s > t > k, 所以 c[i] 除以 y的商至少要比 t 大1,否则不可能余数小于k。于是我们就有了一个加速优化。
代码如下:
#include<iostream>#include<cstdio>#include<cmath>#include<cstring>#include<cstdlib>#include<queue>#include<algorithm>#include<stack>#include<deque>#include<list>#include<set>#include<vector>#include<iomanip>#include<cctype>#include<string>#include<memory>#include<map>#include<sstream>#pragma warning (disable : 4996)#define mem(a) memset(a, 0, sizeof(a))#define sl(a) strlen(a)#define LL long long#define dou doubleconst int Mod = 1000000007;const int N = 300005;using namespace std;int c[N];int main(){/*freopen("in.txt", "r", stdin);freopen("out.txt", "w",stdout);*/int re, n, m, i, j, k, mi = 100000000, x;cin >> n >> k;for (i = 0; i < n; ++i){scanf("%d", c + i);mi = mi > c[i] ? c[i] : mi;}sort(c, c + n);if (k >= mi) cout << mi;else{x = mi;while (1){for (i = 0; i < n; ++i){if (c[i] % x > k) {x = c[i] / (c[i] / x + 1); break;}}if (i == n) {printf("%d", x); break;}}}return 0;}有一点没想通,一开始没有加排序的那一句,觉得会更快,结果Test #41 TLE了,加上这句话以后反而只需要78ms,不能理解啊!排序以后测试是从小的数开始的,但是这样会快呢?不满足的点总是在数据小的时候更容易被判掉么?求指教。。。。
- Codeforces Round #206 (Div. 2) E. Vasya and Beautiful Arrays
- Codeforces Round #206 (Div. 2) E-Vasya and Beautiful Arrays
- Codeforces #206(Div.1) C. Vasya and Beautiful Arrays
- [思路题] Codeforces 354C #206 (Div. 1) C. Vasya and Beautiful Arrays
- Codeforces Round #206 (Div. 2) C - Vasya and Robot
- Codeforces Round #206 (Div. 2) A. Vasya and Digital Root
- Codeforces Round #206 (Div. 2) B. Vasya and Public Transport
- Codeforces Round #206 (Div. 2) C. Vasya and Robot
- Codeforces Round #206 (Div. 2) C-Vasya and Robot
- Codeforces Round #281 (Div. 2) E. Vasya and Polynomial 数学 思考题
- Codeforces Round #425 (Div. 2) E-Vasya and Shifts (高斯消元)
- Codeforces 354C Vasya and Beautiful Arrays[dp+暴力]
- Codeforces 354C. Vasya and Beautiful Arrays【DP,暴力】
- codeforces 354C C. Vasya and Beautiful Arrays( 数论+枚举)
- Codeforces Round #206 (Div. 1) A. Vasya and Robot
- Codeforces Round #262 (Div. 2) A. Vasya and Socks
- Codeforces Round #281 (Div. 2) A. Vasya and Football
- Codeforces Round #281 (Div. 2) A - Vasya and Football
- Android开机画面的修改
- UBUNTU默认root密码,如何修改root密码
- C#中的Attribute 特性入门总结--从DotNET平台调用Win32 API
- javascript获取网页中指定节点的父节点、子节点的方法小结
- android AsyncTask介绍
- Codeforces Round #206 (Div. 2) E. Vasya and Beautiful Arrays
- 俞敏洪北大演讲
- 图片放大镜
- 什么是能做事业的人?
- 用vim编辑编译执行一个c程序(初学者对编译器的认识)
- 粒子滤波
- Vasya and Robot
- 常用Git代码托管服务分享
- naoqi framework之 Broker Proxy Module Method