【ZOJ - 3870】Team Formation(异或,思维)
題干:
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?Tindicating 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?ithinteger 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 5Sample Output
1 6題目大意:
給n個數。任選兩個數,如果異或大于他們兩個數,那么方案數+1。問有多少種方案數。
解題報告:
可以發現,如果要讓一個數增大,只要該數化為二進制后的出現0的位置跟1異或就會變大,同時需要滿足另一個數的最高位為該數出現0位置的位數,如10可以跟1異或變為11 ,100可以跟10、11、1異或分別變為110,111,101,而101只能跟兩位的進行異或,因為它的0出現的位置為第二位。
以上部分來自某題解、、
就說下實現吧:其實很簡單排個序后從小往大掃,邊統計答案邊更新數組就行了。
這種題做多了。。見到都感覺是套路了、、
AC代碼:
#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define F first #define S second #define ll long long #define pb push_back #define pm make_pair using namespace std; typedef pair<int,int> PII; const int MAX = 2e5 + 5; int a[MAX]; int cnt[MAX]; int c[MAX]; int main() {int t,n;cin>>t;while(t--){memset(cnt,0,sizeof(cnt));scanf("%d",&n);for(int i = 1; i<=n; i++) scanf("%d",a+i);sort(a+1,a+n+1);for(int x,cur,i = 1; i<=n; i++){x=a[i],cur = 0;while(x) {cnt[++cur]+=x%2;x/=2;}c[i]=cur;}ll ans=0;int all=n;for(int x,cur,i = 1; i<=n; i++) {ans += all-cnt[c[i]];all--;x=a[i],cur=0;while(x) {cnt[++cur] -= x%2;x/=2;}}printf("%lld\n",ans);} return 0; }?
總結
以上是生活随笔為你收集整理的【ZOJ - 3870】Team Formation(异或,思维)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 人家细菌肉眼都看不见 它却能长到2厘米长
- 下一篇: 【CodeForces - 508B】A