NC24155. [USACO 2015 Dec B]Contaminated Milk
描述
Farmer John, known far and wide for the quality of the milk produced on his farm, is hosting a milk-tasting party for NN of his best friends (1≤N≤501≤N≤50). Unfortunately, of the MM types of milk featured at the party (1≤M≤501≤M≤50), exactly one of them has gone bad, but Farmer John does not know which one! Anyone who drinks the bad milk will later become sick, either during the remainder of the party or afterward.
You are given a transcript of the party -- who drinks what when, and also who gets sick when. Based on this information, you can deduce which of the milks could possibly be the bad one. Using this knowledge, help Farmer John determine the minimum number of doses of medicine he will need to obtain in order to guarantee that he can cure all of the individuals who become sick, either during or after the party.
输入描述
The first line of the input contains integers N, M,D, and S.
The next D lines (1≤D≤1000) each contain three integers p,m,tp,m,t, indicating that person p drank milk m at time t. The value of pp is in the range 1…N, m is in the range 1…M, and tt is in the range 1…100. A person may drink the same milk several times, and may also drink several types of milk at the same point in time.
The next S lines (1≤S≤N) each contain two integers p,tp,t, indicating that person p gets sick at time t. The value of pp is in the range 1…N, and tt is in the range 1…100. Each person gets sick at most once, and they only get sick because they drank the bad milk at some strictly earlier point in time.
输出描述
A single integer, specifying the minimum number of doses of medicine Farmer John needs to obtain so that he can guarantee that he will have sufficiently many doses to treat all the people who become sick, both during and after the party.
示例1
输入:
3 4 7 2 1 1 1 1 4 1 1 3 4 1 2 2 3 1 3 2 1 5 2 2 7 1 3 2 8
输出:
3
说明:
There are 3 people and 4 milk types. Person 1 gets sick at time 3 and person 2 gets sick at time 8. Person 3 does not get sick at the party, although we may still need to consider the possibility that he could become sick later, after the party ends. Let's consider the milk types one by one to see which ones could be contaminated; we know a milk type is potentially bad if everyone who became sick drank that milk type before becoming sick.C++11(clang++ 3.9) 解法, 执行用时: 3ms, 内存消耗: 504K, 提交时间: 2020-09-19 13:23:49
#include<bits/stdc++.h> const int maxn=1e3+10; using namespace std; int n,m,k,t,d,s,he[maxn],he1[maxn],ma; vector<pair<int,int> >person[maxn]; set<int>pk[maxn]; int main() { int i,j; scanf("%d%d%d%d",&n,&m,&d,&s); while(d--) { int p,m,t; scanf("%d%d%d",&p,&m,&t); if(pk[p].find(m)==pk[p].end()) pk[p].insert(m),he[m]++; person[p].push_back(make_pair(m,t)); } for(i=1;i<=s;i++) { int p,m; set<int>ca; scanf("%d%d",&p,&m); for(auto q:person[p]) { if(q.second<m&&ca.find(q.first)==ca.end()) he1[q.first]++,ca.insert(q.first); } } for(i=1;i<=m;i++) { if(he1[i]==s) ma=max(ma,he[i]); } printf("%d\n",ma); return 0; }