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 ...
随机推荐
- cookie domain and cookie path
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie Domain=<domain-value> Opt ...
- E20170626-hm
authenticate vt. 认证,证明是真实的.可靠的或有效的; 鉴定,使生效; author n. 作者; 著作家; 创造者; 发起人;
- PID204特种部队
特种部队 题目描述 Description 某特种部队接到一个任务,需要潜入一个仓库.该部队士兵分为两路,第一路士兵已经在正面牵制住了敌人,第二路士兵正在悄悄地从后方秘密潜入敌人的仓库.当他们到达 ...
- [Swift通天遁地]三、手势与图表-(2)监听手势事件自由拖动图像视图
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- [Swift通天遁地]七、数据与安全-(13)单元测试的各个状态和应用
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- Unity - 简单实例化的应用
项目描述:每帧实例化一个随机颜色的物体(Cube),坐标在某范围内随机:且物体每帧都会缩小,当缩小到一定的尺寸时,就销毁物体 代码描述: public class CubeSpawner : Mono ...
- Kafka详解与总结(三)
Kafka分片存储机制 几个kafka重要概念: Broker:消息中间件处理结点,一个Kafka节点就是一个broker,多个broker可以组成一个Kafka集群. Topic:一类消息,例如pa ...
- Prime Ring Problem -- 第一个真正意义上的 搜索
这个搜索............搜的我头都大了.......不过还是 懂了那么一点点...哈哈 从3/7晚上 做到3/8晚上------从女生到妇女 我都用来做着一道题了......... 所谓的 ...
- 322 Coin Change 零钱兑换
给定不同面额的硬币(coins)和一个总金额(amount).写一个函数来计算可以凑成总金额所需的最少的硬币个数.如果没有任何一种硬币组合方式能组成总金额,返回-1.示例 1:coins = [1, ...
- 【转】Linux 之 数据流重定向
转自:http://www.linuxidc.com/Linux/2012-09/69764.htm linux在你登入时,便将默认的标准输入.标准输出.标准错误输出安排成你的终端.I/O重定向就是你 ...