/* ID: garakch1 LANG: C++ PROB: milk2 */ #include <cstdio> #include <vector> #include <algorithm> bool cmp(const std::pair<int, int> &left, const std::pair<int, int> &right) { return left.first < right.first; } int main() { freopen("milk2.in", "r", stdin); freopen("milk2.out", "w", stdout); std::vector< std::pair<int, int> > v; int n, a, b; scanf("%d", &n); for (int i=0; i<n; i++) { scanf("%d%d", &a, &b); v.push_back(std::make_pair(a, b)); } sort(v.begin(), v.end(), cmp); //for (int i=0; i<n; i++) printf("%d %d\n", v[i].first, v[i].second); int left=v[0].first, right=v[0].second, idle=0, cont; cont=right-left; for(int i=0; i<n; i++) { if(v[i].first>right) idle=std::max(v[i].first-right, idle), left=v[i].first, right=v[i].second; else if(v[i].second>right) right=v[i].second, cont=std::max(right-left, cont); } printf("%d %d\n", cont, idle); return 0; }
Showing posts with label usaco. Show all posts
Showing posts with label usaco. Show all posts
Wednesday, January 31, 2018
usaco Milking Cows solution
usaco Milking Cows solution
Sunday, August 10, 2014
usaco Broken Necklace solution
usaco Broken Necklace:
You have a necklace of N red, white, or blue beads (3<=N<=350) some of which are red, others blue, and others white, arranged at random. Here are two examples for n=29:
The configuration in Figure A may be represented as a string of b's and r's, where b represents a blue bead and r represents a red one, as follows: brbrrrbbbrrrrrbrrbbrbbbbrrrrb .
Suppose you are to break the necklace at some point, lay it out straight, and then collect beads of the same color from one end until you reach a bead of a different color, and do the same for the other end (which might not be of the same color as the beads collected before this).
Determine the point where the necklace should be broken so that the most number of beads can be collected.
In some necklaces, white beads had been included as shown in Figure B above. When collecting beads, a white bead that is encountered may be treated as either red or blue and then painted with the desired color. The string that represents this configuration can include any of the three symbols r, b and w.
Write a program to determine the largest number of beads that can be collected from a supplied necklace.
Broken Necklace
1 2 1 2
r b b r b r r b
r b b b
r r b r
r r w r
b r w w
b b r r
b b b b
b b r b
r r b r
b r r r
b r r r
r r r b
r b r r r w
Figure A Figure B
r red bead
b blue bead
w white bead
The beads considered first and second in the text that follows have been marked in the picture.The configuration in Figure A may be represented as a string of b's and r's, where b represents a blue bead and r represents a red one, as follows: brbrrrbbbrrrrrbrrbbrbbbbrrrrb .
Suppose you are to break the necklace at some point, lay it out straight, and then collect beads of the same color from one end until you reach a bead of a different color, and do the same for the other end (which might not be of the same color as the beads collected before this).
Determine the point where the necklace should be broken so that the most number of beads can be collected.
Example
For example, for the necklace in Figure A, 8 beads can be collected, with the breaking point either between bead 9 and bead 10 or else between bead 24 and bead 25.In some necklaces, white beads had been included as shown in Figure B above. When collecting beads, a white bead that is encountered may be treated as either red or blue and then painted with the desired color. The string that represents this configuration can include any of the three symbols r, b and w.
Write a program to determine the largest number of beads that can be collected from a supplied necklace.
PROGRAM NAME: beads
INPUT FORMAT
| Line 1: | N, the number of beads |
| Line 2: | a string of N characters, each of which is r, b, or w |
SAMPLE INPUT (file beads.in)
29 wwwbbrwrbrbrrbrbrwrwwrbwrwrrb
OUTPUT FORMAT
A single line containing the maximum of number of beads that can be collected from the supplied necklace.SAMPLE OUTPUT (file beads.out)
11
OUTPUT EXPLANATION
Consider two copies of the beads (kind of like being able to runaround the ends). The string of 11 is marked. Two necklace copies joined here
v
wwwbbrwrbrbrrbrbrwrwwrbwrwrrb|wwwbbrwrbrbrrbrbrwrwwrbwrwrrb
******|*****
rrrrrb bbbbb <-- assignments
rrrrr#bbbbbb
5 x r 6 x b <-- 11 total
usaco Broken Necklace solution: http://ideone.com/zdNlhI
/* ID: [your ID here] LANG: C++ PROB: beads */ #include <iostream> #include <cstdio> #include <algorithm> int n; char s[355]; int md(int j) { while(j<0) j+=n; return j%n; } int bp(int pos, int dir) { int i, j; char col='w'; if(dir==1) j=pos; else j=pos-1; for(i=0; i<n; i++, j=md(j+dir)) { if(col=='w' && s[j]!='w') col=s[j]; if(col!='w' && s[j]!='w' && s[j]!=col) break; } return i; } int main() { freopen("beads.in", "r", stdin); freopen("beads.out", "w", stdout); int ma=0; scanf("%d", &n); scanf("%s", s); for(int i=0; i<n; i++) ma=std::max(bp(i, -1)+bp(i, 1), ma); if(ma>n) ma=n; printf("%d\n", ma); return 0; }
alternative solutions for usaco Broken Necklace: http://usacotraining.blogspot.com.tr/2013/09/problem-114-broken-necklace.html
Tuesday, July 8, 2014
usaco Friday the Thirteenth solution
usaco Friday the Thirteenth:
note: dont forget to add your usacogate ID, PROG, LANG comment at the beginning of the code. and freopen() stuff in main() part.
usaco Friday the Thirteenth solution: http://ideone.com/ClCFT6
#include <iostream> #include <cstdio> using namespace std; int main() { int n, l=1, w[10]={0}; scanf("%d", &n); for(int i=1900; i<1900+n; i++) { for(int j=0; j<12; j++) { if(j==3 || j==5 || j==8 || j==10) { for(int k=0; k<30; k++) { l++, l%=7; if(k==12) w[l]++; } } else if(j!=1) { for(int k=0; k<31; k++) { l++, l%=7; if(k==12) w[l]++; } } else { if(i%400==0 || (i%100!=0 && i%4==0)) { for(int k=0; k<29; k++) { l++, l%=7; if(k==12) w[l]++; } } else { for(int k=0; k<28; k++) { l++, l%=7; if(k==12) w[l]++; } } } } } for(int i=0; i<6; i++) printf("%d ", w[i]); printf("%d\n", w[6]); return 0; }
note: dont forget to add your usacogate ID, PROG, LANG comment at the beginning of the code. and freopen() stuff in main() part.
Sunday, July 6, 2014
usaco Greedy gift givers solution
usaco Greedy gift givers:
note: function find() belongs to <algorithm> C++ library, and it helps you to find position/index of sepcific thing in C++ vectors. for ref: http://stackoverflow.com/a/15099748/2948746
usaco Greedy gift givers solution: http://ideone.com/49AVHb
/* ID: [your usacogate ID here] PROG: gift1 LANG: C++ */ #include <iostream> #include <cstdio> #include <vector> #include <algorithm> using namespace std; int main() { freopen("gift1.in", "r", stdin); freopen("gift1.out", "w", stdout); int n, a, b, k=0, acc[15]={0}, idx, tmp; vector <string> v; string s; scanf("%d", &n); for(int i=0; i<n; i++) cin>>s, v.push_back(s); for(int i=0; i<n; i++) { cin>>s; scanf("%d%d", &a, &b); idx=find(v.begin(), v.end(), s)-v.begin(); tmp=idx; for(int j=0; j<b; j++) { cin>>s; idx=find(v.begin(), v.end(), s)-v.begin(); acc[idx]+=a/b, acc[tmp]-=a/b; } } for(vector <string>::iterator i=v.begin(); i!=v.end(); i++) { cout<<(*i)<<" "<<acc[k]<<endl; k++; } return 0; }
note: function find() belongs to <algorithm> C++ library, and it helps you to find position/index of sepcific thing in C++ vectors. for ref: http://stackoverflow.com/a/15099748/2948746
usaco Your ride is here solution
usaco Your ride is here:
note: just comment freopen() to test it in ideone
usaco Your ride is here solution: http://ideone.com/GmGx8J
/* ID: [your usacogate ID here] PROG: ride LANG: C++ */ #include <iostream> #include <cstdio> #include <cstring> using namespace std; int main() { freopen("ride.in", "r", stdin); freopen("ride.out", "w", stdout); char a[10], b[10]; int c=1, d=1; scanf("%s\n%s", a, b); for(int i=0; i<strlen(a); i++) c*=a[i]-'A'+1; for(int i=0; i<strlen(b); i++) d*=b[i]-'A'+1; c%=47, d%=47; if(c==d) printf("GO\n"); else printf("STAY\n"); return 0; }
note: just comment freopen() to test it in ideone
usaco Submitting solutions - Sum the numbers solution
usaco TEXT Submitting solutions - Sum the numbers:
usaco TEXT Submitting solutions - Sum the numbers solution:
/* ID: [your usacogate ID here] PROG: test LANG: C++ */ #include <iostream> #include <cstdio> #include <fstream> using namespace std; int main() { freopen("test.in", "r", stdin); freopen("test.out", "w", stdout); int a, b; scanf("%d%d", &a, &b); printf("%d\n", a+b); return 0; }
Subscribe to:
Posts (Atom)