(第七场)A Minimum Cost Perfect Matching 【位运算】
题目链接:https://www.nowcoder.com/acm/contest/145/A A、Minimum Cost Perfect MatchingYou have a complete bipartite graph where each part contains exactly n nodes,numbered from 0 to n - 1 inclusive. 输入描述:The input contains a single integer n (1 ≤ n ≤ 5e5). 输出描述: Output n space-separated integers,where the i-th integer denotes p i (0 ≤ pi ≤ n - 1,the number of the vertex in the right part that is matched with the vertex numbered i in the left part. All pi should be distinct. 示例1:输入 3 输出 0 2 1 说明 For n = 3,p0 = 0,p1 = 2,p2 = 1 works. You can check that the total cost of this matching is 0,which is obviously minimal. ? 题意概括:求0~N-1的一个排列 p, 使得p[ i ] ^ i 的总和最小。 官方题解:? 最优解的和一定是0。 举例 解题思路:题解给的是常识和构造。 最优和为 0 毋庸置疑。结合这道题思考,有按位与运算,我们可以从大到小把 i 取反(则相与的结果肯定为0),然后求该数 i 的最高位,保证取反的情况下不超过N的范围。 ? AC code: 1 #include <cstdio> 2 #include <iostream> 3 #include <algorithm> 4 #include <cstring> 5 #include <cmath> 6 #include <bitset> 7 #define INF 0x3f3f3f3f 8 using namespace std; 9 10 const int MAXN = 5e5+10; 11 int num[MAXN],a; 12 int N; 13 int hb(int k) 14 { 15 int n = 0; 16 while(k > 0) 17 { 18 n++; 19 k>>=1; 20 } 21 return n; 22 } 23 int main() 24 { 25 scanf("%d",&N); 26 bitset<32> b; 27 memset(num,-1,sizeof(num)); 28 for(int i = N-1; i >= 0; i--) 29 { 30 if(num[i] == -1) 31 { 32 b = ~i; 33 int len = hb(i); 34 int t = 1; 35 int sum = 0; 36 for(int k = 0; k < len; k++) 37 { 38 sum+=b[k]*t; 39 t*=2; 40 } 41 num[i] = sum; 42 num[sum] = i; 43 } 44 } 45 for(int i = 0; i < N; i++) 46 { 47 printf("%d",num[i]); 48 if(i < N-1) printf(" "); 49 } 50 puts(""); 51 return 0; 52 } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |