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.
2 4 5004 5005 5005 5006 5004 5006 5004 5006 7 4 5 2 3 1 2 2 2 4 4 2 3 3 4
3 2 3 4 5 1 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 long
using 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");elseprintf(" ");}}return 0;
}