HDU 3729 I'm Telling the Truth(二分图匹配+匈牙利算法+变形)
来源:互联网 发布:淘宝排名突然下降 编辑:程序博客网 时间:2024/05/18 18:19
After this year’s college-entrance exam, the teacher did a survey in his class on students’ score. There are n students in the class. The students didn’t want to tell their teacher their exact score; they only told their teacher their rank in the province (in the form of intervals).
After asking all the students, the teacher found that some students didn’t tell the truth. For example, Student1 said he was between 5004th and 5005th, Student2 said he was between 5005th and 5006th, Student3 said he was between 5004th and 5006th, Student4 said he was between 5004th and 5006th, too. This situation is obviously impossible. So at least one told a lie. Because the teacher thinks most of his students are honest, he wants to know how many students told the truth at most.
After asking all the students, the teacher found that some students didn’t tell the truth. For example, Student1 said he was between 5004th and 5005th, Student2 said he was between 5005th and 5006th, Student3 said he was between 5004th and 5006th, Student4 said he was between 5004th and 5006th, too. This situation is obviously impossible. So at least one told a lie. Because the teacher thinks most of his students are honest, he wants to know how many students told the truth at most.
245004 50055005 50065004 50065004 500674 52 31 22 24 42 33 4
32 3 451 3 5 6 7
题解:
题意:
有不超过60的学生,每个学生都不想说出自己的成绩,只说了自己的排名范围,让你找至少多少个人说的是真话,然后按照字典序最大输出说真话人的名字
思路:
这是我第一次没看题解做出来稍微复杂一点的匈牙利算法,也就感觉变了下形,也就是人匹配排名的问题,匈牙利算法搞的去,因为这里的排名数据范围比较大,我们就不用邻接矩阵了,改用了邻接表,然后就快了很多,至于字典序最大只要从后往前匹配就好了,后来搜了下题解的代码,比我的代码慢了一倍hhhhhh
代码:
#include<algorithm>#include<iostream>#include<cstring>#include<stdio.h>#include<math.h>#include<string>#include<stdio.h>#include<queue>#include<stack>#include<map>#include<deque>#define M (t[k].l+t[k].r)/2#define lson k*2#define rson k*2+1#define ll long longusing namespace std;int num[61];//存每个同学对应可能有几个排名int p[61][100001];//邻接表,存每个同学对应的排名是哪几个int vis[100001];//遍历数组int used[100001];//存排名i匹配的人int find(int u)//改进的匈牙利算法{ int i; for(i=0;i<num[u];i++)//这里就只在那几个人里找不用全程扫一遍了 { if(!vis[p[u][i]]) { vis[p[u][i]]=1; if(!used[p[u][i]]||find(used[p[u][i]])) { used[p[u][i]]=u; return 1; } } } return 0;}int main(){ int i,j,k,n,test,x,y; scanf("%d",&test); while(test--) { scanf("%d",&n); memset(num,0,sizeof(num)); memset(used,0,sizeof(used)); stack<int>q;//用堆,因为先进后出 for(i=1;i<=n;i++) { scanf("%d%d",&x,&y); for(j=x;j<=y;j++) { p[i][num[i]]=j;//邻接表存数据 num[i]++; } } int ans=0; for(i=n;i>=1;i--)//从后往前 { memset(vis,0,sizeof(vis)); if(find(i)) { q.push(i); ans++; } } printf("%d\n",ans); while(!q.empty())//从堆里面取 { printf("%d",q.top()); q.pop(); if(q.empty()) printf("\n"); else printf(" "); } } return 0;}
阅读全文
0 0
- HDU 3729 I'm Telling the Truth(二分图匹配+匈牙利算法+变形)
- hdu 3729 I'm Telling the Truth(二分匹配_ 匈牙利算法)
- hdu 3729 I'm Telling the Truth 二分图匹配
- hdu 3729 I'm Telling the Truth(二分图匹配)
- HDU3729 I'm Telling the Truth 匈牙利算法的二分图匹配
- hdu 3729 I'm Telling the Truth【二分匹配】
- hdu 3729 I'm Telling the Truth(二分图最大匹配)
- hdu 3729 I'm Telling the Truth (二分图匹配)
- HDU 3729 I'm Telling the Truth(二分图最大匹配+结果输出)
- hdu 3729 I'm Telling the Truth(二分图最大匹配,字典序最大输出)
- hdu 3729 I'm Telling the Truth(最大匹配)
- HDU 3729 I'm Telling the Truth -- 二分图最大匹配 输出方案
- 二分图的最大匹配-hdu-3729-I'm Telling the Truth
- 二分图的最大匹配-hdu-3729-I'm Telling the Truth
- HDU 3729 I'm Telling the Truth(二分图最大匹配)
- HDU 3729 I'm Telling the Truth(二分图最大匹配)
- HDU 3729 I'm Telling the Truth(二分图最大匹配)
- hdu 3279 I'm Telling the Truth 二分匹配
- H5面试——HTTP状态码
- leetcode--Sudoku Solver
- Learning to Track at 100 FPS with Deep Regression Networks 论文笔记
- CSS选择器
- 实现iziModal弹窗移动的功能
- HDU 3729 I'm Telling the Truth(二分图匹配+匈牙利算法+变形)
- spring-boot oauth2
- MySQL 线程池内幕
- 简单实现Ajax异步上传文件
- leetcode--Count and Say
- re.findall
- 牛客刷题知识点总结
- Pthread_mutex_t锁
- 数据库Sharding的基本思想和切分策略