Finished beads and milk2 for usaco
This commit is contained in:
@@ -7,9 +7,12 @@ LANG: C++
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <algorithm>
|
||||
|
||||
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;
|
||||
}
|
||||
73
usaco/ch1/milk2.cpp
Normal file
73
usaco/ch1/milk2.cpp
Normal file
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
ID: philipp17
|
||||
PROG: milk2
|
||||
LANG: C++
|
||||
*/
|
||||
/* LANG can be C++11 or C++14 for those more recent releases */
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <utility>
|
||||
#include <algorithm>
|
||||
|
||||
using namespace std;
|
||||
|
||||
bool sorter(const pair<int, bool>& i, const pair<int, bool>& 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<pair<int, bool>> 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;
|
||||
}
|
||||
Reference in New Issue
Block a user