ZOJ 3870 Team Formation && ZOJ 3872Beauty of Array

来源:互联网 发布:vb.net与数据库连接 编辑:程序博客网 时间:2024/06/05 14:51

ZOJ 3870 Team Formation

Description

For an upcoming programming contest, Edward, the headmaster of Marjar University, is forming a two-man team from N students of his university.

Edward knows the skill level of each student. He has found that if two students with skill level A and B form a team, the skill level of the team will be A ⊕ B, where ⊕ means bitwise exclusive or. A team will play well if and only if the skill level of the team is greater than the skill level of each team member (i.e. A ⊕ B > max{A, B}).

Edward wants to form a team that will play well in the contest. Please tell him the possible number of such teams. Two teams are considered different if there is at least one different team member.

Input

There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:

The first line contains an integer N (2 <= N <= 100000), which indicates the number of student. The next line contains N positive integers separated by spaces. The ith integer denotes the skill level of ith student. Every integer will not exceed 109.

Output

For each case, print the answer in one line.

Sample Input

2
3
1 2 3
5
1 2 3 4 5

Sample Output

1
6


如果小的那个数的二进制最高位对应的大的那一位是0的话,就可以
因为根据每一位维护前缀和

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;int t,n;int a[100005];int pre[100005][33];bool cmp(int aa,int bb){return aa > bb;}int main(){    scanf("%d",&t);    while(t--)    {        scanf("%d",&n);        for(int i=1;i<=n;i++)            scanf("%d",&a[i]);        sort(a+1,a+1+n,cmp);        for(int i=0;i<32;i++) pre[0][i] = 0;        for(int i=1;i<=n;i++)        {            for(int p=0;p<32;p++)            {                if((a[i] & (1 << p)) == 0) pre[i][p] = pre[i-1][p] + 1;                else pre[i][p] = pre[i-1][p];            }        }        long long ans = 0;        for(int i=1;i<=n;i++)        {            int highpos;            for(int p=0;p<32;p++)            if(a[i] & (1 << p)) highpos = p;            ans += pre[i-1][highpos];        }        printf("%lld\n",ans);    }

ZOJ 3872 Beauty of Array

Description

Edward has an array A with N integers. He defines the beauty of an array as the summation of all distinct integers in the array. Now Edward wants to know the summation of the beauty of all contiguous subarray of the array A.

Input

There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:

The first line contains an integer N (1 <= N <= 100000), which indicates the size of the array. The next line contains N positive integers separated by spaces. Every integer is no larger than 1000000.

Output

For each case, print the answer in one line.

Sample Input

3
5
1 2 3 4 5
3
2 3 3
4
2 3 3 2

Sample Output

105
21
38


算每一个值出现在多少个区间内
记录一个值的前面一个相同的值所在的位置,然后从pre的前面到数组头取一段,然后从pre+1的后面取到当前位置,做乘积来计算有多少个区间包含这个数。

//By LH#pragma warning(disable:4996)#include <algorithm>#include <iostream>#include <iomanip>#include <cstring>#include <climits>#include <complex>#include <fstream>#include <cassert>#include <cstdio>#include <bitset>#include <vector>#include <deque>#include <queue>#include <stack>#include <ctime>#include <set>#include <map>#include <cmath>#include <functional>#include <numeric>#pragma comment(linker, "/STACK:1024000000,1024000000")using namespace std;typedef long long LL;int n, T;LL ans;int pre[1000005];int main(){    scanf("%d", &T);    while (T--)    {        scanf("%d", &n);        int maxx = 0;        ans = 0;        memset(pre, 0, sizeof(pre));        for (int i = 1; i <= n; i++)        {            int x;            scanf("%d", &x);            maxx = max(x, maxx);            LL tmp = LL(pre[x]) * LL(i - pre[x]) * LL(x);            ans += tmp;            pre[x] = i;        }        for (int i = 1; i <= maxx; i++)        if (pre[i] != 0)        {            LL tmp = LL(pre[i]) * LL(n - pre[i]+1) * LL(i);            ans += tmp;        }        printf("%lld\n", ans);    }    return 0;}
0 0
原创粉丝点击