题意:

      有n个人,任意两个人都比一次赛(一共比了n*(n-1)/2场),赢一场得到一分,最后的时候如果得分最高,或者是自己打败了所有比自己得分都高的人就算是King,给你每个人的最后得分,问最多有多少个人是King.

思路:

      一开始上了就贪心了一次,WA了,改了改之后又贪心了一次,还是没过,然后换思路(其实做这个题目是本着最大流来的),然后就去考虑最大流,最大流的话也比较容易理解,关键是枚举的时候有点说道,先说下建图,把每个人看成一个点,然后把每场比赛看成一个点,源点连接所有人,流量为得分,所有比赛连接汇点,流量1,说到这估计大多数都能建图了吧,接着说,然后就是对于那些King的点,如果和他相关的比赛节点中的另一个点比他分高,那么他一定要赢这场比赛,那么他连接这场比赛的点,否则的话就是不确定,谁赢都行,那么和这场比赛相关的两个点都连接这场比赛就行了,最多11个人左右吧,那么可以直接枚举谁是King,最保守而且没争议的方法就是暴力搜索枚举,就是01枚举,2^10*最大流时间,这个完全可以接受,然后就是网上说的那种直接枚举个数的说法,这个说法要明确一个问题,那就是如果当前有m个King,那么这m个不一定必须是后m个,也可以是中间的一些个,但是按照结论反推可以知道,这m个可以是后m个,也就是说即使不是后m个也可以转换成后m个,所以直接枚举后m就行,关键是要明白不是后m个是怎么可以转换成后m个的,我的想法一开始是找两个点,假设1..i..j..n,假如i是King,而j不是,那么就想办法吧ij的关系调换,就是让i变成不是King,而j是King,按照结论反推肯定是可以调换的,但是我想了很多方法都没有平衡掉ij之间的数字,找网上的也没有可以让我信服的,但是我自己有一个猜想,那就是我感觉可以按照贪心的方式去理解,越往后的称为King的代价就越小,也就是消耗的总的固定流量就越小,那么当然是越往后越可能了,枚举当然那是从后面开始,还有一个问题就是我感觉如果当前序列有k个可以称为King的话,那么一定可以转换成后k个,但是如果当前是后k个的话,不一定能往前转换,也就是我感觉后k个是最优的选择,就是类似那种后k个可以,但是还余出来一些资源没用,通过这些资源可以吧后k个中的一些挪到前面来,这样就产生了答案不以为的情况,但是直接枚举后k个是最优的方式,所有是正确的,这个也只是自己的感觉而已,已过想证明必须要把得分平衡掉。。。。下面是两种方法的代码,我的流用的是DINIC,估计姿势不优吧,速度和网上别人的速度比慢了很多.

DFS+DINIC

#include<queue>

#include<stdio.h>

#include<string.h>

#define N_node 150

#define N_edge 5000

#define INF 1000000000

using namespace std;

typedef struct

{

    int to ,cost ,next;

}STAR;

typedef struct

{

    int x ,t;

}DEP;

STAR E[N_edge];

DEP xin ,tou;

int list[N_node] ,listt[N_node] ,tot;

int deep[N_node];

void add(int a ,int b ,int c)

{

    E[++tot].to = b;

    E[tot].cost = c;

    E[tot].next = list[a];

    list[a] = tot;

    E[++tot].to = a;

    E[tot].cost = 0;

    E[tot].next = list[b];

    list[b] = tot;

}

int minn(int x ,int y)

{

    return x < y ? x : y;

}

bool BFS_DEEP(int s ,int t ,int n)

{

    memset(deep ,255 ,sizeof(deep));

    xin.x = s ,xin.t = 0;

    queue<DEP>q;

    q.push(xin);

    deep[s] = 0;

    while(!q.empty())

    {

        tou = q.front();

        q.pop();

        for(int k = list[tou.x] ;k ;k = E[k].next)

        {

            xin.x = E[k].to;

            xin.t = tou.t + 1;

            if(deep[xin.x] != -1 || !E[k].cost)

            continue;

            deep[xin.x] = xin.t;

            q.push(xin);

        }

    }

    for(int i = 0 ;i <= n ;i ++)

    listt[i] = list[i];

    return deep[t] != -1;

}

int DFS_Flow(int s ,int t ,int flow)

