CodeForces 702B Powers of Two (暴力,优化)

题意:给定 n 个数,问你从有多少下标 i < j,并且 ai + aj 是2的倍数。

析:方法一:

从输入开始暴力,因为 i < j 和 i > j 是一样,所以可以从前面就开始查找,然后计数,用个map就搞定,不过时间有点长,接近两秒。

方法二:

先排序,然后暴力,暴力的原则是找前面的,也是用map查找,时间62ms。

代码如下:

#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
using namespace std ;
typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f3f;
const double eps = 1e-8;
const int maxn = 1e5 + 5;
const int dr[] = {0, 0, -1, 1};
const int dc[] = {-1, 1, 0, 0};
int n, m;
inline bool is_in(int r, int c){
    return r >= 0 && r < n && c >= 0 && c < m;
}
map<int, int> mp;

int main(){
    cin >> n;
    int x;
    LL ans = 0;
    for(int i = 0; i < n; ++i){
        scanf("%d", &x);
        for(int j = 1; j < 31; ++j){
            ans += mp[(1<<j)-x];
        }
        ++mp[x];
    }
    cout << ans << endl;
    return 0;
}
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
using namespace std ;
typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f3f;
const double eps = 1e-8;
const int maxn = 1e5 + 5;
const int dr[] = {0, 0, -1, 1};
const int dc[] = {-1, 1, 0, 0};
int n, m;
inline bool is_in(int r, int c){
    return r >= 0 && r < n && c >= 0 && c < m;
}
map<int, int> mp;
int a[maxn];

int main(){
    cin >> n;
    int x;
    LL ans = 0;
    for(int i = 0; i < n; ++i){
        scanf("%d", &a[i]);
    }

    sort(a, a+n);
    for(int i = 0; i < n; ++i){
        for(LL j = 1; j <= 2 * a[i]; j *= 2){//注意是long long
            if(j <= a[i])  continue;
            ans += mp[j-a[i]];
        }
        ++mp[a[i]];
    }

    cout << ans << endl;
    return 0;
}
时间: 2024-10-13 19:00:34

CodeForces 702B Powers of Two (暴力,优化)的相关文章

Codeforces 702B Powers of Two

题目链接:http://codeforces.com/problemset/problem/702/B 题意: 给你N个数a0, a1, a2......an-1,问存在几对 <i,j> 满足i < j, ai + aj = 2x ,x可以是任意整数. 思路: 最容易想到的是把这 n  个数全部两两加起来,然后判断是否是 2 的幂,但是这需要O(n2),题目给的 n <= 1e5,所以会TLE.再思考思考ai + aj = 2x ,那么变下形: aj = 2x - ai ,这样只需

Codeforces 443A Borya and Hanabi(暴力)

题目链接:Codeforces 443A Borya and Hanabi 题目大意:有若干个牌,每张牌有花色和数字两个值,现在问说至少询问多少次才能区分出所有的牌,每次询问可以确定一种花色牌的位置,或者是一种数字牌的位置. 解题思路:暴力枚举需要问的花色和数字,210,然后枚举两两判断是否可以被区分. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const

Codeforces 850A - Five Dimensional Points(暴力)

原题链接:http://codeforces.com/problemset/problem/850/A 题意:有n个五维空间内的点,如果其中三个点A,B,C,向量AB,AC的夹角不大于90°,则点A是"bad"的否则是"good".题目让我们输出good的点. 思路:从2,3维空间超过5,7个点时不存在"good"的点,可以简单推知五维空间内,超过11个点时不存在"good"的点,那么点数小于11时暴力,大于11时输出0. 其

Codeforces Gym 100203G Good elements 暴力乱搞

原题链接:http://codeforces.com/gym/100203/attachments/download/1702/statements.pdf 题解 考虑暴力的复杂度是O(n^3),所以我们需要记录所有的ai+aj,如果当前考虑到了ak,那么就去前面寻找ai,使得ak-ai是我们记录过的和.整个算法的复杂度O(n^2). 代码 #include<iostream> #include<cstring> #include<cstdio> #include<

Codeforces Gym 100637G G. #TheDress 暴力

G. #TheDress Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100637/problem/G Description After landing on planet i1c5l people noticed that blue and black clothes are quite popular among the locals. Each aboriginal has at least

Codeforces gym 100685 A. Ariel 暴力

A. ArielTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100685/problem/A Description King Triton really likes watching sport competitions on TV. But much more Triton likes watching live competitions. So Triton decides to set up

Codeforces 839D Winter is here - 暴力 - 容斥原理

Winter is here at the North and the White Walkers are close. John Snow has an army consisting of n soldiers. While the rest of the world is fighting for the Iron Throne, he is going to get ready for the attack of the White Walkers. He has created a m

[TJOI2017]城市 【树的直径+暴力+优化】

Online Judge:Luogu P3761 Label:树的直径,暴力 题目描述 从加里敦大学城市规划专业毕业的小明来到了一个地区城市规划局工作.这个地区一共有n座城市,n-1条高速公路,保证了任意两运城市之间都可以通过高速公路相互可达,但是通过一条高速公路需要收取一定的交通费用.小明对这个地区深入研究后,觉得这个地区的交通费用太贵. 小明想彻底改造这个地区,但是由于上司给他的资源有限,因而小明现在只能对一条高速公路进行改造,改造的方式就是去掉一条高速公路,并且重新修建一条一样的高速公路(

HDU - 1248 寒冰王座(完全背包做法和暴力优化做法)

题意:完全背包裸题 每件物品的cost就是它的value. 1.完全背包法 1 #include <iostream> 2 using namespace std; 3 4 const int maxn=11111; 5 int dp[maxn]; 6 7 int main(){ 8 int t; 9 cin>>t; 10 int value[4]={0,150,200,350}; 11 while(t--){ 12 int n; 13 cin>>n; 14 for(i