链接:https://ac.nowcoder.com/acm/contest/882/F
来源:牛客网

Given 2N people, you need to assign each of them into either red team or white team such that each team consists of exactly N people and the total competitive value is maximized.

Total competitive value is the summation of competitive value of each pair of people in different team.
The equivalent equation is ∑2Ni=1∑2Nj=i+1(vij if i-th person is not in the same team as j-th person else 0)∑i=12N∑j=i+12N(vij if i-th person is not in the same team as j-th person else 0)

输入描述:

  1. The first line of input contains an integers N.
  2.  
  3. Following 2N lines each contains 2N space-separated integers vijvij is the j-th value of the i-th line which indicates the competitive value of person i and person j.
  4.  
  5. * 1N141N14
    * 0vij1090vij109
    * vij=vjivij=vji

输出描述:

  1. Output one line containing an integer representing the maximum possible total competitive value.
示例1

输入

  1. 1
  2. 0 3
  3. 3 0

输出

  1. 3
  2.  
  3. 题意:
    n*2个人分为两部分,每一个人与另外一半的每一个人贡献一个权值,求贡献和的最大值。
    思路:
    暴力搜索,最坏的复杂度是C(2*14,14),也就是差不多4e7,如果你确定某一个人在第一部分,还可以将复杂度除而2
    关于算贡献,你可以选择14*14的复杂度,但是肯定会T
    在搜索的时候,如果n=5,那么第一次选在第一部分的人就是 1 2 3 4 5.
    第二次选在第一部分的人就是 1 2 3 4 6,可以发现只有一个数字不同。
    分析一下,其实在整个搜索的过程中,也会出现很多这样只差一个的数组。
    于是,我们可以记录上一个状态,通过上个状态算出当前状态,这样可以减小很多算贡献的复杂度。
    就这样,我的代码跑了3700ms之后卡过去了。
  1. #include <bits/stdc++.h>
  2. #define eps 1e-8
  3. #define INF 0x3f3f3f3f
  4. #define PI acos(-1)
  5. #define lson l,mid,rt<<1
  6. #define rson mid+1,r,(rt<<1)+1
  7. #define CLR(x,y) memset((x),y,sizeof(x))
  8. #define fuck(x) cerr << #x << "=" << x << endl;
  9. using namespace std;
  10. typedef long long ll;
  11. typedef unsigned long long ull;
  12. const int seed = ;
  13. const int maxn = 1e5 + ;
  14. const int mod = 1e9 + ;
  15. int n;
  16. ll mp[][];
  17. bool vis[];
  18. ll MIN;
  19. int v1[];
  20. int v2[];
  21. int prev1[];
  22. int prev2[];
  23. ll prenum = ;
  24. ll C[][];
  25. //C[n][m]就是C(n,m)
  26. int tot;
  27. void init(int N) {
  28. for (int i = ; i < N; i ++) {
  29. C[i][] = C[i][i] = ;
  30. for (int j = ; j < i; j ++) {
  31. C[i][j] = C[i - ][j] + C[i - ][j - ];
  32. C[i][j] %= mod;
  33. }
  34. }
  35. }
  36.  
  37. void dfs(int x, int deep) {//必须>=x开始,已经选了num个人
  38. if (deep == n) {
  39. tot--;
  40. if(tot<){return;}
  41. int cnt1 = ;
  42. int cnt2 = ;
  43. for (int i = ; i <= * n; i++) {
  44. if (vis[i]) v1[++cnt1] = i;
  45. else v2[++cnt2] = i;
  46. }
  47. ll num = prenum;
  48. int pos = ;
  49. for (int i = ; i <= n; i++) {
  50. if (v1[i] != prev1[i]) {
  51. pos = i;
  52. break;
  53. }
  54. }
  55. for (int i = pos; i <= n; i++) {
  56. for (int j = ; j <= n; j++) {
  57. num -= mp[prev1[i]][prev2[j]];
  58. num -= mp[v1[i]][prev1[j]];
  59. }
  60. for (int j = ; j <= n; j++) {
  61. num += mp[v1[i]][v2[j]];
  62. num += mp[v1[j]][prev1[i]];
  63. }
  64. }
  65. MIN = max(MIN, num);
  66. for (int i = ; i <= n; i++) {
  67. prev1[i] = v1[i];
  68. prev2[i] = v2[i];
  69. prenum = num;
  70. }
  71. return ;
  72. }
  73. for (int i = x + ; i <= * n; i++) {
  74. vis[i] = ;
  75. dfs(i, deep + );
  76. if(tot<){return;}
  77. vis[i] = ;
  78. }
  79. }
  80. int main() {
  81. MIN = -;
  82. init();
  83. scanf("%d", &n);
  84. tot=C[*n][n];
  85. tot/=;
  86. for (int i = ; i <= * n; i++) {
  87. for (int j = ; j <= * n; j++) {
  88. scanf("%lld", &mp[i][j]);
  89. }
  90. }
  91. dfs(, );
  92. printf("%lld\n", MIN);
  93. return ;
  94. }