{

    if(s == t) return flow;

    int nowflow = 0;

    for(int k = listt[s] ;k ;k = E[k].next)

    {

        listt[s] = k;

        int c = E[k].cost;

        int to = E[k].to;

        if(!c || deep[to] != deep[s] + 1)

        continue;

        int tmp = DFS_Flow(to ,t ,minn(c ,flow - nowflow));

        nowflow += tmp;

        E[k].cost -= tmp;

        E[k^1].cost += tmp;

        if(nowflow == flow) break;

    }

    if(!nowflow) deep[s] = 0;

    return nowflow;

}

int DINIC(int s ,int t ,int n)

{

    int ans = 0;

    while(BFS_DEEP(s ,t ,n))

    {

        ans += DFS_Flow(s ,t ,INF);

    }

    return ans;

}

int now[15] ,num[15];

int Ans ,maxn ,n;

void Flow()

{

    memset(list ,0 ,sizeof(list));

    tot = 1;

    for(int i = 1 ;i <= n ;i ++)

    add(0 ,i ,num[i]);

    int nowid = n;

    for(int i = 1 ;i <= n ;i ++)

    for(int j = i + 1 ;j <= n ;j ++)

    {

        ++nowid;

        if(num[i] < num[j] && now[i])

        add(i ,nowid ,1);

        else if(num[j] < num[i] && now[j])

        add(j ,nowid ,1);

        else add(i ,nowid ,1) ,add(j ,nowid ,1);

        add(nowid ,n + n * (n - 1) / 2 + 1 ,1);

    }

    int flow = DINIC(0 ,n + n * (n - 1) / 2 + 1 ,n + n * (n - 1) / 2 + 1);

    if(flow == n * (n - 1) / 2)

    {

        int s = 0;

        for(int i = 1 ;i <= n ;i ++)

        s += now[i];

        if(Ans < s) Ans = s;

    }

    return ;

}

void DFS(int nowid)

{

    if(nowid == maxn + 1)

    {

        Flow();

        return;

    }

    now[nowid] = 0;

    DFS(nowid + 1);

    now[nowid] = 1;

    DFS(nowid + 1);

}

int main ()

{

    int t ,i ,nowid;

    char str[30];

    scanf("%d" ,&t);

    getchar();

    while(t--)

    {

        gets(str);

        int len = strlen(str);

        nowid = 0;

        for(i = 0 ;i < len ;i ++)

        {

            if(str[i] >= '0' && str[i] <= '9')

            {

                if(i == len - 1 || str[i+1] < '0' || str[i+1] > '9')

                num[++nowid] = str[i] - '0';

                else

                {

                    num[++nowid] = (str[i] - '0') * 10 + str[i+1] - '0';

                    i ++;

                }

            }

        }

        n = nowid;

        for(i = n ;i >= 1 ;i --)

        {

            if(num[i] == num[n])

            {

                maxn = i;

                now[i] = 1;

            }

            else now[i] = 0;

        }

        if(maxn == 1)

        {

            printf("%d\n" ,n);

            continue;

        }

        Ans = 0;

        DFS(1);

        printf("%d\n" ,Ans);

    }

    return 0;

}

枚举后几位+DINIC

#include<queue>

#include<stdio.h>

#include<string.h>

#define N_node 150

#define N_edge 5000

#define INF 1000000000

using namespace std;

typedef struct

{

    int to ,cost ,next;

}STAR;

typedef struct

{

    int x ,t;

}DEP;

STAR E[N_edge];

DEP xin ,tou;

int list[N_node] ,listt[N_node] ,tot;

int deep[N_node];

int num[15];

void add(int a ,int b ,int c)

{

    E[++tot].to = b;

    E[tot].cost = c;

    E[tot].next = list[a];

    list[a] = tot;

    E[++tot].to = a;

    E[tot].cost = 0;

    E[tot].next = list[b];

    list[b] = tot;

}

int minn(int x ,int y)

{

    return x < y ? x : y;

}

bool BFS_DEEP(int s ,int t ,int n)

