The Maximum Number of Strong Kings
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 2302   Accepted: 1056

Description

A tournament can be represented by a complete graph in which each vertex denotes a player and a directed edge is from vertex x to vertex y if player x beats player y. For a player x in a tournament T, the score of x is the number of players beaten by x. The score sequence of T, denoted by S(T) = (s1, s2, . . . , sn), is a non-decreasing list of the scores of all the players in T. It can be proved that S(T) = (s1, s2, . . . , sn) is a score sequence of T if and only if
for k = 1, 2, . . . , n and equality holds when k = n. A player x in a tournament is a strong king if and only if x beats all of the players whose scores are greater than the score of x. For a score sequence S, we say that a tournament T realizes S if S(T) = S. In particular, T is a heavy tournament realizing S if T has the maximum number of strong kings among all tournaments realizing S. For example, see T2 in Figure 1. Player a is a strong king since the score of player a is the largest score in the tournament. Player b is also a strong king since player b beats player a who is the only player having a score larger than player b. However, players c, d and e are not strong kings since they do not beat all of the players having larger scores.
The purpose of this problem is to find the maximum number of strong kings in a heavy tournament after a score sequence is given. For example,Figure 1 depicts two possible tournaments on five players with the same score sequence (1, 2, 2, 2, 3). We can see that there are at most two strong kings in any tournament with the score sequence (1, 2, 2, 2, 3) since the player with score 3 can be beaten by only one other player. We can also see that T2 contains two strong kings a and b. Thus, T2 is one of heavy tournaments. However, T1 is not a heavy tournament since there is only one strong king in T1. Therefore, the answer of this example is 2.

Input

The first line of the input file contains an integer m, m <= 10, which represents the number of test cases. The following m lines contain m score sequences in which each line contains a score sequence. Note that each score sequence contains at most ten scores.

Output

The maximum number of strong kings for each test case line by line.

Sample Input

5
1 2 2 2 3
1 1 3 4 4 4 4
3 3 4 4 4 4 5 6 6 6
0 3 4 4 4 5 5 5 6
0 3 3 3 3 3

Sample Output

2
4
5
3
5 神奇的构图
把每个人和每场比赛看成两种点
源点向每个人连,cap为其得分
每场比赛向汇点连,cap为1
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=;
int head[N],tot,S,T;
int q[N],dis[N],n,sc[];
struct node
{
int next,v,w;
} e[N];
void add(int u,int v,int w)
{
e[tot].v=v;
e[tot].w=w;
e[tot].next=head[u];
head[u]=tot++;
}
bool bfs()
{
memset(dis,-,sizeof(dis));
dis[S]=;
int l=,r=;
q[r++]=S;
while(l<r)
{
int u=q[l++];
for(int i=head[u]; ~i; i=e[i].next)
{
int v=e[i].v;
if(dis[v]==-&&e[i].w>)
{
q[r++]=v;
dis[v]=dis[u]+;
if(v==T) return true;
}
}
}
return false;
}
int dfs(int s,int low)
{
if(s==T||!low) return low;
int ans=low,a;
for(int i=head[s]; ~i; i=e[i].next)
{
if(e[i].w>&&dis[e[i].v]==dis[s]+&&(a=dfs(e[i].v,min(e[i].w,ans))))
{
e[i].w-=a;
e[i^].w+=a;
ans-=a;
if(!ans) return low;
}
}
if(low==ans) dis[s]=-;
return low-ans;
}
bool Ju(int x){
tot=;
memset(head,-,sizeof(head));
for(int i=;i<n;++i) add(S,i+,sc[i]),add(i+,S,);
for(int i=n+;i<=(n*n+n)/;++i) add(i,T,),add(T,i,);
int pos[][],tc=n+;
for(int i=;i<=n;++i) for(int j=i+;j<=n;++j) pos[i][j]=pos[j][i]=tc++;
for(int i=;i<x;++i) for(int j=i+;j<=n;++j) {
add(i,pos[i][j],),add(pos[i][j],i,);
add(j,pos[i][j],),add(pos[i][j],j,);
}
for(int i=x;i<=n;++i) for(int j=i+;j<=n;++j) {
add(i,pos[i][j],),add(pos[i][j],i,);
if(sc[i-]==sc[j-]) add(j,pos[i][j],),add(pos[i][j],j,);
}
int ans=;
while(bfs()) ans+=dfs(S,);
return ans==n*(n-)/;
}
int main(){
int Ta;
for(scanf("%d ",&Ta);Ta--;){
char str[];
gets(str);
int len=strlen(str);
n=(len+)/;
if(n==) {puts("");continue;}
S=,T=(n*n+n)/+;
for(int i=;i<len;i+=) sc[i>>]=str[i]-'';
int l=,r=n,ans=;
sort(sc,sc+n);
while(l<=r){
int mid=(l+r)>>;
if(Ju(mid)) {r=mid-;ans=n-mid+;}
else l=mid+;
}
printf("%d\n",ans);
}
}

