题目描述

k组数,每组ni个,数互不相同

把每组数中的一个移到其他组(或者不移动),使得最终每组数的个数不变且总和相等

k<=15,ni<=5000

题解

最终的移动关系一定为若干个环

枚举每个环的起点,找到一个数补上去使得和等于平均值

因为互不相同,所以出边(找到的数)唯一

判断是否能成环,并且把对应的选择方案记下

预处理的时间为O(k*∑ni)

最后O(3^n)dp即可

code

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#define fo(a,b,c) for (a=b; a<=c; a++)
#define fd(a,b,c) for (a=b; a>=c; a--)
using namespace std; struct type{
int s,x,y;
} a[75001];
int p[16]={0,1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384};
int b[16][5001];
int c[16];
int d[32768][16][2]; //d[][1][1] is special
int d2[16][2];
bool bz[16];
bool f[32768];
int g[32768];
int ans[16][2];
int D[16];
int n,I,J,i,j,k,l,len,S,s,L,tot;
long long Sum[16];
long long sum; bool cmp(type a,type b)
{
return a.s<b.s;
} int get(long long t)
{
int l=1,r=len,mid; while (l<r)
{
mid=(l+r)/2; if (a[mid].s<t)
l=mid+1;
else
r=mid;
} if (a[l].s==t)
return l;
return -1;
} int main()
{
// freopen("c.in","r",stdin); scanf("%d",&n);L=p[n]*2-1;
fo(i,1,n)
{
scanf("%d",&c[i]);
fo(j,1,c[i])
scanf("%d",&b[i][j]),a[++len]={b[i][j],i,j},sum+=b[i][j],Sum[i]+=b[i][j];
} if (sum%n)
{
printf("No\n");
return 0;
}
sum/=n; sort(a+1,a+len+1,cmp); fo(I,1,n)
{
fo(J,1,c[I])
{
memset(bz,0,sizeof(bz)); S=p[I]; tot=1;
d2[1][0]=I;
d2[1][1]=b[I][J]; j=I;
s=b[I][J]; l=get(sum-(Sum[j]-s)); while (!bz[j])
{
bz[j]=1; if (l==-1 || bz[a[l].x])
break; j=a[l].x;
s=a[l].s;
S|=p[j]; ++tot;
d2[tot][0]=j;
d2[tot][1]=l; l=get(sum-(Sum[j]-s));
} if (l!=-1 && a[l].x==I && a[l].y==J)
{
f[S]=1;
fo(j,1,tot)
{
d[S][j][0]=d2[j][0];
d[S][j][1]=d2[j][1];
}
}
}
} fo(i,1,L)
{
if (f[i])
g[i]=i;
else
{
for (j=(i-1)&i; j; j=(j-1)&i)
if (f[j] && g[i^j])
{
g[i]=j;
break;
}
}
} if (g[L])
{
for (j=L; j; j^=g[j])
{
tot=0;
fo(i,1,n)
if (g[j]&p[i])
++tot; ans[d[g[j]][1][0]][0]=d[g[j]][tot][0];
ans[d[g[j]][1][0]][1]=d[g[j]][1][1];
fo(i,2,tot)
{
ans[d[g[j]][i][0]][0]=d[g[j]][i-1][0];
ans[d[g[j]][i][0]][1]=a[d[g[j]][i][1]].s;
}
} printf("Yes\n");
fo(i,1,n)
printf("%d %d\n",ans[i][1],ans[i][0]);
}
else
printf("No\n");
}

