Introduction

This is my blog of programming, I take notes and leave codes of computer science problems I solved here. Be my guest to comment :)

Monday, December 31, 2012

uva 10819 - Trouble of 13-Dots


 1 /*
 2  Problem link
 3  Type: DP, knapsack(0/1)
 4  */

Thursday, December 20, 2012

uva 147 - Dollars


 1 /*
 2  Problem link
 3  Type: DP
 4  Algorithm:

uva 352 - The Seasonal War


/*
Problem link
Type: Graph, flood fill
*/
 1  #include <iostream>
 2  #include <cstdio>
 3  #include <cstring>
 4  #include <cmath>
 5  #include <cstdlib>
 6  using namespace std;

Tuesday, December 18, 2012

uva 11995 - I Can Guess the Data Structure!


/*
Problem link
Type: Data structure
*/  
  1  #include <iostream>
  2  #include <cstdio>
  3  #include <cstring>
  4  #include <cmath>
  5  #include <cstdlib>
  6  #include <vector>
  7  #include <queue>
  8  #include <stack>

Saturday, December 15, 2012

uva 10954 - Add All



/*
Problem link
Type: Adhoc - greedy, Data structure - priority queue
*/
 1  #include <iostream>
 2  #include <cstdio>
 3  #include <cstring>
 4  #include <cmath>
 5  #include <vector>
 6  #include <queue>

Friday, December 14, 2012

uva 11926 - Multitasking


/*
Problem link
Type: Adhoc, brute-force
*/
 1  #include <iostream>
 2  #include <cstdio>
 3  #include <cstring>
 4  #include <cmath>
 5  #include <cstdlib>
 6  #include <vector>
 7  using namespace std;
 8  const int maxn = 1000010;

Thursday, December 13, 2012

Monday, December 10, 2012

uva 11235 - Frequent values



//============================================================================
// Name        : 11235.cpp
// Author      : Mr Tu
// Version     :
// Copyright   : Your copyright notice
// Description : Uva 11235 - Frequent values in C++, Ansi-style
// Problem link
// Algorithm: Data structure - Segment tree
//============================================================================

Sunday, December 9, 2012

uva 11631 - Dark roads


/*
Problem link
Type: Graph
Algorithm: Kruskal, minimum spanning tree, using priority queue
*/
#include <iostream>
#include <queue>
#include <vector>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <fstream>
using namespace std;
const int maxn = 200010;

Thursday, December 6, 2012

uva 10616 - Divisible Group Sums


/*
Problem link
Type: DP, Mathematics
Algorithm:
 Let f(i,j,k) is the number of ways to choose i elements from a[1..j] so that
 their sum divides d gives the remainder of k. 

Tuesday, December 4, 2012

uva 473 - Raucous Rockers


/*
Problem link
Type: DP
Algorithm:
 Let f(i,j,k) be the largest amount of songs from 1 to k can be written into 
 the disks from 1 to i with disk i having space j

The Fibonacci Numbers


  • The rabbits growth problem:

Fibonacci's book Liber Abaci written in 1202 concerned about the growth of the number of rabbits in a certain area. The problem is: A young pair of rabbits, one of each sex, is placed on an island. Assuming that rabbits do not breed until they are two months old and after they are two months old, each pair of rabbits produces another pair each month, how many pairs are there after n months?

Let f(n) be the number of rabbits after n months. We have f(1) = 1 because only the original pair is on the island after one month. As this pair does not breed during the second month, f(2) = 1. To find the number of pairs after n months, add the number on the island the previous month, f(n-1), to the number of newborn pairs, which equals to f(n-2), because each newborn pair comes from a pair at least two months old. This leads to the following definition.


  • Definition: 

The Fibonacci sequence is defined recursively by f(1) = 1, f(2) = 1, and f(n) = f(n-1) + f(n-2) for n>=3. The terms of this sequence are called the Fibonacci Numbers

Monday, December 3, 2012

uva 12015 - Google is Feeling Lucky


/*
Problem link
*/
#include <iostream>
#include <cstdio>
using namespace std;
const int n = 20;

class url {
public:
 string link;
 int value;
};

url a[n];

int main() {
 int ntest;
 string line;
 cin >> ntest;
 for (int test=1; test<=ntest; test++)
 {
  printf("Case #%d:\n",test);
  for (int i=1; i<=10; i++) 
  {
   cin >> a[i].link;
   cin >> a[i].value;
  }
  int themax = 0;
  for (int i=1; i<=n; i++) themax = a[i].value>themax ? a[i].value : themax;
  for (int i =1; i<=n; i++)
   if (a[i].value==themax) cout << a[i].link << endl;
 } 
 return 0;
}

uva 11942 - Lumberjack Sequencing


/*
Problem link
*/
#include <iostream>
#include <cstdio>
using namespace std;
const int n = 10;

int a[n];

int main() {
 int ntest;
 cin >> ntest;
 cout << "Lumberjacks:" << endl;
 for (int test=1; test<=ntest; test++)
 {
  for (int i=1; i<=n; i++) cin >> a[i];
  int tmp = a[2]-a[1];
  int i;
  for (i=3; i<=n; i++)
   if ((a[i]-a[i-1])*tmp<=0)
   {
    cout << "Unordered" << endl;
    break;
   }
  if (i==n+1) cout << "Ordered" << endl;
 }
 return 0;
}