From c98af8f6919a3134cd701d6b926837e572474ff5 Mon Sep 17 00:00:00 2001 From: Amr Keleg Date: Sun, 6 Oct 2019 18:21:12 +0200 Subject: [PATCH] October 2019 Add solutions to exercises from competitive programming book (3rd Edition) --- UVA/10375.cpp | 42 ++++++++++++++++++++ UVA/10911.cpp | 60 ++++++++++++++++++++++++++++ UVA/12439.cpp | 39 ++++++++++++++++++ UVA/573.cpp | 43 ++++++++++++++++++++ UVA/608.cpp | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++ UVA/900.cpp | 24 +++++++++++ 6 files changed, 316 insertions(+) create mode 100644 UVA/10375.cpp create mode 100644 UVA/10911.cpp create mode 100644 UVA/12439.cpp create mode 100644 UVA/573.cpp create mode 100644 UVA/608.cpp create mode 100644 UVA/900.cpp diff --git a/UVA/10375.cpp b/UVA/10375.cpp new file mode 100644 index 0000000..0f6027b --- /dev/null +++ b/UVA/10375.cpp @@ -0,0 +1,42 @@ +// AC +#include +using namespace std; + +void add_to_queue(priority_queue & q, int val){ + while(val>1){ + q.push(val--); + } +} + +int main(){ + #ifndef ONLINE_JUDGE + freopen("in.txt","r",stdin); + #endif + int p, q, r, s; + while(cin>>p>>q>>r>>s){ + priority_queue num; + priority_queue den; + add_to_queue(num, p); + add_to_queue(num, r-s); + add_to_queue(num, s); + add_to_queue(den, q); + add_to_queue(den, p-q); + add_to_queue(den, r); + long double ans = 1; + while (!num.empty() || !den.empty()){ + if (!num.empty()) + { + ans *= num.top(); + num.pop(); + } + + if (!den.empty()) + { + ans /= den.top(); + den.pop(); + } + } + printf("%0.5Lf\n", ans); + } +} + diff --git a/UVA/10911.cpp b/UVA/10911.cpp new file mode 100644 index 0000000..6699c88 --- /dev/null +++ b/UVA/10911.cpp @@ -0,0 +1,60 @@ +// AC +// Bitmask dp +#include +#define INF 1000000000 +using namespace std; +const int max_size = 1<<16; +long double dp[max_size]; +bool solved[max_size]; + +int x[16], y[16], N; + +bool done(int mask){ + return mask == ((1<<2*N)-1); +} + +bool free(int mask, int index){ + return ((mask>>index) &1) ==0; +} + +long double distance(int a, int b){ + return sqrt((x[a]-x[b])*(x[a]-x[b]) + (y[a]-y[b])*(y[a]-y[b])); +} + +long double solve(int mask=0){ + if(solved[mask]) + return dp[mask]; + if(done(mask)){ + solved[mask] = 1; + return dp[mask] = 0; + } + solved[mask] = 1; + int first_free = 0; + while(!free(mask, first_free)){ + first_free++; + } + long double ans = INF; + for(int i=first_free+1; i<2*N;i++){ + if(free(mask, i)){ + ans = min(ans, + distance(first_free, i) + solve(mask | (1<>N; + int t=1; + string _; + + while(N){ + memset(solved, 0, sizeof solved); + for(int i=0; i<2*N;i++){ + cin>>_>>x[i]>>y[i]; + } + printf("Case %d: %0.2LF\n", t, solve()); + t++; + cin>>N; + } +} \ No newline at end of file diff --git a/UVA/12439.cpp b/UVA/12439.cpp new file mode 100644 index 0000000..94cbc6c --- /dev/null +++ b/UVA/12439.cpp @@ -0,0 +1,39 @@ +/* AC + Inclusion-Exclusion + Skip a year if February has already passed +*/ + +#include +using namespace std; + +int find_no_of_leap(int year){ + // leap years from 0 to year inclusive + return year/4 - year/100 + year/400; +} + +int find_no_of_leap_in_range(int start_year, int end_year){ + return start_year>end_year?0:find_no_of_leap(end_year) - find_no_of_leap(start_year-1); +} + +int main(){ + int T; + cin>>T; + char month[12]; + int day, start_year, end_year; + for(int t=1; t<=T;t++){ + scanf("%s %d, %d", month, &day, &start_year); + string m(month); + if (m!="January" && m!="February"){ + // this year isn't included (February 29 have already passed if it's a leap year) + start_year++; + } + scanf("%s %d, %d", month, &day, &end_year); + m = string(month); + if (m=="January" || (m=="February" && day<29)){ + // this year isn't included + end_year--; + } + printf("Case %d: %d\n", t, find_no_of_leap_in_range(start_year, end_year)); + } + return 0; +} \ No newline at end of file diff --git a/UVA/573.cpp b/UVA/573.cpp new file mode 100644 index 0000000..b7c8427 --- /dev/null +++ b/UVA/573.cpp @@ -0,0 +1,43 @@ +// AC +// Lots of tricky statement conditions + +#include +#define EPS (1e-10) +using namespace std; + +int main(){ + int H; + + // Use double for U + long double U, D, F; + cin>>H>>U>>D>>F; + while(H!=0){ + long double lost_value = U * (0.01*F); + long double height = 0; + int day = 1; + while(true){ + height += U; + // Trick: Snail can reach the top before night (before going down again) + if (height > H){ + // Success + printf("success on day %d\n", day); + break; + } + height -= D; + // Trick: Avoid double comparison + if (height<0 && abs(height)>EPS) + break; + U -= lost_value; + + // Trick: Snail can't climb negative values + if (U<0) + U = 0; + day++; + } + if (height>H>>U>>D>>F; + } +} diff --git a/UVA/608.cpp b/UVA/608.cpp new file mode 100644 index 0000000..27a1b28 --- /dev/null +++ b/UVA/608.cpp @@ -0,0 +1,108 @@ +/* AC + +Hints: +- If the balance shows even, then all the coins are good +- If the balance shows up or down, then all the (missing) coins are good. +"The bad coin is already on the balance, all the other coins are good" +- Brute force on all the feasible solutions (coins that aren't known to be good) +and validate the rules. +*/ +#include +using namespace std; +string left_s[3], right_s[3], result[3]; +map coins; +#define EPS (1e-9) + +bool in_str(char c, string s){ + for(int i=0; i< s.size(); i++) + { + if(c==s[i]) + return 1; + } + return 0; +} + +string find_missing(string a, string b){ + string missing = ""; + for(char c='A'; c<='L'; c++){ + if(in_str(c, a) || in_str(c, b)) + continue; + missing += string(c, 1); + } + return missing; +} + +long double get_weight(string s, char c, bool up){ + long double weight = 0; + for(auto i:s){ + if(c==i){ + if(up) + // Give the bad coin higher weight + weight += 1.5; + else + // Give the bad coin lower weight + weight += 0.5; + } + else{ + // The default weight of the coin + weight += 1; + } + } + return weight; +} + +bool validate(string l, string r, string res, char c, bool up){ + long double lw = get_weight(l, c, up); + long double rw = get_weight(r, c, up); + if (abs(lw-rw)rw){ + return res == "up"; + } + return res == "down"; +} + +int main(){ + int n; + cin>>n; + while(n--){ + coins.clear(); + for(int i=0;i<3;i++){ + cin>>left_s[i]>>right_s[i]>>result[i]; + string equal; + if (result[i] == "even"){ + equal = left_s[i] + right_s[i]; + } + else{ + equal = find_missing(left_s[i], right_s[i]); + } + for(auto c: equal) + { + coins[c] = 1; + } + } + + for(char c='A'; c<='L'; c++){ + if(coins[c]) + continue; + for(int up=0;up<2;up++){ + int valid_rules = 0; + for(int i=0; i<3; i++){ + if(validate(left_s[i], right_s[i], result[i], c, up)) + valid_rules++; + } + if(valid_rules == 3){ + // Done + printf("%c is the counterfeit coin and it is ", c); + if(up) + printf("heavy.\n"); + else + printf("light.\n"); + c = 'M'; + break; + } + } + } + } +} \ No newline at end of file diff --git a/UVA/900.cpp b/UVA/900.cpp new file mode 100644 index 0000000..087b7f0 --- /dev/null +++ b/UVA/900.cpp @@ -0,0 +1,24 @@ +// AC +#include + +using namespace std; +long long int dp[51]; + +long long int solve(int len){ + if (len<0) + return 0; + if (len==0) + return 1; + if (dp[len] != 0) + return dp[len]; + return dp[len] = solve(len-1) + solve(len-2); +} +int main(){ + freopen("in.txt", "r", stdin); + int n; + cin>>n; + while(n!=0){ + cout<>n; + } +} \ No newline at end of file