CodeForces 468B Two Sets 二分匹配

把n个数放到集合A和集合B

使得:

对于某个数x,若x在A集合则 a-x也必须在A集合(若a-x不存在于n个数中,则x不能放在A集合)

放在B集合同理。

输出任意解:每个数放在哪个集合里(允许n个数都放一个集合)

思路:

类似二分匹配的做法。

[java] view plaincopy

    1. import java.io.PrintWriter;
    2. import java.util.ArrayList;
    3. import java.util.Arrays;
    4. import java.util.Collections;
    5. import java.util.HashMap;
    6. import java.util.HashSet;
    7. import java.util.Iterator;
    8. import java.util.List;
    9. import java.util.Map;
    10. import java.util.Scanner;
    11. import java.util.Set;
    12. import java.util.TreeSet;
    13. public class Main {
    14. int work(int x){
    15. if(x%2==0)return x+1;
    16. else return x-1;
    17. }
    18. static int N = 200050;
    19. class Node implements Comparable <Node>{
    20. int x, id;
    21. Node(int x, int id){
    22. this.x = x; this.id = id;
    23. }
    24. public int compareTo(Node o){
    25. return Integer.compare(x, o.x);
    26. }
    27. public String toString(){
    28. return id + "=" + x;
    29. }
    30. }
    31. class Edge{
    32. int from, to, nex;
    33. Edge (int from, int to, int nex){
    34. this.from = from;
    35. this.to = to;
    36. this.nex = nex;
    37. }
    38. }
    39. Edge[] edge = new Edge[N*10];
    40. int[] head = new int[N];
    41. int edgenum;
    42. void addedge(int u, int v){
    43. Edge E = new Edge(u, v, head[u]);
    44. edge[edgenum] = E;
    45. head[u] = edgenum ++;
    46. }
    47. int n;
    48. int[] p = new int[N], ans = new int[N];
    49. int a, b, max;
    50. Map<Integer, Integer> map = new HashMap();
    51. boolean match(int x, int y, int col){
    52. int P = map.get(x);
    53. if(map.containsKey(y-x) == false)
    54. return false;
    55. int Q = map.get(y - x);
    56. if(ans[Q] == -1 || x * 2 == y){
    57. ans[Q] = ans[P] = col;
    58. }
    59. else {
    60. if(match(a+b-2*y+x, y, col))
    61. ans[Q] = ans[P] = col;
    62. else return false;
    63. }
    64. return true;
    65. }
    66. boolean solve(){
    67. if(max >= a && max >= b)return false;
    68. for(int i = 1; i <= n; i++)
    69. if(ans[i] == -1)
    70. {
    71. if(match(p[i], a, 0)==false && match(p[i], b, 1) == false)
    72. return false;
    73. }
    74. return true;
    75. }
    76. void init(){
    77. n = cin.nextInt();
    78. a = cin.nextInt(); b = cin.nextInt();
    79. max = 0;
    80. for(int i = 1; i <= n; i++){
    81. ans[i] = -1;
    82. p[i] = cin.nextInt();
    83. map.put(p[i], i);
    84. if(p[i] > max) max = p[i];
    85. }
    86. }
    87. public void work(){
    88. init();
    89. if(solve()){
    90. out.println("YES");
    91. for(int i = 1; i <= n; i++)out.print(ans[i]+" "); out.println();
    92. }
    93. else
    94. out.println("NO");
    95. }
    96. Main() {
    97. cin = new Scanner(System.in);
    98. out = new PrintWriter(System.out);
    99. }
    100. public static void main(String[] args) {
    101. Main e = new Main();
    102. e.work();
    103. out.close();
    104. }
    105. public Scanner cin;
    106. public static PrintWriter out;
    107. }
时间: 2024-10-10 17:18:28

CodeForces 468B Two Sets 二分匹配的相关文章

Codeforces 468B Two Sets(二分图匹配)

题目链接:Codeforces 468B Two Sets 题目大意:给出n个数,要求将n个数分配到两个集合中,集合0中的元素x,要求A-x也再0中,同理1集合. 解题思路:类似二分图匹配的方法. #include <cstdio> #include <cstring> #include <map> #include <stack> #include <algorithm> using namespace std; const int maxn

Codeforces 468B Two Sets 并查集

题目大意:给出n个数,要求将n个数分配到两个集合中,集合0中的元素x,要求A-x也再0中,同理1集合. 写了几个版本,一直WA在第8组数据...最后参考下ans,写了并查集过了 学到:1.注意离散的逻辑思维,官方答案的 从条件推逆否命题 2.并查集做法:fa[find(i)]=mp[a-p[i]] ? find(a-p[i]) : find(n+2); 3.离散化然后hash的方法,用map时间还是承受得住的,写起来也简单 //#pragma comment(linker, "/STACK:10

二分匹配最大匹配 PKU1469

一个学生可以有多种选择,问能否每个学生刚好选一门课,但是每门课最多只有一个学生可以选择 典型的二分匹配最大匹配,直接套模板 COURSES Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17878   Accepted: 7048 Description Consider a group of N students and P courses. Each student visits zero, one or more

hdu1151 poj1422 最小路径覆盖.最大二分匹配

Air RaidTime Limit:1000MS    Memory Limit:10000KB    64bit IO Format:%I64d & %I64u SubmitStatusPracticePOJ 1422 Appoint description: Description Consider a town where all the streets are one-way and each street leads from one intersection to another.

poj 1422 Air Raid (二分匹配)

Air Raid Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6520   Accepted: 3877 Description Consider a town where all the streets are one-way and each street leads from one intersection to another. It is also known that starting from an i

poj 1469 COURSES (二分匹配)

COURSES Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16877   Accepted: 6627 Description Consider a group of N students and P courses. Each student visits zero, one or more than one courses. Your task is to determine whether it is poss

hduoj-----(1068)Girls and Boys(二分匹配)

Girls and Boys Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7577    Accepted Submission(s): 3472 Problem Description the second year of the university somebody started a study on the romant

HDU 1083 裸的二分匹配

Courses Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3424    Accepted Submission(s): 1650 Problem Description Consider a group of N students and P courses. Each student visits zero, one or

poj 2584 T-Shirt Gumbo (二分匹配)

T-Shirt Gumbo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2571   Accepted: 1202 Description Boudreaux and Thibodeaux are student volunteers for this year's ACM South Central Region's programming contest. One of their duties is to dis