CF1242C. Sum Balance的更多相关文章

  1. Codeforces Round #599 (Div. 1) C. Sum Balance 图论 dp

    C. Sum Balance Ujan has a lot of numbers in his boxes. He likes order and balance, so he decided to ...

  2. Codeforces Round #599 (Div. 2) E. Sum Balance

    这题写起来真的有点麻烦,按照官方题解的写法 先建图,然后求强连通分量,然后判断掉不符合条件的换 最后做dp转移即可 虽然看起来复杂度很高,但是n只有15,所以问题不大 #include <ios ...

  3. [Codeforces 1242C]Sum Balance

    Description 题库链接 给你 \(k\) 个盒子,第 \(i\) 个盒子中有 \(n_i\) 个数,第 \(j\) 个数为 \(x_{i,j}\).现在让你进行 \(k\) 次操作,第 \( ...

  4. SQL Server 中存储过程的练习

    建库建表建约束 插入数据 --建库建表建约束和插入测试数据 use bankDB go --1.完成存款,取款业务--存款 create proc usp_takeMoney ),),)=null,@ ...

  5. order_by_、group_by_、having的用法区别

    写于 2012-11-20 22:14  doc文档上. Having 这个是用在聚合函数的用法.当我们在用聚合函数的时候,一般都要用到GROUP BY 先进行分组,然后再进行聚合函数的运算.运算完后 ...

  6. [POJ] #1004# Financial Management : 浮点数运算

    一. 题目 Financial Management Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 173910   Acc ...

  7. HW4.7

    public class Solution { public static void main(String[] args) { double rate = 0.05; double balance ...

  8. pubwin会员合并

    此博文已移至爬不稳独立博客:www.pubwin2009.net连接:http://www.pubwin2009.net/index.php/post/15.html 我们说下过程(这里,我们要求两个 ...

  9. 事务,Oracle,MySQL及Spring事务隔离级别

    一.什么是事务: 事务逻辑上的一组操作,组成这组操作的各个逻辑单元,要么一起成功,要么一起失败. 二.事务特性(4种): 原子性 (atomicity):强调事务的不可分割:一致性 (consiste ...

随机推荐

  1. unity拖尾粒子问题

    拖尾粒子有一个问题就是当设置父物体时候,拖动父物体,就没有拖尾效果了 此时只需设置Emitter Velocity的类型为 transform 就行了 还有一种设置simulation space类型 ...

  2. Virtual DOM和snabbdom.js

    Virtual DOM和snabbdom.js:https://www.jianshu.com/p/1f1ef915e83e

  3. LOJ 10214 计算器 题解

    题面 k==1时,快速幂就好了: k==2时,exgcd就好了,但要注意取模范围的控制: k==3时,BSGS可以解决高次同余方程: 然后就可以开心的A掉了,但要注意特殊情况的特判 #include ...

  4. Luogu P1080 [NOIP2012]国王游戏

    题目 按\(a_i*b_i\)升序排序即可. 证明考虑交换法. 对于排序后相邻的两个人\(i,j(a_ib_i\le a_jb_j)\),设前面的总的积为\(s\),则当前答案为\(\max(\fra ...

  5. Android-Widget桌面小组件

    1, 掌握Widget的用:Widget的用途,能够添加到手机桌面的程序 2, Widget的特点和用法步骤: 特点:快捷,方便,个性化,可自定义功能,可及时控制更新Widget显示内容 3, 用法步 ...

  6. 字符集详解 ASCII码、Unicode、UTF-8 (转)

    认识字符集 对于计算机而言,它仅认识两个0和1,不管是在内存中还是外部存储设备上,我们所看到的文字.图片.视频等等“数据”在计算机中都是已二进制形式存在的.不同字符对应二进制数的规则,就是字符的编码. ...

  7. 2019牛客暑期多校训练营(第九场) - B - Quadratic equation - 二次剩余

    https://ac.nowcoder.com/acm/contest/889/B 假如我们能够求出 \(x-y\) 在模p意义的值,那么就可以和 \(x+y\) 联立解出来了. 由于 \((x-y) ...

  8. 2018-2019 ICPC, NEERC, Southern Subregional Contest (codeforces 1070)

    A. 直接从状态(0,0)bfs, 这样一定是最小的 #include <iostream> #include <sstream> #include <algorithm ...

  9. [WPF]鼠标位置捕捉

    private void StackPanel_MouseMove(object sender, MouseEventArgs e) {     Debug.WriteLine("Move& ...

  10. wex5 windowDialog组件

    windowDialog组件可以实现页面跳转 xid:windowDialog src:$UI/lohas/detailedknowledge.w 父页:myknowledge.w Model.pro ...