Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions UVA/10375.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// AC
#include <bits/stdc++.h>
using namespace std;

void add_to_queue(priority_queue<int> & 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<int> num;
priority_queue<int> 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);
}
}

60 changes: 60 additions & 0 deletions UVA/10911.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// AC
// Bitmask dp
#include <bits/stdc++.h>
#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<<first_free) | (1<<i)));
}
}
return dp[mask] = ans;
}

int main(){
cin>>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;
}
}
39 changes: 39 additions & 0 deletions UVA/12439.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/* AC
Inclusion-Exclusion
Skip a year if February has already passed
*/

#include <bits/stdc++.h>
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;
}
43 changes: 43 additions & 0 deletions UVA/573.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// AC
// Lots of tricky statement conditions

#include <bits/stdc++.h>
#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){
// Failure
printf("failure on day %d\n", day);
}
cin>>H>>U>>D>>F;
}
}
108 changes: 108 additions & 0 deletions UVA/608.cpp
Original file line number Diff line number Diff line change
@@ -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 <bits/stdc++.h>
using namespace std;
string left_s[3], right_s[3], result[3];
map<char, bool> 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)<EPS){
return res == "even";
}
if(lw>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;
}
}
}
}
}
24 changes: 24 additions & 0 deletions UVA/900.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// AC
#include <bits/stdc++.h>

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<<solve(n)<<endl;
cin>>n;
}
}