Codeforces Round #561 (Div. 2)
题意:
给出 n 个数,问有多少点对(x,y)满足 |x-y| ≤ |x|,|y| ≤ |x+y|;
(x,y) 和 (y,x) 表示一种答案;
题解:
数形结合;
对于某数 x 查找满足条件 y 有多少个;
①x ≥ 0
y ∈ [x/2 , 2x] ∪ [ -2x , -x/2];
②x < 0
y ∈ [2x , -x/2] ∪ [-x/2 , -2x];
特别注意临界值 x/2 处;
AC代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=2e5+; int n;
int x[maxn]; int F(int a,int b)///查找数组x中有多少数位于[a,b]之间
{
if(a > b)
swap(a,b);
int t1=lower_bound(x+,x+n+,a)-x;
int t2=upper_bound(x+,x+n+,b)-x;
return t2-t1;
}
ll Solve()
{
sort(x+,x+n+); ll ans=;
for(int i=;i <= n;++i)
{
int a,b;
if(x[i] >= )
a=(x[i]+)/;
else
a=(x[i]-)/;///不能写成(x[i]-1)>>1;
b=x[i]*;///不能写成b=x[i]<<1;
ans += F(a,b);
ans += F(-a,-b);
ans--;///减掉x[i]本身
}
return ans>>;///重复计算,除以2
}
int main()
{
scanf("%d",&n);
for(int i=;i <= n;++i)
scanf("%d",x+i);
printf("%lld\n",Solve()); return ;
}
坑:
负数使用右移符号出错;
-2×2 ≠ -2<<1;
左移,右移只可以用到正整数上;
负数禁用位移运算;
题意:
定义“Cute Sequences”,如果序列x满足 ∀i∈[2 , n] xi=xi−1+xi−2+⋯+x1+ri (1 ≤ ri ≤ m),那么次序列为"Cute Sequences";
现给出你 a,b,m ,让你构造一个序列,满足:
①x[1] = a , x[n] = b;
②n ≤ 50;
③此序列为"Cute Sequences";
题解:
定义数组val[]存储最终的值;
看下面这段代码:
ll sum=a;
ll k=;
val[++k]=a;
while(k <= )
{
if(val[k] >= b)
break; val[++k]=sum+m;
sum += val[k];
}
因为题目要求 k ≤ 50,所以,要先让其每次加最大的值 m ,找到使得 val[k] ≥ b 的最小的 k ;
那么多的部分 val[k]-b 该怎么办呢?

