bupt summer training for 16 #3 ——构造
https://vjudge.net/contest/172464
后来补题发现这场做的可真他妈傻逼
A.签到傻逼题,自己分情况
#include <cstdio>
#include <vector>
#include <algorithm> using std::vector;
using std::sort; typedef long long ll; int n, m; ll a[], b[]; ll sa, sb, dis, tmp, qaq; int t1 = -, t2 = -; int a1, a2, a3, a4; struct node {
ll x, y, z;
bool operator < (const node &a) const {
return x < a.x;
}
}; vector <node> e; ll abs(ll x) {
return x < ? -x : x;
} node find1(ll x) {
node ret = {, -, -};
int l = , r = e.size() - , mid;
while(l <= r) {
int mid = (l + r) >> ;
if(e[mid].x * <= x) ret = e[mid], l = mid + ;
else r = mid - ;
}
return ret;
} node find2(ll x) {
node ret = {, -, -};
int l = , r = e.size() - , mid;
while(l <= r) {
int mid = (l + r) >> ;
if(e[mid].x * > x) ret = e[mid], r = mid - ;
else l = mid + ;
}
return ret;
} int main() {
scanf("%d", &n);
for(int i = ;i <= n;i ++)
scanf("%lld", &a[i]), sa += a[i];
scanf("%d", &m);
for(int i = ;i <= m;i ++)
scanf("%lld", &b[i]), sb += b[i];
if(abs(sa - sb) <= ) {
printf("%lld\n0\n", abs(sa - sb));
return ;
}
qaq = tmp = dis = sa - sb;
for(int i = ;i <= n;i ++)
for(int j = ;j <= m;j ++)
if(abs(qaq - (a[i] - b[j]) * ) < abs(dis))
dis = qaq - (a[i] - b[j]) * , t1 = i, t2 = j;
for(int i = ;i <= n;i ++)
for(int j = i + ;j <= n;j ++)
e.push_back((node){a[i] + a[j], i, j});
sort(e.begin(), e.end());
for(int i = ;i <= m;i ++)
for(int j = i + ;j <= m;j ++) {
ll k = b[i] + b[j];
node tt = find1(qaq + k * );
if(tt.y != - && abs(qaq + k * - tt.x * ) < abs(tmp)) tmp = qaq + k * - tt.x * , a1 = tt.y, a2 = i, a3 = tt.z, a4 = j;
tt = find2(qaq + k * );
if(tt.y != - && abs(qaq + k * - tt.x * ) < abs(tmp)) tmp = qaq + k * - tt.x * , a1 = tt.y, a2 = i, a3 = tt.z, a4 = j;
}
if(abs(qaq) <= abs(dis) && abs(qaq) <= abs(tmp)) printf("%lld\n0\n", abs(qaq));
else if(abs(dis) <= abs(tmp)) printf("%lld\n1\n%d %d", abs(dis), t1, t2);
else printf("%lld\n2\n%d %d\n%d %d", abs(tmp), a1, a2, a3, a4);
return ;
}
想写if-else结果写完if忘记else了
B.我是暴力的O(n^2logn)
首先如果只交换一个数,那O(n^2)都会算吧
那交换两个数呢,我把n个数两两结合并求和,再对他们排序
再枚举另一数组的数对,二分一下尝试更新答案
#include <cstdio>
#include <vector>
#include <algorithm> using std::vector;
using std::sort; typedef long long ll; int n, m; ll a[], b[]; ll sa, sb, dis, tmp, qaq; int t1 = -, t2 = -; int a1, a2, a3, a4; struct node {
ll x, y, z;
bool operator < (const node &a) const {
return x < a.x;
}
}; vector <node> e; ll abs(ll x) {
return x < ? -x : x;
} node find1(ll x) {
node ret = {, -, -};
int l = , r = e.size() - , mid;
while(l <= r) {
int mid = (l + r) >> ;
if(e[mid].x * <= x) ret = e[mid], l = mid + ;
else r = mid - ;
}
return ret;
} node find2(ll x) {
node ret = {, -, -};
int l = , r = e.size() - , mid;
while(l <= r) {
int mid = (l + r) >> ;
if(e[mid].x * > x) ret = e[mid], r = mid - ;
else l = mid + ;
}
return ret;
} int main() {
scanf("%d", &n);
for(int i = ;i <= n;i ++)
scanf("%lld", &a[i]), sa += a[i];
scanf("%d", &m);
for(int i = ;i <= m;i ++)
scanf("%lld", &b[i]), sb += b[i];
if(abs(sa - sb) <= ) {
printf("%lld\n0\n", abs(sa - sb));
return ;
}
qaq = tmp = dis = sa - sb;
for(int i = ;i <= n;i ++)
for(int j = ;j <= m;j ++)
if(abs(qaq - (a[i] - b[j]) * ) < abs(dis))
dis = qaq - (a[i] - b[j]) * , t1 = i, t2 = j;
for(int i = ;i <= n;i ++)
for(int j = i + ;j <= n;j ++)
e.push_back((node){a[i] + a[j], i, j});
sort(e.begin(), e.end());
for(int i = ;i <= m;i ++)
for(int j = i + ;j <= m;j ++) {
ll k = b[i] + b[j];
node tt = find1(qaq + k * );
if(tt.y != - && abs(qaq + k * - tt.x * ) < abs(tmp)) tmp = qaq + k * - tt.x * , a1 = tt.y, a2 = i, a3 = tt.z, a4 = j;
tt = find2(qaq + k * );
if(tt.y != - && abs(qaq + k * - tt.x * ) < abs(tmp)) tmp = qaq + k * - tt.x * , a1 = tt.y, a2 = i, a3 = tt.z, a4 = j;
}
if(abs(qaq) <= abs(dis) && abs(qaq) <= abs(tmp)) printf("%lld\n0\n", abs(qaq));
else if(abs(dis) <= abs(tmp)) printf("%lld\n1\n%d %d", abs(dis), t1, t2);
else printf("%lld\n2\n%d %d\n%d %d", abs(tmp), a1, a2, a3, a4);
return ;
}
补题的时候,就把比赛代码三个地方的 int 改成了long long就过了
C.别人补题写的DP一眼看不懂...反正数据范围也不大,我们来xjb乱搞吧
数据范围20,时间5s,没有直接爆搜的思路
答案在0-1之间,满足单调性...试试二分?那judge呢?暴力dfs枚举?
效率玄学?并没有关系!...就当试试咯...过了...比DP还快...
#include <cmath>
#include <cstdio>
#include <algorithm> using namespace std; const double eps = 1e-; int n, L[], R[]; double a[]; bool dfs(int i, int x) {
if(i > n)
return ;
int y = L[i] / x;
if(L[i] % x) y ++;
for(y *= x;y <= R[i];y += x)
if(dfs(i + , y))
return ;
return ;
} bool judge(double k) {
for(int i = ;i <= n;i ++)
L[i] = ceil(a[i] - a[i] * k), R[i] = floor(a[i] + a[i] * k);
for(int i = L[];i <= R[];i ++)
if(dfs(, i))
return ;
return ;
} int main() {
scanf("%d", &n);
for(int i = ;i <= n;i ++)
scanf("%lf", &a[i]);
double l = , r = 1.0, mid, ans;
for(int t = ;t --;) {
mid = (l + r) / ;
if(judge(mid)) ans = mid, r = mid - eps;
else l = mid + eps;
}
printf("%.12f", ans);
return ;
}
看了别人DP代码...看懂了...
f[i][j]代表把第 i 种货币变成 j 的最小答案
我们有一种无赖解是把所有货币都变0
所以对于第 i 种货币,从 0 枚举到 a[i] * 2 就可以啦
复杂度O(nklogk), k = max(a[i]) = 20W
这么来说粗略计算我的做法复杂度就是O(nklogk * log(1/eps))...实际优太多
D.ans = C(n,3) - 不合法的三角形
对于非法三角形枚举最大边 z
再求 x + y <= z 的 (x,y) 有多少对即可
预处理,O(1)回答
#include <cstdio>
typedef long long ll;
const int maxn = ;
ll a[maxn], b[maxn];
ll c(ll x) {
return x * (x - ) * (x - ) / ;
}
int main() {
for(int i = ;i <= ;i ++) a[i] = (i + ) / - ;
for(int i = , j = ;i <= ;i ++) {
if(!(i & )) j ++;
b[i] = b[i - ] + j;
}
for(int i = ;i <= ;i ++) a[i] += a[i - ], b[i] += b[i - ];
int n;
while(scanf("%d", &n), n >= ) printf("%lld\n", c(n) - a[n] - b[n]);
return ;
}
E.
bupt summer training for 16 #3 ——构造的更多相关文章
- bupt summer training for 16 #8 ——字符串处理
https://vjudge.net/contest/175596#overview A.设第i次出现的位置左右端点分别为Li,Ri 初始化L0 = 0,则有ans = sum{ (L[i] - L[ ...
- bupt summer training for 16 #7 ——搜索与DP
https://vjudge.net/contest/174962#overview A.我们发现重点在于x,y只要累加就ok了 在每个x上只有上下两种状态,所以可以记忆化搜索 f[0/1][i]表示 ...
- bupt summer training for 16 #6 ——图论
https://vjudge.net/contest/174020 A.100条双向边,每个点最少连2个边 所以最多100个点,点的标号需要离散化 然后要求恰好经过n条路径 快速幂,乘法过程就是flo ...
- bupt summer training for 16 #5 ——数据结构
https://vjudge.net/contest/173780 A.假设 Pt = i,则由Ppi = i得 Ppt = t = Pi 所以就有 if Pt = i then Pi = t #in ...
- bupt summer training for 16 #4 ——数论
https://vjudge.net/contest/173277#overview A.平方差公式后变为 n = (x + y)(x - y) 令 t = x - y ,变成 n = (t + 2x ...
- bupt summer training for 16 #2 ——计算几何
https://vjudge.net/contest/171368#overview A.一个签到题,用叉积来判断一个点在一条线的哪个方向 可以二分,数据范围允许暴力 #include <cst ...
- bupt summer training for 16 #1 ——简单题目
D.What a Mess 给n个数,求其中能满足 a[i] % a[j] == 0 的数对之和 n = 1W,max_ai = 100W 不是很大,所以就直接筛就可以了 计算可得最高复杂度 < ...
- STL——容器(Set & multiset)编译器提供的16种构造(挖个坑)
Set & multiset 在vs2019编译器中提供了16种构造方法 1.默认的无参构造 2.比较容器内容,key_comp()函数返回一个比较key的函数. 3.使用迭代器的区间拷贝,拷 ...
- 【Android Developers Training】 16. 暂停和恢复一个Activity
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
随机推荐
- [NOIP2003普及组]麦森数(快速幂+高精度)
[NOIP2003普及组]麦森数(快速幂+高精度) Description 形如2^P-1的素数称为麦森数,这时P一定也是个素数.但反过来不一定,即如果P是个素数,2^P-1不一定也是素数.到1998 ...
- 【Codevs1288】埃及分数
Position: http://codevs.cn/problem/1288/ Description 在古埃及,人们使用单位分数的和(形如1/a的, a是自然数)表示一切有理数. 如:2/3=1/ ...
- restrict关键字
值得注意的是,一旦你决定使用restrict来修饰指针,你必须得保证它们之间不会互相重叠,编译器不会替你检查. 关键字restrict有两个读者.一个是编译器,它告诉编译器可以自由地做一些有关优化的假 ...
- P3258 [JLOI2014]松鼠的新家 树链剖分
这个题就是一道树剖板子题,就是每走一步就把所有的经过点加一就行了.还有,我的树剖板子没问题!!!谁知道为什么板子T3个点!我不管了!反正这道题正常写A了. 题干: 题目描述 松鼠的新家是一棵树,前几天 ...
- codeforces round #427 div2
A:读懂题,乘一下判断大小就行了 #include<bits/stdc++.h> using namespace std; int main() { int s, v1, v2, t1, ...
- Linux文件属性相关补充及软硬连接
第1章 文件属性相关 1.1 文件的属性 1.1.1 扩展名 windows 通过扩展名区分不同的类型的文件 linux 扩展名是给人类看的 方便我们区分不同类型文件 .conf 配置文件 ...
- ibatis 基类生成
using IBatisNet.Common.Utilities; using IBatisNet.DataMapper; using IBatisNet.DataMapper.Configurati ...
- vue学习记录(一)—— vue开发调试神器vue-devtools安装
网上有些贴子少了至关重要的一步导致我一直没装上, 切记!!install后还需build,且install和build都在vue-devtools文件夹内执行 github下载地址 点击跳转 具体步骤 ...
- CSS-类和ID选择器的区别
学习了类选择器和ID选择器,我们会发现他们之间有很多的相似处,是不是两者可以通用呢?我们不要着急先来总结一下他们的相同点和不同点: 相同点:可以应用于任何元素不同点: 1.ID选择器只能在文档中使用一 ...
- easyui textbox 内容改变事件 增加oninpu 类似事件,
//======================利用easyui验证功能,进行内容变化监控=== =============$(function () { var CustomerService = ...