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. zabbix自动监控钉钉报警

    钉钉报警 一:设置钉钉机器人  二:zabbix服务器server端配置 1.修改zabbix_server.conf文件 [root@server ~]# vim /usr/local/zabbix ...

  2. 华硕笔记本无法U盘启动,快捷键识别不了

    http://www.udaxia.com/upqd/8254.html 转载于:https://www.cnblogs.com/wanglinjie/p/10507888.html

  3. 如何在mysql中实现自然排序

    背景 熟悉mysql的同学应该清楚,mysql在对字符串做order by排序时是按照字典序进行排序的,但是如果字符串中包含数字的话(我们称这种类型的字符串为alphanumeric),仅按照字典序的 ...

  4. Java反射与注解

    反射 能够分析类能力的程序称为反射(reflective),代码的这种能力称为"自省".反射机制的功能极其强大,反射机制可以用来: 在运行时分析类的能力 在运行时查看对象,例如,编 ...

  5. flutter中使用redux之多界面互动

    在上一篇文章,我们介绍了如何在flutter中使用redux.在上一篇文章的例子中,我们使用了单界面集成redux,但是在实际项目中,我们通常涉及多个模块,每个模块涉及多个界面,那么如何使用redux ...

  6. Mac查看与修改系统默认shell

    Mac查看与修改系统默认shell 查看所有shell cat /etc/shells 输出: # List of acceptable shells for chpass(1). # Ftpd wi ...

  7. Codeforce 1098-A

    A. Sum in the tree   Mitya has a rooted tree with nn vertices indexed from 11 to nn, where the root ...

  8. Kafka平滑滚动升级2.4.0指南

    今天测试了下kafka从2.0.0滚动升级至2.4.0,下面做一下记录.这个链接是Kafka官网对升级2.4.0的指南,可以参考  http://kafka.apache.org/24/documen ...

  9. 内存淘汰机制——LRU与LFU

    内存淘汰机制之LRU与LFU LRU(Least Recently Used):淘汰 近期最不会访问的数据 LFU(Least Frequently Used):淘汰 最不经常使用(访问次数少) 所谓 ...

  10. golang之array

    golang使用array表示固定大小的数组,使用slice表示动态数组. package main import "fmt" func main() { var a = [5]i ...