{

    memset(deep ,255 ,sizeof(deep));

    xin.x = s ,xin.t = 0;

    queue<DEP>q;

    q.push(xin);

    deep[s] = 0;

    while(!q.empty())

    {

        tou = q.front();

        q.pop();

        for(int k = list[tou.x] ;k ;k = E[k].next)

        {

            xin.x = E[k].to;

            xin.t = tou.t + 1;

            if(deep[xin.x] != -1 || !E[k].cost)

            continue;

            deep[xin.x] = xin.t;

            q.push(xin);

        }

    }

    for(int i = 0 ;i <= n ;i ++)

    listt[i] = list[i];

    return deep[t] != -1;

}

int DFS_Flow(int s ,int t ,int flow)

{

    if(s == t) return flow;

    int nowflow = 0;

    for(int k = listt[s] ;k ;k = E[k].next)

    {

        listt[s] = k;

        int c = E[k].cost;

        int to = E[k].to;

        if(!c || deep[to] != deep[s] + 1)

        continue;

        int tmp = DFS_Flow(to ,t ,minn(c ,flow - nowflow));

        nowflow += tmp;

        E[k].cost -= tmp;

        E[k^1].cost += tmp;

        if(nowflow == flow) break;

    }

    if(!nowflow) deep[s] = 0;

    return nowflow;

}

int DINIC(int s ,int t ,int n)

{

    int ans = 0;

    while(BFS_DEEP(s ,t ,n))

    {

        ans += DFS_Flow(s ,t ,INF);

    }

    return ans;

}

int Flow(int now ,int n)

{

    memset(list ,0 ,sizeof(list));

    tot = 1;

    int s = 0 ,t = n + n * (n - 1) / 2 + 1;

    for(int i = 1 ;i <= n ;i ++)

    add(s ,i ,num[i]);

    int nowid = n;

    for(int i = 1 ;i <= n ;i ++)

    for(int j = i + 1 ;j <= n ;j ++)

    {

        ++nowid;

        if(num[i] < num[j] && i >= now)

        add(i ,nowid ,1);

        else if(num[j] < num[i] && j >= now)

        add(j ,nowid ,1);

        else add(i ,nowid ,1) ,add(j ,nowid ,1);

        add(nowid ,t ,1);

    }

    return DINIC(s ,t ,t);

}

int main ()

{

    int i ,nowid ,t;

    char str[30];

    scanf("%d" ,&t);

    getchar();

    while(t--)

    {

        gets(str);

        int len = strlen(str);

        nowid = 0;

        for(i = 0 ;i < len ;i ++)

        {

            if(str[i] >= '0' && str[i] <= '9')

            {

                if(i == len - 1 || str[i+1] < '0' || str[i + 1] > '9')

                num[++nowid] = str[i] - '0';

                else

                {

                    num[++nowid] = (str[i] - '0') * 10 + str[i+1] - '0';

                    i ++;

                }

            }

        }

        int list;

        for(i = nowid ;i >= 1 ;i --)

        if(num[i] == num[nowid]) list = i;

        if(list == 1)

        {

            printf("%d\n" ,nowid);

            continue;

        }

        for(i = 1 ;i <= nowid ;i ++)

        {

            if(Flow(i ,nowid) == nowid * (nowid - 1) / 2)

            break;

        }

        printf("%d\n" ,nowid - i + 1);

    }

    return 0;

}