poj2699 转化为可行性判定问题+二分枚举+最大流的更多相关文章

  1. 枚举 转化为可行性判定问题 网络流 poj3189

    Steady Cow Assignment Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6914   Accepted: ...

  2. FZU-2216 The Longest Straight (二分枚举)

    题目大意:给n个0~m之间的数,如果是0,那么0可以变为任意的一个1~m之间的一个数.从中选出若干个数,使构成一个连续的序列.问能构成的最长序列的长度为多少? 题目分析:枚举连续序列的起点,二分枚举二 ...

  3. uva 12587 二分枚举

    思路:维护一个森林,二分枚举最小的最大值. #include<set> #include<map> #include<cmath> #include<queu ...

  4. SDIBT 3237 Boring Counting( 划分树+二分枚举 )

    http://acm.sdibt.edu.cn/JudgeOnline/problem.php?id=3237 Problem H:Boring Counting Time Limit: 3 Sec  ...

  5. POJ 3273 Monthly Expense 二分枚举

    题目:http://poj.org/problem?id=3273 二分枚举,据说是经典题,看了题解才做的,暂时还没有完全理解.. #include <stdio.h> #include ...

  6. POJ 2112 Optimal Milking(Floyd+多重匹配+二分枚举)

    题意:有K台挤奶机,C头奶牛,每个挤奶机每天只能为M头奶牛服务,下面给的K+C的矩阵,是形容相互之间的距离,求出来走最远的那头奶牛要走多远   输入数据: 第一行三个数 K, C, M  接下来是   ...

  7. hdu 5248 序列变换(二分枚举)

    Problem Description 给定序列A={A1,A2,...,An}, 要求改变序列A中的某些元素,形成一个严格单调的序列B(严格单调的定义为:Bi<Bi+,≤i<N). 我们 ...

  8. HDU 1669 Jamie's Contact Groups(多重匹配+二分枚举)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1669 题目大意: 给你各个人可以属于的组,把这些人分组,使这些组中人数最多的组人数最少,并输出这个人数 ...

  9. Codeforces 807C - Success Rate(二分枚举)

    题目链接:http://codeforces.com/problemset/problem/807/C 题目大意:给你T组数据,每组有x,y,p,q四个数,x/y是你当前提交正确率,让你求出最少需要再 ...

随机推荐

  1. java 之 继承 super关键籽 this关键字 final关键字

    继承 语法: 使用 extends 来继承 class子类    extends父类{ 子类属性 子类方法 } 继承的特点: 1.子类会把父类所有的属性和方法继承下来,final修饰的类是不可以被继承 ...

  2. 从零开始搭建口袋妖怪管理系统(4)-借助webpack4.6工程化项目(上)

    "手动是不可能手动的了,这辈子都不可能手动的了." 一.目标 上一章我们借助ngRoute,完成了口袋妖怪SPA系统的多模块导航开发,但是现在引用的东西越来越多,项目文件目录开始变 ...

  3. 由JS数组去重说起

    一.问题描述: var array=[1,45,3,1,4,67,45],请编写一个函数reDup来去掉其中的重复项,即 reDup(array); console.log(array);//[1,4 ...

  4. Centos7下查询jdk安装路径

    今天一个小实验需要安装jdk,用命令java -version查询了一下,原来Centos7自带OpenJDK的环境,但是需要手动配置/etc/profile文件,于是开始找java的安装路径.... ...

  5. POJ Building a Space Station 最小生成树

    Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 15664   Accepted: 6865 Description You ...

  6. Servlet 教程——检视阅读

    Servlet 教程--检视阅读 参考 Servlet教程--菜鸟--蓝本 Servlet教程--w3cschool Servlet教程--易百 servlet依赖maven依赖: <!--se ...

  7. 学习笔记之pip的基本使用

    粗略学习了pip的基础知识,便将此作为学习笔记记录下来同样希望分享的能帮到大家! 如果自己电脑没有pip,小澈在此分享如何安装,解决办法很多呢 1.使用easy_install安装: 各种进入到eas ...

  8. 数据结构之递归Demo(走迷宫)(八皇后)(汉诺塔)

    递归 顾名思义,递归就是递归就是递归就是递归就是递归......就是递归 Google递归:

  9. 布局问题杂(html和css)

    \(一.删除线可以用一对strike标签括起来\) <p><strike>删除线可以用一对strike标签括起来</strike></p> \(\col ...

  10. 201771010113 李婷华 《面向对象程序设计(java)》第七周学习总结

    一.理论知识学习部分 1.动态绑定:又称为运行时绑定.程序在运行时会自动选择调用哪个方法. 2.静态绑定:如果方法是private.static.final修饰的,或者是构造器,那么编译器能准确地判断 ...