LN : Eden Bitset_3
- Appreciation to our TA, 王毅峰, who designed this task.
问题描述
Give you N numbers a[1]...a[n]
and M numbers b[1]...b[m]
For each b[k], if we can find i,j a[i] + a[j] = b[k] or a[i] = b[k] , we say k is a good number.
And you should only output the number of good numbers.
0 < n, m, a[i], b[j] <= 200000
sample input
3 6
1
3
5
2
4
5
7
8
9
sample output
4
b[1]...b[m] 2,4,5,7,8,9
2 = 1+1
4 = 1+3
5 = 5
8 = 3+5
问题解析
TA的本意是想让我们运用bitset的方法,然而我不太懂,所以投机取巧用了类似于桶排序的方式,之后我会再去研究一下TA的解法的。
My answer
#include <iostream>
using namespace std;
int main() {
    int tong1[200000] = {0};
    int tong2[200000] = {0};
    int n, m, temp, sum = 0;
    cin >> n >> m;
    while (n--) {
        cin >> temp;
        tong1[temp]++;
    }
    while (m--) {
        cin >> temp;
        tong2[temp]++;
    }
    for (int i = 1; i < 200000; i++) {
        int pan = 0;
        if (tong2[i] != 0) {
            if (tong1[i] != 0) {
                pan = 1;
            } else {
                for (int j = 1; j < i; j++) {
                    if (tong1[j] != 0 && tong1[i-j] != 0) {
                        pan = 1;
                        break;
                    }
                }
            }
            if (pan == 1)
            sum += tong2[i];
        }
    }
    cout << sum << endl;
    return 0;
}
TA's answer
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <bitset>
using namespace std;
const int maxn = 50001;
bitset<maxn> goal, now, tmp;
int a[maxn], n, m;
void work() {
    scanf("%d", &m);
    goal.reset();
    now.reset();
    for (int i = 1; i <= n; ++i) {
        scanf("%d", &a[i]);
        now.set(a[i]);
    }
    // scanf("%d", &m);
    for (int i = 1; i <= m; ++i) {
        int k;
        scanf("%d", &k);
        goal.set(k);
    }
    sort(a + 1, a + n + 1);
    tmp = now;
    for (int i = 1; i <= n; ++i) {
        tmp = tmp << (a[i] - a[i - 1]);
        now = now | tmp;
    }
    goal = goal & now;
    printf("%d\n", goal.count());
}
int main() {
    while (scanf("%d", &n) != EOF) work();
}
 
                    
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号