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 ...
随机推荐
- MSP430:定时器学习TimerA
4. 定时器TA 一.时钟源1.时钟源:ACLK/SMCLK 外部TACLK/INCLK2.分频:1/2/4/8 当 (注:TACLR 置位时,分频器复位) 二.计数模式通过设置MCx可以设置定时器的 ...
- PCB MS SQL SERVER 字段含小写字母更新为大写字母
今天在预审完成时报如下错误,此错误原因是由于SQL Server数据字段存在小写,而Oracle数据库需大写导致的, 怎么解决这个问题了,非常简单 .这里将SQL贴出来 . 1.将生产型号中含有小写字 ...
- E20171016-mk
chaos n. 混乱,紊乱; (天地未出现的) 浑沌世界; 〈古〉无底深渊; 一团糟;
- bzoj1036 树的统计(树链剖分+线段树)
1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 15120 Solved: 6141[Submit ...
- IDEA新项目代码上传到gitlab远程仓库
IDEA新项目代码上传到gitlab远程仓库 具体步骤 创建本地仓库 IDEA:VCS-->Import into Version Control-->Create Git Reposit ...
- 解决Logger在Android Studio 3.1版本无法正常加载tag格式
已经升级到Android Studio 3.1的同学可能会发现一个问题, Logcat中如果短时间出现多条日志tag相同, 只会显示第一条日志的tag, 后面的tag会自动隐藏, 这时com.orha ...
- python django简单操作
准备: pip3 install django==1.10.3 cmd django-admin startproject guest 创建一个guest的项目 cd guest manage. ...
- 题解报告:hdu 2647 Reward(拓扑排序)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 Problem Description Dandelion's uncle is a boss ...
- 10 在C#中读取文件
我们在前一个练习中已经了解了如何在C#控制台程序(console)中读取用户的输入.现在我们要学习如何从一个文件中读取内容.在下面的练习中,你要格外小心.关于文件的操作,一不小心会损失你的重要文件. ...
- [Android]异常7-Error:Configuration with name 'default' not found.
背景:使用SVN更新代码,运行出现 异常原因: 可能一>缺少Modules 解决办法有: 解决一>Android Studio切换为Project,settings.gradle中引用和现 ...