POJ2669不错的最大流 竞赛问题(枚举King的个数)的更多相关文章

  1. POJ 3185 The Water Bowls(高斯消元-枚举变元个数)

    题目链接:http://poj.org/problem?id=3185 题意:20盏灯排成一排.操作第i盏灯的时候,i-1和i+1盏灯的状态均会改变.给定初始状态,问最少操作多少盏灯使得所有灯的状态最 ...

  2. Educational Codeforces Round 35 B. Two Cakes【枚举/给盘子个数,两份蛋糕块数,最少需要在每个盘子放几块蛋糕保证所有蛋糕块都装下】

    B. Two Cakes time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  3. hdu 2058 枚举区间和个数

    题意:       给你两个数n,m,意思是有一个序列长度n,他是1 2 3 4 ...n,然后让你输出所有连续和等于m的范围. 思路:       是个小水题,随便写几个数字就能发现规律了,我们可以 ...

  4. POJ2699 The Maximum Number of Strong Kings

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2102   Accepted: 975 Description A tour ...

  5. POJ 2699 The Maximum Number of Strong Kings (最大流+枚举)

    http://poj.org/problem?id=2699 题意: 一场联赛可以表示成一个完全图,点表示参赛选手,任意两点u, v之间有且仅有一条有向边(u, v)或( v, u),表示u打败v或v ...

  6. BNU 33693——Problemsetting——————【枚举+最大流】

    Problemsetting Time Limit: 5000ms Memory Limit: 131072KB 64-bit integer IO format: %lld      Java cl ...

  7. poj 2699 The Maximum Number of Strong Kings 枚举 最大流

    题目链接 题意 对于一个竞赛图(有向完全图),其顶点是选手,边是比赛,边\(e=(u,v)\)代表该场比赛中\(u\)战胜\(v\). 现定义选手的分数为其战胜的人的个数(即竞赛图中点的出度).并且定 ...

  8. Spring Cloud Alibaba:Sentinel实现熔断与限流

    一.什么是Sentinel Sentinel,中文翻译为哨兵,是为微服务提供流量控制.熔断降级的功能,它和Hystrix提供的功能一样,可以有效的解决微服务调用产生的“雪崩效应”,为微服务系统提供了稳 ...

  9. 最大流 总结&&做题记录

    最近一直很忙,为了节省时间,从今以后的题解会 一个专题 写一篇. 刷了一些题后,有了以下总结: 模型要点: 1.构造流量平衡,在满足流量平衡的情况下,找到要让什么最大. 2.一般用于判断性问题,即所有 ...

随机推荐

  1. vscode动态调试

    前言: 关于vscode动态调试php项目其实在网上有文章,但那些文章或多或少都有些坑点或者转载他人,未经验证过,几度重装系统重新配置的时候在网上看文章配置总是有点问题,所以这次自己写了一篇文章,从头 ...

  2. CMU数据库(15-445)Lab3- QUERY EXECUTION

    Lab3 - QUERY EXECUTION 实验三是添加对在数据库系统中执行查询的支持.您将实现负责获取查询计划节点并执行它们的executor.您将创建执行下列操作的executor Access ...

  3. 前端学习 node 快速入门 系列 —— npm

    其他章节请看: 前端学习 node 快速入门 系列 npm npm 是什么 npm 是 node 的包管理器,绝大多数 javascript 相关的包都放在 npm 上. 所谓包,就是别人提供出来供他 ...

  4. 微服务网关Zuul过滤器Filter

    Zuul本质 Zuul是一个网关,关于网关的介绍参考:亿级流量架构之网关设计思路.常见网关对比, 可知Zuul是一个业务网关, 而深入了解Zuul, 基本就是一系列过滤器的集合: Zuul的过滤器 下 ...

  5. Vite2+Electron仿抖音|vite2.x+electron12+vant3短视频|直播|聊天

    整合vite2+electron12跨平台仿抖音电脑版实战Vite2-ElectronDouYin. 基于vite2.0+electron12+vant3+swiper6+v3popup等技术跨端仿制 ...

  6. 【linux】驱动-3-字符设备驱动

    目录 前言 3. 字符设备驱动 3.1 Linux设备分类 3.2 设备相关概念 3.2.1 设备号 3.2.2 设备节点 3.2.3 APP open 文件理解 ** 3.3 数据结构 3.3.1 ...

  7. Nacos概述及安装

    Nacos是什么? 在Spring Cloud中我们使用eureka.consul等做为服务注册中心,使用Spring Cloud Config做为配置中心.而Spring Cloud中,也可以使用n ...

  8. 如何获取canvas当前的缩放值

    项目中一直有一个问题困扰着我,我们的画布可以缩放平移旋转,支持拖拽生成图形,生成手写笔迹,如果用户选择的线条粗细为5像素,那么即使画布缩放过绘制出的线条粗细也应该是视觉上的5px,所以再绘制时赋值给c ...

  9. 【C/C++】memset方法的误区

    目录 一.前言 二.函数作用 三.效率对比 四.误区总结 1.按字节设置 2.设置的值只有最低字节有效 3.堆内存不可直接 sizeof 取首地址 4.传参数组不可直接 sizeof 取首地址 一.前 ...

  10. (五)Struts2处理结果管理

    当Action处理完用户请求时,处理结果应该通过视图资源实现,但将哪个视图呈现给浏览者呢.由<result.../>来决定 Action处理完用户请求后,返回一个普通字符串.整个普通字符串 ...