From 75e7b77c2b6083a3dbc7b596302a0f37c5d4544e Mon Sep 17 00:00:00 2001 From: Philippe Zwietering Date: Tue, 19 Apr 2022 15:22:25 +0200 Subject: [PATCH] Finished beads and milk2 for usaco --- usaco/ch1/beads.cpp | 93 ++++++++++++++++++++++++++++++++++++++++++++- usaco/ch1/milk2.cpp | 73 +++++++++++++++++++++++++++++++++++ 2 files changed, 165 insertions(+), 1 deletion(-) create mode 100644 usaco/ch1/milk2.cpp diff --git a/usaco/ch1/beads.cpp b/usaco/ch1/beads.cpp index 444d581..f2a19ec 100644 --- a/usaco/ch1/beads.cpp +++ b/usaco/ch1/beads.cpp @@ -7,9 +7,12 @@ LANG: C++ #include #include #include +#include using namespace std; +enum Colour{ red, blue, white }; + int main() { ofstream fout ("beads.out"); ifstream fin ("beads.in"); @@ -19,7 +22,95 @@ int main() { fin >> n >> s; - + string doubleNecklace = s.append(s); + + Colour lastBead = white, prewhiteBead = white; + int currentLength = 0, redLength = 0, blueLength = 0, longestChain = 0, preWhiteLength = 0, inbetweenWhiteLength = 0; + + // The most intricate scenario is the following scenario: + // white - red - white - blue - white + // or equivalently: + // white - blue - white - red - white + + // When white beads are enclosed by beads of the same colour we regard the beads as those colour + + + for(const char& bead : doubleNecklace){ + if(bead == 'w'){ + if(lastBead == white){ + currentLength++; + } else if(lastBead == red){ + prewhiteBead = red; + redLength = currentLength; + currentLength = 1; + } else{ // Last bead is blue + prewhiteBead = blue; + blueLength = currentLength; + currentLength = 1; + } + longestChain = max(longestChain, currentLength + redLength + blueLength + prewhiteBead + inbetweenWhiteLength); + lastBead = white; + + } else if(bead == 'r'){ + if(lastBead == red){ + currentLength++; + longestChain = max(longestChain, currentLength + preWhiteLength + blueLength + inbetweenWhiteLength); + } else if(lastBead == blue){ + longestChain = max(longestChain, currentLength + redLength + preWhiteLength + inbetweenWhiteLength); + blueLength = currentLength; + redLength = 0; + currentLength = 1; + preWhiteLength = inbetweenWhiteLength; + inbetweenWhiteLength = 0; + } else{ // Last bead is white + if(prewhiteBead == red){ + currentLength += redLength + 1; + redLength = 0; + longestChain = max(longestChain, currentLength + preWhiteLength + blueLength + inbetweenWhiteLength); + } else{ + longestChain = max(longestChain, currentLength + blueLength + redLength + preWhiteLength + inbetweenWhiteLength); + preWhiteLength = inbetweenWhiteLength; + inbetweenWhiteLength = currentLength; + redLength = 0; + currentLength = 1; + } + } + lastBead = red; + + } else if(bead == 'b'){ + if(lastBead == blue){ + currentLength++; + longestChain = max(longestChain, currentLength + redLength + preWhiteLength + inbetweenWhiteLength); + } else if(lastBead == red){ + longestChain = max(longestChain, currentLength + blueLength + preWhiteLength + inbetweenWhiteLength); + redLength = currentLength; + blueLength = 0; + currentLength = 1; + preWhiteLength = inbetweenWhiteLength; + inbetweenWhiteLength = 0; + } else{ // Last bead is white + if(prewhiteBead == blue){ + currentLength += blueLength + 1; + blueLength = 0; + longestChain = max(longestChain, currentLength + redLength + preWhiteLength + inbetweenWhiteLength); + } else{ + longestChain = max(longestChain, currentLength + blueLength + redLength + preWhiteLength + inbetweenWhiteLength); + preWhiteLength = inbetweenWhiteLength; + inbetweenWhiteLength = currentLength; + blueLength = 0; + currentLength = 1; + } + } + lastBead = blue; + + } else{ + cout << "unexpected input" << endl; + return 0; + } + // cout << bead << " CurrentLength: " << currentLength << " RedLength: " << redLength << " BlueLength: " << blueLength << " Prewhitelength: " << preWhiteLength << " Inbetweenwhite: " << inbetweenWhiteLength << " LongestChain: " << longestChain << endl; + } + + fout << min(longestChain, int(s.size()) / 2) << endl; return 0; } \ No newline at end of file diff --git a/usaco/ch1/milk2.cpp b/usaco/ch1/milk2.cpp new file mode 100644 index 0000000..cfc0513 --- /dev/null +++ b/usaco/ch1/milk2.cpp @@ -0,0 +1,73 @@ +/* +ID: philipp17 +PROG: milk2 +LANG: C++ +*/ +/* LANG can be C++11 or C++14 for those more recent releases */ +#include +#include +#include +#include +#include +#include + +using namespace std; + +bool sorter(const pair& i, const pair& j){ + if(i.first != j.first){ + return (i.first < j.first); + } + + return (i.second > j.second); +} + +int main() { + ofstream fout ("milk2.out"); + ifstream fin ("milk2.in"); + + int n; + fin >> n; + + vector> events(2 * n); + for(int i = 0; i < n; ++i){ + int start, end; + fin >> start >> end; + + events[2 * i] = {start, true}; + events[2 * i+1] = {end, false}; + } + + sort(events.begin(), events.end(), sorter); + + int longestMilking = 0, longestIdle = 0, milkers = 0, startMilking = -1, endMilking = -1; + + for(auto e : events){ + if(e.second){ // So it is time to milk + if(milkers == 0 && endMilking != -1){ + longestIdle = max(longestIdle, e.first - endMilking); + } if(milkers == 0){ + startMilking = e.first; + } + milkers++; + } + + else{ // So milking is stopped somewhere + milkers--; + + if(milkers == 0 && startMilking != -1){ + longestMilking = max(longestMilking, e.first - startMilking); + } if(milkers == 0){ + endMilking = e.first; + } + } + // cout << e.second << " " << e.first << " " << milkers << " " << startMilking << " " << endMilking << " " << longestMilking << " " << longestIdle << endl; + } + + fout << longestMilking << " " << longestIdle << endl; + + // for(auto e : events){ + // cout << e.first << " " << e.second << endl; + // } + + return 0; +} \ No newline at end of file