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的更多相关文章

  1. ACM:SCU 4437 Carries - 水题

    SCU 4437  Carries Time Limit:0MS     Memory Limit:0KB     64bit IO Format:%lld & %llu  Practice  ...

  2. SCU 4437 Carries(二分乱搞)题解

    题意:问任意两对ai,aj相加的总进位数为多少.比如5,6,95分为(5,6)(5,95)(6,95),进位数 = 1 + 2 + 2 = 5 思路:显然暴力是会超时的.我们可以知道总进位数等于每一位 ...

  3. Carries SCU - 4437

    Carries frog has nn integers a1,a2,-,ana1,a2,-,an, and she wants to add them pairwise. Unfortunately ...

  4. ACM: SCU 4438 Censor - KMP

     SCU 4438 Censor Time Limit:0MS     Memory Limit:0KB     64bit IO Format:%lld & %llu  Practice D ...

  5. ACM: SCU 4440 Rectangle - 暴力

     SCU 4440 Rectangle Time Limit:0MS     Memory Limit:0KB     64bit IO Format:%lld & %llu  Practic ...

  6. 2015弱校联盟(1) - B. Carries

    B. Carries Time Limit: 1000ms Memory Limit: 65536KB frog has n integers a1,a2,-,an, and she wants to ...

  7. SCU 4424(求子集排列数)

    A - A Time Limit:0MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice ...

  8. SCU 2941 I NEED A OFFER!(01背包变形)

    I NEED A OFFER!     64bit IO Format: %lld & %llu Submit Status Description Description Speakless ...

  9. SCU 4440 分类: ACM 2015-06-20 23:58 16人阅读 评论(0) 收藏

    SCU - 4440 Rectangle Time Limit: Unknown   Memory Limit: Unknown   64bit IO Format: %lld & %llu ...

  10. scu 4436: Easy Math 水题

    4436: Easy Math Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.scu.edu.cn/soj/problem.actio ...

随机推荐

  1. 关于PHP函数传参的注意点

    PHP的实参在传递过程中是顺序传递的,不支持指定参数名传递.怎么理解呢?看下面的代码: function test($name,$age){ echo '姓名:'.$name,' 年纪:'.$age; ...

  2. redis.clients.jedis.exceptions.JedisDataException :READONLY You can't write

    分布式直连同步调用测试时出现的错误:主从复制架构下,默认Slave是只读的,如果写入则会报错: redis.clients.jedis.exceptions.JedisDataException: R ...

  3. Maven最佳实战

    Maven中内置的隐藏变量: http://www.cnblogs.com/quanyongan/category/471332.html Maven提供了三个隐式的变量可以用来访问环境变量,POM信 ...

  4. Java反射和注解

    反射:http://blog.csdn.net/liujiahan629629/article/details/18013523 注解:http://www.cnblogs.com/peida/arc ...

  5. 2.docker的网络模式

    本篇文章使用nginx:apline  镜像进行编辑. docker 版本基于 [root@master song]# docker version Client: Version: API vers ...

  6. Atcoder Beginner Contest 121 D - XOR World(区间异或和)

    题目链接:https://atcoder.jp/contests/abc121/tasks/abc121_d 题目很裸(Atcoder好像都比较裸 就给一个区间求异或和 n到1e12 肯定不能O(n) ...

  7. 【嵌入式】Arduino编程基础到应用全解析

    Arduino Author: Andrew.Du 基础 基础语法: setup() loop() pinMode(引脚,模式) pinMode(13,OUTPUT):设置13号引脚为输出 //在使用 ...

  8. luogu P1816 【忠诚】

    话说许多dalao都采取线段树A题可本蒟蒻不会啊, 暴力的我想出了暴力解法(快排) #include<cstdio> #include<algorithm> using nam ...

  9. python查找读写文件

    import os ''' 跟据文件名称,后缀查找指定文件 path:传入的路径 filename:要查找的文件名 suffix:要查找的文件后缀 return :返回查找的文件路径 ''' file ...

  10. MySQL 报错 1055

    具体报错 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'exer.student.sid' w ...