This one was too good a question for me. i did something couldn't pass out. I have joined topcoder for practice - all people out there help me improve.....
Fly Swatter
Problem
What are your chances of hitting a fly with a tennis racquet?
To start with, ignore the racquet's handle. Assume the racquet is a perfect ring, of outer radius R and thickness t (so the inner radius of the ring is R−t).
The ring is covered with horizontal and vertical strings. Each string is a cylinder of radius r. Each string is a chord of the ring (a straight line connecting two points of the circle). There is a gap of length g between neighbouring strings. The strings are symmetric with respect to the center of the racquet i.e. there is a pair of strings whose centers meet at the center of the ring.
The fly is a sphere of radius f. Assume that the racquet is moving in a straight line perpendicular to the plane of the ring. Assume also that the fly's center is inside the outer radius of the racquet and is equally likely to be anywhere within that radius. Any overlap between the fly and the racquet (the ring or a string) counts as a hit.
Input
One line containing an integer N, the number of test cases in the input file.
The next N lines will each contain the numbers f, R, t, r and g separated by exactly one space. Also the numbers will have at most 6 digits after the decimal point.
Output
N lines, each of the form "Case #k: P", where k is the number of the test case and P is the probability of hitting the fly with a piece of the racquet.
Answers with a relative or absolute error of at most 10-6 will be considered correct.
Limits
f, R, t, r and g will be positive and smaller or equal to 10000.
t < style="font-weight:bold;">Nothing to speak man here is what rem wrote:
#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <numeric>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <ctime>
using namespace std;
typedef long long int64;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<double> vd;
#define _CRT_SECURE_NO_WARNINGS
#define For(i,a,b) for (int i(a),_b(b); i <= _b; ++i)
#define Ford(i,a,b) for (int i(a),_b(b); i >= _b; --i)
#define Rep(i,n) for (int i(0),_n(n); i < _n; ++i)
#define Repd(i,n) for (int i((n)-1); i >= 0; --i)
#define Fill(a,c) memset(&a, c, sizeof(a))
#define MP(x, y) make_pair((x), (y))
#define All(v) (v).begin(), (v).end()
template<typename T, typename S> T cast(S s) {
stringstream ss;
ss << s;
T res;
ss >> res;
return res;
}
template<typename T> inline T sqr(T a) { return a*a; }
template<typename T> inline int Size(const T& c) { return (int)c.size(); }
template<typename T> inline void checkMin(T& a, T b) { if (b < a) a = b; }
template<typename T> inline void checkMax(T& a, T b) { if (b > a) a = b; }
const double pi = 2*acos(0.0);
const double eps = 1e-9;
double asin2(double x) {
double res = asin(max(-1.0, min(1.0, x)));
assert(-pi/2 <= res && res <= pi/2);
return res;
}
double inters(double x1, double y1, double x2, double y2, double r) {
assert(x1 <= x2 && y1 <= y2);
assert(sqrt(sqr(x1)+sqr(y1)) <= r);
if (sqrt(sqr(x2)+sqr(y2)) <= r)
return (x2-x1)*(y2-y1);
vector<double> xs;
xs.push_back(x1);
xs.push_back(x2);
double x = sqrt(sqr(r)-sqr(y1));
if (y1 < r && x1 < x && x < x2)
xs.push_back(x);
x = sqrt(sqr(r)-sqr(y2));
if (y2 < r && x1 < x && x < x2)
xs.push_back(x);
if (x1 < r && r < x2)
xs.push_back(r);
sort(All(xs));
double res = 0;
Rep(i, Size(xs)-1) {
double x1 = xs[i], x2 = xs[i+1];
if (x1 > r-eps)
break;
double s1 = y1*(x2-x1), s2 = y2*(x2-x1);
double s4 = (0.5*x2*sqrt(sqr(r)-sqr(x2))+0.5*sqr(r)*asin2(x2/r))-(0.5*x1*sqrt(sqr(r)-sqr(x1))+0.5*sqr(r)*asin2(x1/r));
double s3 = -s4;
res += max(0.0, min(s2, s4)-max(s1, s3));
}
assert(res <= (x2-x1)*(y2-y1));
return res;
}
int main() {
freopen("input.txt", "rt", stdin);
freopen("output.txt", "wt", stdout);
int t;
scanf("%d", &t);
For(test, 1, t) {
double f, R, t, r, g;
scanf("%lf%lf%lf%lf%lf", &f, &R, &t, &r, &g);
double sum = 0;
if (g > 2*f) {
double x = r;
while (x+f < R-t-f) {
double y = r;
while (sqrt(sqr(x+f)+sqr(y+f)) < R-t-f) {
sum += inters(x+f, y+f, x+g-f, y+g-f, R-t-f);
y += 2*r+g;
}
x += 2*r+g;
}
}
printf("Case #%d: %.6lf\n", test, (pi*sqr(R)-4*sum)/(pi*sqr(R)));
}
exit(0);
}
Everyone out there help me improve!!!
Again a bad answer by me. But was easy to write and correct in attempt 1:
Train Timetable
Problem
A train line has two stations on it, A and B. Trains can take trips from A to B or from B to A multiple times during a day. When a train arrives at B from A (or arrives at A from B), it needs a certain amount of time before it is ready to take the return journey - this is the turnaround time. For example, if a train arrives at 12:00 and the turnaround time is 0 minutes, it can leave immediately, at 12:00.
A train timetable specifies departure and arrival time of all trips between A and B. The train company needs to know how many trains have to start the day at A and B in order to make the timetable work: whenever a train is supposed to leave A or B, there must actually be one there ready to go. There are passing sections on the track, so trains don't necessarily arrive in the same order that they leave. Trains may not travel on trips that do not appear on the schedule.
Input
The first line of input gives the number of cases, N. N test cases follow.
Each case contains a number of lines. The first line is the turnaround time, T, in minutes. The next line has two numbers on it, NA and NB. NA is the number of trips from A to B, and NB is the number of trips from B to A. Then there are NA lines giving the details of the trips from A to B.
Each line contains two fields, giving the HH:MM departure and arrival time for that trip. The departure time for each trip will be earlier than the arrival time. All arrivals and departures occur on the same day. The trips may appear in any order - they are not necessarily sorted by time. The hour and minute values are both two digits, zero-padded, and are on a 24-hour clock (00:00 through 23:59).
After these NA lines, there are NB lines giving the departure and arrival times for the trips from B to A.
Output
For each test case, output one line containing "Case #x: " followed by the number of trains that must start at A and the number of trains that must start at B.
Limits
1 ≤ N ≤ 100
Small dataset
0 ≤ NA, NB ≤ 20
0 ≤ T ≤ 5
Large dataset
0 ≤ NA, NB ≤ 100
0 ≤ T ≤ 60
Sample
Input
2
5
3 2
09:00 12:00
10:00 13:00
11:00 12:30
12:02 15:00
09:00 10:30
2
2 0
09:00 09:01
12:00 12:02
Output
Case #1: 2 2
Case #2: 2 0
My answer - agin bad but I got a hold of OOPS
// train.cpp : Defines the entry polong int for the console application.
//
#include "stdafx.h"
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string>
#include<iostream>
using namespace std;
class train
{
public:
long int presentstation;
long int availableafter;
long int turnaround;
public:
void reachstation(long int station,long int time)
{
presentstation = station;
availableafter = time+turnaround;
}
void settrain(long int tt)
{
turnaround = tt;
}
};
class time
{
public:
long int start;
long int end;
long int station;
public:
void settime(long int Station,char* time)
{
station =Station;
start = (time[0] - '0') *600 + (time[1] -'0')*60 + (time[3] - '0')*10 + (time[4]-'0');
end = (time[6] - '0') *600 + (time[7] -'0')*60 + (time[9] - '0')*10 + (time[10]-'0');
}
};
int comp(const void *A,const void *B)
{
time *p = (time*) A;
time *q = (time*) B;
return (int) (p->start - q->start);
}
long int main()
{
long int no =0;
cin>>no;
for(long int counter = 1; counter<=no;counter++)
{
printf("Case #%d: ",counter);
long int turnaround = 0;
long int NA,NB;
cin>>turnaround;
cin>>NA>>NB;
char useless[7];
if(NA+NB != 0 )
{gets_s(useless);
}
time Time[200];
train Train[200];
long int NoTrains = 0;
char a[100];
for(long int i=0;i<NA; i++)
{
gets_s(a);
Time[i].settime(0,a);
strcpy_s(a,"");
}
for(long int i=NA;i<(NB+NA); i++)
{
gets_s(a);
Time[i].settime(1,a);
}
qsort(Time,NA+NB,sizeof(time),&comp);
//printf("%d\t%d\t%d",Time[0].start,Time[1].start,Time[2].start);
long int flag;
long int startA=0,startB=0;
for(long int i=0; i<NA+NB; i++)
{
flag=0;
for(long int j=0;j<NoTrains; j++)
{
if((Train[j].availableafter <= Time[i].start) && (Train[j].presentstation == Time[i].station))
{
Train[j].reachstation(1-Time[i].station,Time[i].end);
flag=1;
break;
}
}
if(flag==0)
{
Train[NoTrains].settrain(turnaround);
Train[NoTrains].reachstation(1-Time[i].station,Time[i].end);
NoTrains++;
if(Time[i].station == 0)
startA++;
else
startB++;
}
}
printf("%d %d\n",startA,startB);
}
return 0;
}
and the great answer by rem
#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <numeric>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <ctime>
using namespace std;
typedef long long int64;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<double> vd;
typedef pair<int,int> pii;
#define _CRT_SECURE_NO_WARNINGS
#define For(i,a,b) for (int i(a),_b(b); i <= _b; ++i)
#define Ford(i,a,b) for (int i(a),_b(b); i >= _b; --i)
#define Rep(i,n) for (int i(0),_n(n); i < _n; ++i)
#define Repd(i,n) for (int i((n)-1); i >= 0; --i)
#define Fill(a,c) memset(&a, c, sizeof(a))
#define MP(x, y) make_pair((x), (y))
#define All(v) (v).begin(), (v).end()
template<typename T, typename S> T cast(S s) {
stringstream ss;
ss << s;
T res;
ss >> res;
return res;
}
template<typename T> inline T sqr(T a) { return a*a; }
template<typename T> inline int Size(const T& c) { return (int)c.size(); }
template<typename T> inline void checkMin(T& a, T b) { if (b < a) a = b; }
template<typename T> inline void checkMax(T& a, T b) { if (b > a) a = b; }
int readTime() {
int h, m;
scanf("%d:%d", &h, &m);
return m+60*h;
}
int main() {
freopen("input.txt", "rt", stdin);
freopen("output.txt", "wt", stdout);
int t;
scanf("%d", &t);
For(test, 1, t) {
int t;
scanf("%d", &t);
int n1, n2;
scanf("%d%d", &n1, &n2);
vector<pii> t1(n1), t2(n2);
Rep(i, n1) {
t1[i].first = readTime();
t1[i].second = readTime();
}
Rep(i, n2) {
t2[i].first = readTime();
t2[i].second = readTime();
}
sort(All(t1));
sort(All(t2));
int r1 = 0, r2 = 0;
while (Size(t1) > 0 || Size(t2) > 0) {
int x;
if (Size(t2) > 0 && (Size(t1) == 0 || t2[0] < t1[0])) {
++r2;
x = t2[0].second;
t2.erase(t2.begin());
} else {
++r1;
x = -t;
}
for (;;) {
int i = 0;
while (i < Size(t1) && t1[i].first < x+t)
++i;
if (i == Size(t1))
break;
x = t1[i].second;
t1.erase(t1.begin()+i);
i = 0;
while (i < Size(t2) && t2[i].first < x+t)
++i;
if (i == Size(t2))
break;
x = t2[i].second;
t2.erase(t2.begin()+i);
}
}
printf("Case #%d: %d %d\n", test, r1, r2);
}
exit(0);
}
See how much I need to improve answer 3 in next post
I may be among the best programmers of my batch in my college but definitely I am not worth calling myself as a programmer. I am far from a standard contest programmer for my programs are still those written on college papers. Plus I am still new to the STl and the contest world. i have just had a few outings. But I plan to have massive changes to this state this year. I have started with the google code jam, a big programming contest popularised by google for programmer's all over the world. The contest had its qualifiers on thursday and indeed as said qualifiers are for only to sort out people who don't know what programming is, it came out. The first question was sort of easy and so was the second one( Even i could do that). But the third one was gud. Just not really as an article I write this posting to my blog to help me trace if i can improve as a programmaer this year. You can expect to see top coder contest codes to be here as well, for I just joined that.
Here is the link to the files Ques+Mine as well as ideal answers + Hope I see this next year:http://w14.easy-share.com/1700957331.html
I have converted the cpp to html. I have used a project at codeproject. The files are at:http://w14.easy-share.com/1700966549.html
So now I can paste the stuff here:
Saving the universe
Problem
The urban legend goes that if you go to the Google homepage and search for "Google", the universe will implode. We have a secret to share... It is true! Please don't try it, or tell anyone. All right, maybe not. We are just kidding.
The same is not true for a universe far far away. In that universe, if you search on any search engine for that search engine's name, the universe does implode!
To combat this, people came up with an interesting solution. All queries are pooled together. They are passed to a central system that decides which query goes to which search engine. The central system sends a series of queries to one search engine, and can switch to another at any time. Queries must be processed in the order they're received. The central system must never send a query to a search engine whose name matches the query. In order to reduce costs, the number of switches should be minimized.
Your task is to tell us how many times the central system will have to switch between search engines, assuming that we program it optimally.
Input
The first line of the input file contains the number of cases, N. N test cases follow.
Each case starts with the number S -- the number of search engines. The next S lines each contain the name of a search engine. Each search engine name is no more than one hundred characters long and contains only uppercase letters, lowercase letters, spaces, and numbers. There will not be two search engines with the same name.
The following line contains a number Q -- the number of incoming queries. The next Q lines will each contain a query. Each query will be the name of a search engine in the case.
Output
For each input case, you should output:
Case #X: Y
where X is the number of the test case and Y is the number of search engine switches. Do not count the initial choice of a search engine as a switch.
Limits
0 <>Input
2
5
Yeehaw
NSM
Dont Ask
B9
Googol
10
Yeehaw
Yeehaw
Googol
B9
Googol
NSM
B9
NSM
Dont Ask
Googol
5
Yeehaw
NSM
Dont Ask
B9
Googol
7
Googol
Dont Ask
NSM
NSM
Yeehaw
Yeehaw
Googol
Output
Case #1: 1
Case #2: 0
So here is what I managed: Not good but i need to keep a track of myself.
// savetheuniverse.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<iostream>
#include<string.h>
#include<conio.h>
#include<stdio.h>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int no =0;
cin>>no;
for(int counter = 1; counter<=no;counter++)
{
printf("Case #%d: ",counter);
int NoOfEngines = 0;
cin>>NoOfEngines;
char useless[7];
if(NoOfEngines != 0 )
gets_s(useless);
//cout<<NoOfEngines<<endl;
char Eng[1000][100];
int CalledEng[1000] = {0};
for (int Engine = 0;Engine<NoOfEngines;Engine++)
{
gets_s(Eng[Engine]);
//cout<<Eng[Engine]<<endl;
}
int NoOfSearches;
cin>>NoOfSearches;
//cout<<NoOfSearches<<endl;
if(NoOfSearches != 0 )
gets_s(useless);
char Sear[1000][100];
for(int Search = 0;Search <NoOfSearches; Search++)
{
gets_s(Sear[Search]);
//cout<<Sear[Search]<<endl;
}
int Present = 1;
int LastElementIndex=0;
while(1)
{
for(int i=LastElementIndex;i<NoOfSearches;i++)
{
for(int j=0;j<NoOfEngines;j++)
{
if(strcmp(Sear[i],Eng[j]) == 0)
{
if(CalledEng[j] != Present)
{
CalledEng[j] =Present;
LastElementIndex = i;
}
break;
}
}
}
//if(counter==8)
//printf("%d\t%d\n",Present,LastElementIndex);
int flag=0;
for(int i=0;i<NoOfEngines;i++)
{
if(CalledEng[i]<Present) {flag =1 ;}
}
if(flag==1) {break;}
Present++;
if(LastElementIndex >= NoOfSearches-1) break;
}
printf("%d\n",Present-1);
}
return 0;
}
And this was the ideal answer
#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <numeric>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <ctime>
using namespace std;
typedef long long int64;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<double> vd;
#define _CRT_SECURE_NO_WARNINGS
#define For(i,a,b) for (int i(a),_b(b); i <= _b; ++i)
#define Ford(i,a,b) for (int i(a),_b(b); i >= _b; --i)
#define Rep(i,n) for (int i(0),_n(n); i < _n; ++i)
#define Repd(i,n) for (int i((n)-1); i >= 0; --i)
#define Fill(a,c) memset(&a, c, sizeof(a))
#define MP(x, y) make_pair((x), (y))
#define All(v) (v).begin(), (v).end()
template<typename T, typename S> T cast(S s) {
stringstream ss;
ss << s;
T res;
ss >> res;
return res;
}
template<typename T> inline T sqr(T a) { return a*a; }
template<typename T> inline int Size(const T& c) { return (int)c.size(); }
template<typename T> inline void checkMin(T& a, T b) { if (b < a) a = b; }
template<typename T> inline void checkMax(T& a, T b) { if (b > a) a = b; }
char buf[1024*1024];
int main() {
freopen("input.txt", "rt", stdin);
freopen("output.txt", "wt", stdout);
gets(buf);
For(test, 1, atoi(buf)) {
int s, q;
map<string,int> num;
gets(buf);
s = atoi(buf);
Rep(i, s) {
gets(buf);
num[buf] = i;
}
gets(buf);
q = atoi(buf);
vi dp(s, 0);
Rep(i, q) {
gets(buf);
assert(num.count(buf) > 0);
int id = num[buf];
Rep(j, s)
if (j != id)
checkMin(dp[j], dp[id]+1);
dp[id] = 1000000;
}
printf("Case #%d: %d\n", test, *min_element(All(dp)));
}
exit(0);
}
I'll write answer 2 and 3 in a seperate post