zoukankan      html  css  js  c++  java
  • HDU.6186.CSCource.(前缀和数组和后缀和数组)

      明天后天是南昌赛了嘤嘤嘤,这几天就先不更新每日题目了,以后补题嘤嘤嘤。

    今天和队友做了一套2017年广西邀请赛,5个题还是有点膨胀......

      好了,先来说一下有意思的题目吧......

    CS Course

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 3450    Accepted Submission(s): 1287


    Problem Description
    Little A has come to college and majored in Computer and Science.

    Today he has learned bit-operations in Algorithm Lessons, and he got a problem as homework.

    Here is the problem:

    You are giving n non-negative integers a1,a2,,an, and some queries.

    A query only contains a positive integer p, which means you 
    are asked to answer the result of bit-operations (and, or, xor) of all the integers except ap.
     
    Input
    There are no more than 15 test cases. 

    Each test case begins with two positive integers n and p
    in a line, indicate the number of positive integers and the number of queries.

    2n,q105

    Then n non-negative integers a1,a2,,an follows in a line, 0ai109 for each i in range[1,n].

    After that there are q positive integers p1,p2,,pqin q lines, 1pin for each i in range[1,q].
     
    Output
    For each query p, output three non-negative integers indicates the result of bit-operations(and, or, xor) of all non-negative integers except ap in a line.
     
    Sample Input
    3 3 1 1 1 1 2 3
     
    Sample Output
    1 1 0 1 1 0 1 1 0
     
    Source
     
    Recommend
    liuyiding   |   We have carefully selected several similar problems for you:  6543 6542 6541 6540 6539 
     
    本题思路:我一开始上来就一发暴力,结果超时了,后来和队友讨论了一下,得出 ^ 可以逆着运算,也就是如果x ^ y = z,那么y 就可以用 z ^ x 得到,&和 | 我们打算将所有位置出现0和1的次数统计和,然后删除数字的时候删除对应数字上的0和1的个数,判断&和|即可。后来没有实现这个,而是用后缀数组和前缀数组实现,接着进行相应的位运算就行了,第一次做到这类型的思维题,所以今天先写一发博客记录。
    参考代码:
    #include <cstdio>
    using namespace std;
    
    const int maxn = 100000 + 5;
    int c[maxn], _and1[maxn], _and2[maxn], _or1[maxn], _or2[maxn], _xor1[maxn], _xor2[maxn];
    
    int main() {
        int n, p, q, ans1, ans2, ans3;
        while(~scanf("%d %d", &n, &p)) {
            for(int i = 1; i <=n; i ++) {
                scanf("%d", &c[i]);
            }
            _and1[1] = _or1[1] = _xor1[1] = c[1];
            _and2[n] = _or2[n] = _xor2[n] = c[n];
            for(int i = 2; i <= n; i ++) {
                _and1[i] = _and1[i - 1] & c[i];
                _or1[i] = _or1[i - 1] | c[i];
                _xor1[i] = _xor1[i - 1] ^ c[i];
            }
            for(int i = n - 1; i >= 1; i --) {
                _and2[i] = _and2[i + 1] & c[i];
                _or2[i] = _or2[i + 1] | c[i];
                _xor2[i] = _xor2[i + 1] ^ c[i];
            }
            
            for(int i = 0; i < p; i ++) {
                scanf("%d", &q);
                if(q == 1) {
                    ans1 = _and2[2];
                    ans2 = _or2[2];
                    ans3 = _xor2[2];            
                } else if(q == n) {
                    ans1 = _and1[n - 1];
                    ans2 = _or1[n - 1];
                    ans3 = _xor2[n - 1];
                } else {
                    ans1 = _and1[q - 1] & _and2[q + 1];
                    ans2 = _or1[q - 1] | _or2[q + 1];
                    ans3 = _xor1[q - 1] ^ _xor2[q + 1];
                }
                printf("%d %d %d
    ", ans1, ans2, ans3);
            }
        }
        return 0;
    }
  • 相关阅读:
    SpringMVC后台token防重复提交解决方案
    浅析微信支付:开通免充值产品功能及如何进行接口升级指引
    浅析微信支付:查询订单和关闭订单
    浅析微信支付:商户平台代金券或立减优惠开通、指定用户代金券发放、查询等
    linux内核源码分析
    linux内存源码分析
    linux内存源码分析
    linux内存源码分析
    linux内存源码分析
    linux内存源码分析
  • 原文地址:https://www.cnblogs.com/bianjunting/p/10958255.html
Copyright © 2011-2022 走看看