刚开始,每个数都会增加 m ,如果第 i 个数少加 x,有什么变化呢?
先通过上述小范围的数据看一下规律;
你会发现,如果第 i 个数少加 x,那么,对于第 j (j > i) 个数,就会少加2j-i-1x;
这样的话,就可以通过前面的数少加 x 使得第 k 个数 val[k] 变为 b;
不过在此之前你得确定 val[k] 可以减少为 b;
bool isSat(int k)
{
memcpy(tmp+,val+,k*sizeof(ll));
for(int i=;i <= k;++i)
{
ll x=m-;
tmp[i] -= x;
for(int j=i+;j <= k;++j)///i之后的数依次减少2^(j-i-1)*x,累加x即可
tmp[j] -= x,x += x;
if(tmp[k] <= b)
return true;
}
return tmp[k] <= b ? true:false;
}
AC代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mem(a,b) memset(a,b,sizeof(a))
const int maxn=2e5+; int q;
ll a,b,m;
ll val[];
ll tmp[]; bool isSat(int k)
{
if(k == )
return true; memcpy(tmp+,val+,k*sizeof(ll));
for(int i=;i <= k;++i)
{
ll x=m-;
tmp[i] -= x;
for(int j=i+;j <= k;++j)
tmp[j] -= x,x += x;
if(tmp[k] <= b)
return true;
}
return false;
}
void Solve()
{
ll sum=a;
ll k=;
val[++k]=a;
while(k <= )
{
if(val[k] >= b)
break; val[++k]=sum+m;
sum += val[k];
} if(k > || !isSat(k))
{
printf("-1\n");
return ;
} printf("%lld %lld",k,a);
for(int i=;i < k;++i)
{
ll d=val[k]-b;
ll x=min(m-,d/(1ll*<<(k-i-))); val[i] -= x;
for(int j=i+;j <= k;++j)
val[j] -= x,x += x; printf(" %lld",val[i]);
}
if(k > )
printf(" %lld\n",b);
else
printf("\n");
}
int main()
{
// freopen("C:/Users/14685/Desktop/stdin&&stdout/contest","r",stdin);
scanf("%d",&q);
while(q--)
{
scanf("%lld%lld%lld",&a,&b,&m);
Solve();
}
return ;
}
bug:
61~64最后输出语句,晚上重新敲的时候将其放到了for()里:
printf("%d %lld",k,a);
for(int i=;i <= k;++i)
{
if(i == k)
{
printf(" %lld\n",b);
return ;
}
ll d=val[k]-b;
ll x=min(m-,d/(1ll*<<(k-i-)));
val[i] -= x;
for(int j=i+;j <= k;++j)
val[j] -= x,x += x;
printf(" %lld",val[i]);
}
一直是“Wrong answer on test 10”,debug了好一会,才发现,当 k = 1是,最后是有换行的;
但是,放到for()里就缺少一个换行;
Codeforces Round #561 (Div. 2)的更多相关文章
- Codeforces Round #561 (Div. 2) C. A Tale of Two Lands
链接:https://codeforces.com/contest/1166/problem/C 题意: The legend of the foundation of Vectorland talk ...
- Codeforces Round #561 (Div. 2) B. All the Vowels Please
链接:https://codeforces.com/contest/1166/problem/B 题意: Tom loves vowels, and he likes long words with ...
- Codeforces Round #561 (Div. 2) A. Silent Classroom
链接:https://codeforces.com/contest/1166/problem/A 题意: There are nn students in the first grade of Nlo ...
- Codeforces Round 561(Div 2)题解
这是一场失败的比赛. 前三题应该是随便搞的. D有点想法,一直死磕D,一直WA.(赛后发现少减了个1……) 看E那么多人过了,猜了个结论交了真过了. 感觉这次升的不光彩……还是等GR3掉了洗掉这次把, ...
- Codeforces Round #561 (Div. 2) E. The LCMs Must be Large(数学)
传送门 题意: 有 n 个商店,第 i 个商店出售正整数 ai: Dora 买了 m 天的东西,第 i 天去了 si 个不同的个商店购买了 si 个数: Dora 的对手 Swiper 在第 i 天去 ...
- Codeforces Round #561 (Div. 2) A. Silent Classroom(贪心)
A. Silent Classroom time limit per test1 second memory limit per test256 megabytes inputstandard inp ...
- Codeforces Round #561 (Div. 2) A Tale of Two Lands 【二分】
A Tale of Two Lands 题目链接(点击) The legend of the foundation of Vectorland talks of two integers xx and ...
- Codeforces Round #366 (Div. 2) ABC
Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
随机推荐
- Spring → 04:Bean(1)
一.Bean概念 Spring Bean是被实例的,组装的及被Spring 容器管理的Java对象. Spring 容器会自动完成@bean对象的实例化. 创建应用对象之间的协作关系的行为称为:装配( ...
- Java大数类BigDecimal及八种舍入模式的介绍
BigDecimal的引入 在利用Java编程语言开发银行.金融类等需要对数值进行高精度计算的软件时,我们经常使用BigDecimal和BigInteger这两个大数据类,而不是常见的int.long ...
- iOS 9适配
iOS 9系统策略更新,请开发者注意升级 近期苹果公司iOS 9系统策略更新,限制了http协议的访问,此外应用需要在“Info.plist”中将要使用的URL Schemes列为白名单,才可正常检查 ...
- 友盟iOS sdk整理
文档中心 :http://dev.umeng.com 集成文档:http://dev.umeng.com/analytics/ios-doc/integration 报表中心:http://www.u ...
- BZOJ3832Rally题解
一道思维神题.... 我们像网络流一样加入原点S,与汇点T 用f[i]表示原点到i的最长路,用g[i]表示i到汇点的最长路 f数组与g数组都可以dp求出来的 接下来考虑如何通过这些信息来维护删除某个点 ...
- 洛谷 P1505 [国家集训队]旅游 树链剖分
目录 题面 题目链接 题目描述 输入输出格式 输入格式 输出格式 输入输出样例 输入样例: 输出样例: 说明 思路 AC代码 总结 题面 题目链接 P1505 [国家集训队]旅游 题目描述 Ray 乐 ...
- Pycurl介绍
pycurl — A Python interface to the cURL library Pycurl包是一个libcurl的Python接口.pycurl已经成功的在Python2.2到Pyt ...
- 【JZOJ4831】【NOIP2016提高A组集训第3场10.31】方程式
题目描述 数据范围 解法 枚举根之后,使用大除法. 代码 #include<stdio.h> #include<iostream> #include<string.h&g ...
- 【uml】之类图中的关系 标签: uml图形类 2014-11-29 09:02 1198人阅读 评论(23)
uml早就画完了图,但是自己迟迟没有总结,因为总觉的自己把握的不到位,虽然现在也还是不到位,废话少说,上篇博客总结了用例图中的几种关系,这篇就讨论一下类图中的几种关系. 在uml的所有图中,就我目前的 ...
- MaxCompute如何对SQL查询结果实现分页获取
由于MaxCompute SQL本身不提供类似数据库的select * from table limit x offset y的分页查询逻辑.但是有很多用户希望在一定场景下能够使用获取类似数据库分页的 ...