Editing
2015 Dec Bronze Problem 3 Contaminated Milk
(section)
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Code == === C++ === <pre> #include <iostream> #include <fstream> #include <vector> using namespace std; vector<int> personDrink; vector<int> milkDrink; vector<int> timeDrink; vector<int> personSick; vector<int> timeSick; void initializeDrinkArrays(int d, ifstream &fin) { personDrink.resize(d); milkDrink.resize(d); timeDrink.resize(d); for(int i = 0; i < d; i++) { fin >> personDrink[i] >> milkDrink[i] >> timeDrink[i]; } } void initializeSickArrays(int s, ifstream &fin) { personSick.resize(s); timeSick.resize(s); for(int i = 0; i < s; i++) { fin >> personSick[i] >> timeSick[i]; } } bool isMilkBad(int milkType) { for(int i = 0; i < personSick.size(); i++) { bool personDrank = false; for(int j = 0; j < personDrink.size(); j++) { if(personDrink[j] == personSick[i] && milkDrink[j] == milkType && timeDrink[j] < timeSick[i]) { personDrank = true; break; } } if(!personDrank) { return false; } } return true; } int countPeopleWhoDrank(int milkType) { vector<bool> didDrink(51, false); for(int i = 0; i < personDrink.size(); i++) { if(milkDrink[i] == milkType) { didDrink[personDrink[i]] = true; } } int drinkCount = 0; for(bool drank : didDrink) { if(drank) { drinkCount++; } } return drinkCount; } int main() { ifstream fin("badmilk.in"); ofstream fout("badmilk.out"); int n, m, d, s; fin >> n >> m >> d >> s; initializeDrinkArrays(d, fin); initializeSickArrays(s, fin); int maxSick = 0; for(int i = 1; i <= m; i++) { if(isMilkBad(i)) { int sickCount = countPeopleWhoDrank(i); maxSick = max(maxSick, sickCount); } } fout << maxSick << endl; return 0; } </pre> === Java === <pre> import java.io.*; import java.util.*; public class badmilk { private static int[] personDrink; private static int[] milkDrink; private static int[] timeDrink; private static int[] personSick; private static int[] timeSick; public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new FileReader("badmilk.in")); PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("badmilk.out"))); StringTokenizer st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); int m = Integer.parseInt(st.nextToken()); int d = Integer.parseInt(st.nextToken()); int s = Integer.parseInt(st.nextToken()); initializeDrinkArrays(d, br); initializeSickArrays(s, br); int maxSick = 0; for(int i = 1; i <= m; i++) { if(isMilkBad(i)) { int sickCount = countPeopleWhoDrank(i); maxSick = Math.max(maxSick, sickCount); } } pw.println(maxSick); pw.close(); } private static void initializeDrinkArrays(int d, BufferedReader br) throws IOException { personDrink = new int[d]; milkDrink = new int[d]; timeDrink = new int[d]; for(int i = 0; i < d; i++) { StringTokenizer st = new StringTokenizer(br.readLine()); personDrink[i] = Integer.parseInt(st.nextToken()); milkDrink[i] = Integer.parseInt(st.nextToken()); timeDrink[i] = Integer.parseInt(st.nextToken()); } } private static void initializeSickArrays(int s, BufferedReader br) throws IOException { personSick = new int[s]; timeSick = new int[s]; for(int i = 0; i < s; i++) { StringTokenizer st = new StringTokenizer(br.readLine()); personSick[i] = Integer.parseInt(st.nextToken()); timeSick[i] = Integer.parseInt(st.nextToken()); } } private static boolean isMilkBad(int milkType) { for(int i = 0; i < personSick.length; i++) { if(!didPersonDrinkBefore(personSick[i], milkType, timeSick[i])) { return false; } } return true; } private static boolean didPersonDrinkBefore(int person, int milkType, int time) { for(int i = 0; i < personDrink.length; i++) { if(personDrink[i] == person && milkDrink[i] == milkType && timeDrink[i] < time) { return true; } } return false; } private static int countPeopleWhoDrank(int milkType) { boolean[] didDrink = new boolean[51]; for(int i = 0; i < personDrink.length; i++) { if(milkDrink[i] == milkType) { didDrink[personDrink[i]] = true; } } int drinkCount = 0; for(boolean drank : didDrink) { if(drank) { drinkCount++; } } return drinkCount; } } </pre> === Python === <pre> def initialize_drink_arrays(d, lines): global personDrink, milkDrink, timeDrink personDrink = [0]*d milkDrink = [0]*d timeDrink = [0]*d for i in range(d): personDrink[i], milkDrink[i], timeDrink[i] = map(int, lines[i].split()) def initialize_sick_arrays(s, lines): global personSick, timeSick personSick = [0]*s timeSick = [0]*s for i in range(s): personSick[i], timeSick[i] = map(int, lines[i].split()) def is_milk_bad(milkType): for i in range(len(personSick)): personDrank = False for j in range(len(personDrink)): if (personDrink[j] == personSick[i] and milkDrink[j] == milkType and timeDrink[j] < timeSick[i]): personDrank = True break if not personDrank: return False return True def count_people_who_drank(milkType): didDrink = [False]*51 for i in range(len(personDrink)): if milkDrink[i] == milkType: didDrink[personDrink[i]] = True drinkCount = sum(didDrink) return drinkCount def main(): with open("badmilk.in", "r") as fin: lines = fin.readlines() n, m, d, s = map(int, lines[0].split()) initialize_drink_arrays(d, lines[1:d+1]) initialize_sick_arrays(s, lines[d+1:]) maxSick = 0 for i in range(1, m+1): if is_milk_bad(i): sickCount = count_people_who_drank(i) maxSick = max(maxSick, sickCount) with open("badmilk.out", "w") as fout: fout.write(str(maxSick)+"\n") if __name__ == "__main__": main() </pre> [[Category:Yearly_2015_2016]] [[Category:Bronze]] [[Category:Arrays]] [[Category:Sets]] [[Category:Simulation]] [[Category:Brute Force]]
Summary:
Please note that all contributions to Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
My wiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Tools
What links here
Related changes
Special pages
Page information