题目
https://pintia.cn/problem-sets/15/problems/895
思路
看其他人的题解,都是用了queue,但是明明两个循环更方便一些嘛~
这个提示救了我一命!一开始情况没考虑全面
过程太乱了,就不放上来了(●’◡’●)
代码
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <string>
#include <map>
#define maxx 10010
#define inf 0x3f3f3f3f
using namespace std;
struct node
{
string name;
int no;
int hour;
} people[maxx];
int n, m;
map<string, int> friends;
bool vis[maxx];
int main()
{
cin >> n >> m;
if (n==1)
{
string string1;
int a,b;
cin>>string1>>a>>b;
cout<<string1<<endl;
cout<<"0.0"<<endl;
return 0;
}
for (int i = 1; i <= m; ++i) //从1开始,因为map不匹配的话返回值是0
{
int x;
cin>>x;
while (x--)
{
string s;
cin >> s;
friends[s] = i;
}
}
for (int i = 0; i < n; ++i)
{
cin >> people[i].name >> people[i].no >> people[i].hour;
if (people[i].hour > 60)
people[i].hour = 60;
}
double time = 0;
int wait = people[0].no;
for (int i = 0; i < n; ++i)
{
int f = 0;
if(!vis[i])
{
vis[i] = 1;
cout<<people[i].name<<endl;
time += wait-people[i].no;
wait += people[i].hour;
if (i<n && people[i+1].no> wait)
{
f = 1;
int bre = people[i+1].no - wait;
//time+=bre;
wait+=bre;
}
for (int j = i+1; j < n && f==0; ++j)
{
if (people[j].no > wait)
break;
if(friends[people[i].name] == friends[people[j].name]) //如果后面有人是一个集合里的
{
vis[j] = 1;
cout<<people[j].name<<endl;
time += wait - people[j].no;
wait+=people[j].hour;
}
}
}
}
printf("%.1f\n",time/n);
return 0;
}
总结
- map的匹配要从1开始,一开始总是把people[3]当成一个friends里的