2019牛客暑期多校训练营(第二场)F.Partition problem的更多相关文章

  1. 2019牛客暑期多校训练营(第二场) H-Second Large Rectangle(单调栈)

    题意:给出由01组成的矩阵,求求全是1的次大子矩阵. 思路: 单调栈 全是1的最大子矩阵的变形,不能直接把所有的面积存起来然后排序取第二大的,因为次大子矩阵可能在最大子矩阵里面,比如: 1 0 0 1 ...

  2. 2020牛客暑期多校训练营 第二场 K Keyboard Free 积分 期望 数学

    LINK:Keyboard Free 我要是会正经的做法 就有鬼了. 我的数学水平没那么高. 三个同心圆 三个动点 求围成三角形面积的期望. 不会告辞. 其实可以\(n^2\)枚举角度然后算出面积 近 ...

  3. 2020牛客暑期多校训练营 第二场 J Just Shuffle 置换 群论

    LINK:Just Shuffle 比较怂群论 因为没怎么学过 置换也是刚理解. 这道题是 已知一个置换\(A\)求一个置换P 两个置换的关键为\(P^k=A\) 且k是一个大质数. 做法是李指导教我 ...

  4. 2020牛客暑期多校训练营 第二场 I Interval 最大流 最小割 平面图对偶图转最短路

    LINK:Interval 赛时连题目都没看. 观察n的范围不大不小 而且建图明显 考虑跑最大流最小割. 图有点稠密dinic不太行. 一个常见的trick就是对偶图转最短路. 建图有点复杂 不过建完 ...

  5. 2020牛客暑期多校训练营 第二场 C Cover the Tree 构造 贪心

    LINK:Cover the Tree 最受挫的是这道题,以为很简单 当时什么都想不清楚. 先胡了一个树的直径乱搞的贪心 一直过不去.后来意识到这类似于最经典长链剖分优化贪心的做法 然后那个是求最大值 ...

  6. 2020牛客暑期多校训练营 第二场 B Boundary 计算几何 圆 已知三点求圆心

    LINK:Boundary 计算几何确实是弱项 因为好多东西都不太会求 没有到很精通的地步. 做法很多,先说官方题解 其实就是枚举一个点 P 然后可以发现 再枚举一个点 然后再判断有多少个点在圆上显然 ...

  7. 2020牛客暑期多校训练营 第二场 A All with Pairs 字符串hash KMP

    LINK:All with Pairs 那天下午打这个东西的时候状态极差 推这个东西都推了1个多小时 (比赛是中午考试的我很困 没睡觉直接开肝果然不爽 一开始看错匹配的位置了 以为是\(1-l\)和\ ...

  8. 2019牛客暑期多校训练营(第九场) D Knapsack Cryptosystem

    题目 题意: 给你n(最大36)个数,让你从这n个数里面找出来一些数,使这些数的和等于s(题目输入),用到的数输出1,没有用到的数输出0 例如:3  4 2 3 4 输出:0 0 1 题解: 认真想一 ...

  9. 2019 牛客暑期多校 第三场 F Planting Trees (单调队列+尺取)

    题目:https://ac.nowcoder.com/acm/contest/883/F 题意:求一个矩阵最大面积,这个矩阵的要求是矩阵内最小值与最大值差值<=m 思路:首先我们仔细观察范围,我 ...

  10. 2019牛客暑期多校训练营(第五场)G - subsequeue 1 (一题我真的不会的题)

    layout: post title: 2019牛客暑期多校训练营(第五场)G - subsequeue 1 (一题我真的不会的题) author: "luowentaoaa" c ...

随机推荐

  1. Promise https://www.liaoxuefeng.com/wiki/1022910821149312/1023024413276544

    在JavaScript的世界中,所有代码都是单线程执行的. 由于这个“缺陷”,导致JavaScript的所有网络操作,浏览器事件,都必须是异步执行.异步执行可以用回调函数实现: function ca ...

  2. Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第二十二章:四元数(QUATERNIONS)

    原文:Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第二十二章:四元数(QUATERNIONS) 学习目标 回顾复数,以及 ...

  3. thinkphp5.0 验证提示信息的类型

    以上是5.0.12版本 下面是5.0.5版本,没有elseif 上图中红方格的值只能是string类型,但是这种情况是在5.0.5版本是可以设置为array类型的

  4. 集合--List&&ArrayList-LinkedList

    1.8新特性  List接口中的replaceAll()方法,替换指定的元素,函数式接口编程 List  元素是有序的并且可以重复 四种add();方法 ArrayList(用于查询操作),底层是数组 ...

  5. 在哈尔滨的寒风中EOJ 3461【组合数学】

    http://acm.ecnu.edu.cn/problem/3461/ 还是能力不够,不能看出来棋盘足够大时,马是可以到达任何位置的.还是直接看题解怎么说的吧:(http://acm.ecnu.ed ...

  6. 通过iOS 9 SFSafariViewController提供完整的Web浏览体验

    http://www.cocoachina.com/ios/20150826/13157.html 本文由CocoaChina译者@涛声依旧-忆往昔翻译自tutsplus校对:BenBeng原文:iO ...

  7. python 捕获异常

  8. JavaScript void

    我们经常会使用到 javascript:void(0) 这样的代码,那么在 JavaScript 中 javascript:void(0) 代表的是什么意思呢? javascript:void(0) ...

  9. 1<<33这种写法是错的!!!

    1<<33不能这么写,1默认int类型,应该改为(long long)1<<33

  10. HDU-1069_Monkey and Banana

    Monkey and Banana Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) P ...