ZOJ problem Set-3870 "Find the Law"

Source: Internet
Author: User
Tags time limit cmath

ZOJ problem Set-3870 Team formation time limit:3 Seconds Memory limit:131072 KB

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

Edward knows the skill level of each student. He has found that if students with skill levela and B form a team, the skill level of the team would be a⊕b, where⊕ means bitwise exclusive OR. A team would play well if and only if the skill level of the team are greater than the skill level of each team member (i.e. A⊕b > Max{a, B}).

Edward wants to form a team that'll play well in the contest. Please tell him the possible number of such teams. Teams is considered different if there was at least one different team member. Input

There is multiple test cases. The first line of input contains an integer T indicating the number of the 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 containsn positive integers separated by spaces. The ith integer denotes the skill level ofith student. Every integer would not exceed 109. Output

For each case, print the answer on one line. Sample Input

2
3
1 2 3
5
1 2 3 4 5
Sample Output
1
6

The pattern to find the law can: the law: As long as a person can make a m different team with m individuals then the same person with the value of the bits number of people can also be with the same m personal team

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn=100010;
struct node{
	int num;
	int cnt;
	int ans;
} A[MAXN];
int VIS[MAXN];
BOOL CMP (Node A,node b) {
	if (a.cnt==b.cnt)
		return a.num<b.num;
	Return a.cnt<b.cnt;
}
int main ()
{
	int t,n,i,j,k;
	scanf ("%d", &t);
	while (t--) {
		scanf ("%d", &n);
		for (i=0;i<n;++i) {
			scanf ("%d", &a[i].num);
			int m=a[i].num; a[i].cnt=0;
			while (m) {
				m/=2;
				a[i].cnt++;
			}
		}
		memset (vis,0,sizeof (Vis));
		Sort (a,a+n,cmp);
		int ans=0;
		for (k=0;k<n;++k) {
			if (k&&a[k].cnt==a[k-1].cnt) {
				Ans+=a[k-1].ans;
				A[k].ans=a[k-1].ans;
				Continue;
			}
			a[k].ans=0;
			for (i=k+1;i<n;++i) {
				if ((a[k].num^a[i].num) >max (a[k].num,a[i].num)) a[k].ans++;
			}
			Ans+=a[k].ans;
		}
		printf ("%d\n", ans);
	}
	return 0;
}

May 1, 2016 update

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <list>
#include <queue>
#include <vector>
using namespace std;
const int maxn=1000010;
/*
thinking when the same number of digits must not be when the number of digits is not the same as if the number of bits can be 
*/
struct node{
	int num,cnt;
} A[MAXN];
int bit[35];
int main ()
{
	int t,n,k,cnt;
	scanf ("%lld", &t);
	while (t--) {
		scanf ("%d", &n);
		memset (bit,0,sizeof (bit));
		for (int i=1;i<=n;++i) {
			scanf ("%d", &a[i].num);
			int temp=a[i].num,cnt=0;
			while (temp) {temp/=2;cnt++;}
			bit[cnt]++; a[i].cnt=cnt;
		}
		Long long ans=0;
		for (int i=1;i<=n;++i) {for
			(int j=a[i].cnt-1;j>=1;--j) {
				if ((a[i].num^ (1<< (j-1))) >a[i]. num) ans+=bit[j];
			}
		}
		printf ("%lld\n", ans);
	}
	return 0;
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.