SCU-4437 Carries
https://blog.csdn.net/catglory/article/details/46539283
https://blog.csdn.net/u012127882/article/details/46517099
学到了很开心
当我们求,有多少个数相加起来会大于某一个常数的时候,只需将原数组排序,然后枚举每一个数就行了,枚举的时候,一个左指针向右移,就是我们正在枚举的数字,而另外一个右指针向左移,而不需要回溯,因为左指针指向的数字变大了,所以右指针右边的不用判断就知道一定会大于这个常数了。这样的话就可以将n方的复杂度变成2*n,所谓的二分解法,与这个也是类似的。二分求解的是,每一个左指针指向的值,与之相加不能超过这个常数的值的位置,实际上就是每次左指针右移时,右指针停住的位置。
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#define fuck(x) cout<<#x<<" = "<<x<<endl;
#define ls (t<<1)
#define rs ((t<<1)+1)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = ;
const int inf = 2.1e9;
const ll Inf = ;
const int mod = ;
const double eps = 1e-;
const double pi = acos(-); int num[maxn];
int a[maxn];
int main()
{
// ios::sync_with_stdio(false);
// freopen("in.txt","r",stdin); int n;
while(scanf("%d",&n)!=EOF){
for(int i=;i<=n;i++){
scanf("%d",&num[i]);
}
int t= ;
ll ans = ;
for(int k=;k<=;k++){
t*=;
for(int i=;i<=n;i++){
a[i]=num[i]%t;
}
sort(a+,a++n); int r = n;
for(int i=;i<=n;i++){
while(r&&a[i]+a[r]>=t){
r--;
}
// fuck(r)
if(i>r){ans+=(n-r-);}
else{ans+=n-r;}
// fuck(ans)
} }printf("%lld\n",ans/); } return ;
}
代码几乎是抄的上面两位博主的,毕竟看了别人的代码,思路就固化了。开始不知道输出是答案的两倍,除了一个2就过了。
后来想了一下,应该是重复枚举了,毕竟l最后会超过r,所以要除以2
SCU-4437 Carries的更多相关文章
- ACM:SCU 4437 Carries - 水题
SCU 4437 Carries Time Limit:0MS Memory Limit:0KB 64bit IO Format:%lld & %llu Practice ...
- SCU 4437 Carries(二分乱搞)题解
题意:问任意两对ai,aj相加的总进位数为多少.比如5,6,95分为(5,6)(5,95)(6,95),进位数 = 1 + 2 + 2 = 5 思路:显然暴力是会超时的.我们可以知道总进位数等于每一位 ...
- Carries SCU - 4437
Carries frog has nn integers a1,a2,-,ana1,a2,-,an, and she wants to add them pairwise. Unfortunately ...
- ACM: SCU 4438 Censor - KMP
SCU 4438 Censor Time Limit:0MS Memory Limit:0KB 64bit IO Format:%lld & %llu Practice D ...
- ACM: SCU 4440 Rectangle - 暴力
SCU 4440 Rectangle Time Limit:0MS Memory Limit:0KB 64bit IO Format:%lld & %llu Practic ...
- 2015弱校联盟(1) - B. Carries
B. Carries Time Limit: 1000ms Memory Limit: 65536KB frog has n integers a1,a2,-,an, and she wants to ...
- SCU 4424(求子集排列数)
A - A Time Limit:0MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Status Practice ...
- SCU 2941 I NEED A OFFER!(01背包变形)
I NEED A OFFER! 64bit IO Format: %lld & %llu Submit Status Description Description Speakless ...
- SCU 4440 分类: ACM 2015-06-20 23:58 16人阅读 评论(0) 收藏
SCU - 4440 Rectangle Time Limit: Unknown Memory Limit: Unknown 64bit IO Format: %lld & %llu ...
- scu 4436: Easy Math 水题
4436: Easy Math Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.scu.edu.cn/soj/problem.actio ...
随机推荐
- 工程师常犯的web后台界面用户体验错误
- Linux中,去掉终端显示的当前目录的绝对路径
Linux中,去掉终端显示的当前目录的绝对路径 去~/.bashrc中,找到PS1变量的定义,如果没有,手动加上: 可以将显示输出到标题栏上: #export PS1="[e]2;u@H w ...
- c++数字和字符串的转换
1 利用stringstream 添加头文件 #include<sstream> 数字转字符串 #include <string> #include <sstrea ...
- CSS3 flexbox 布局 ---- flex项目属性介绍
现在介绍用在flex项目上的css 属性,html结构还是用ul, li 结构,不过内容改成1,2,3, 样式的话,直接把给 ul 设display:flex 变成flex 容器,默认主轴的方向为水平 ...
- ScheduledThreadPoolExecutor 吞异常
线程挂掉不一定会输出日志到控制台,比如ScheduledThreadPoolExecutor,如果在执行的任务中有未捕获的异常抛出,就行停止调度,没有任何错误输出到控制台或日志文件.在项目中这会导致一 ...
- 性能测试工具 Locust
https://docs.locust.io/en/latest/quickstart.html
- 洛谷P2085最小函数值题解
题目 首先我们先分析一下题目范围,\(a,b,c\) 都是整数,因此我们可以得出它的函数值在\((0,+\infty )\)上是单调递增的,,然后我们可以根据函数的性质,将每个函数设置一个当前指向位置 ...
- Game HDU - 3657(最小割)
Game Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- spfa优化板子
用双端队列,当待加入元素小于队首元素时 加入队首, 否则 加入队尾 slf优化 if(!vis[e.v]) { if(Q.empty()) Q.push_front(e.v); else { if(d ...
- pip详解
pip是一个安装和管理 Python 包的工具.python安装包的工具有easy_install, setuptools, pip,distribute等,pip是Python官方推荐的包管理工具 ...