Thursday, May 11, 2017

【UVa】Frogger

Problem here

Solution

自己的石子為點0,目標石子為1
生成MST直到點0點1連通
  1. #include <iostream>
  2. #include <vector>
  3. #include <memory.h>
  4. #include <utility>
  5. #include <string>
  6. #include <math.h>
  7. #include <stdio.h>
  8. #include <algorithm>
  9. using namespace std;
  10. vector<pair<float, pair<int, int> > > edges;
  11. string spp;
  12. float cal(int x1, int x2, int y1, int y2){
  13. return (float)sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
  14. }
  15. bool cmp(pair<float, pair<int, int> > a, pair<float, pair<int, int> > b){
  16. return a.first < b.first;
  17. }
  18. int pre[1000];
  19. int find(int x){
  20. int r = x;
  21. while(pre[r] != r){
  22. r = pre[r];
  23. }
  24. int j = x, y;
  25. while(j != r){
  26. y = pre[j];
  27. pre[j] = r;
  28. j = y;
  29. }
  30. return r;
  31. }
  32. void join(int a, int b){
  33. int fa = find(a);
  34. int fb = find(b);
  35. if(fa != fb){
  36. pre[fa] = fb;
  37. }
  38. }
  39. int main(){
  40. int n;
  41. int count = 1;
  42. while(cin >> n){
  43. if(n==0)
  44. break;
  45. vector<pair<int , int > > point;
  46. for(int i = 0; i < n; i++){
  47. int x, y;
  48. cin >> x >> y;
  49. point.push_back(make_pair(x,y));
  50. for(int j = 0; j < i; j++){
  51. edges.push_back(make_pair(cal(point[i].first, point[j].first, point[i].second, point[j].second), make_pair(i, j)) );
  52. }
  53. }
  54. cout << "Scenario #" << count++ << endl;
  55. sort(edges.begin(), edges.end(), cmp);
  56. for(int i = 0; i < n; i++)
  57. pre[i] = i;
  58. for(int i = 0; i < edges.size(); i++){
  59. if(find(edges[i].second.first)==find(edges[i].second.second))
  60. continue;
  61. join(edges[i].second.first, edges[i].second.second);
  62. if(find(1) == find(0)){
  63. printf("Frog Distance = %.3lf\n", edges[i].first);
  64. break;
  65. }
  66. }
  67. edges.clear();
  68. cout << endl;
  69. getline(cin, spp);
  70. }
  71. return 0;
  72. }

Wednesday, May 10, 2017

【Zerojudge】d908

Problem here

Solution

DFS
  1. #include <iostream>
  2. #include <memory.h>
  3. using namespace std;
  4. int rat[30][30];
  5. bool visit[30];
  6. int sum;
  7. void solve(int here, int cost){
  8. sum = max(sum, cost);
  9. visit[here] = true;
  10. for(int i = 0; i < 26; i++){
  11. if(visit[i] == false && rat[here][i] != 0){
  12. solve(i, cost+rat[here][i]);
  13. visit[i] = false;
  14. }
  15. }
  16. }
  17. int main(){
  18. char start;
  19. while(cin >> start){
  20. int n;cin >> n;
  21. sum = 0;
  22. memset(rat, 0, sizeof(rat));
  23. memset(visit, false, sizeof(visit));
  24. while(n--){
  25. char a, b;int w;
  26. cin >> a >> b >> w;
  27. rat[a-'A'][b-'A'] = max(rat[a-'A'][b-'A'], w);
  28. }
  29. solve(start-'A', sum);
  30. cout << sum << endl;
  31. }
  32. return 0;
  33. }

Monday, May 8, 2017

【UVa】12149 - Feynman

Problem here

Solution

N增加1, 總正方形數會增加N*N


  1. #include <iostream>
  2. #include <memory.h>
  3. using namespace std;
  4. int mark[101];
  5. int main(){
  6. int n;
  7. memset(mark, 0, sizeof(mark));
  8. while(cin >> n){
  9. if(n == 0)
  10. break;
  11. for(int i = 1 ; i <= n; i++)
  12. if(mark[i] == 0)
  13. mark[i] = mark[i-1] + (i*i);
  14. cout << mark[n] << endl;
  15. }
  16. return 0;
  17. }

Wednesday, May 3, 2017

【SPOJ】AIBOHP - Aibohphobia

Problem here

Solution

答案=原字串長度-原字串與其反轉後字串的lcm
一開始用top down寫,TLE orz…. 
之後改成 bottom up 才AC
#include <iostream>
#include <string>
using namespace std;
string input, res;
int lcs[6101][6101];

int main(){

    int kase;
    cin >> kase;
    while(kase--){
        cin >> input;
        res.resize(input.size());
        for(int i = 0; i < input.length(); i++)
            res[i] = input[input.length()-1-i];

        for(int m = 0; m <= input.length(); m++){
            for(int n = 0; n <= res.length(); n++){
                if(m == 0 || n == 0)
                    lcs[m][n] = 0;
                else if(input[m-1] == res[n-1])
                    lcs[m][n] = 1 + lcs[m-1][n-1];
                else
                    lcs[m][n] = max(lcs[m][n-1], lcs[m-1][n]);
            }
        }

        cout << input.length() - lcs[input.length()][res.length()] << endl;
    }

    return 0;
}

Tuesday, February 28, 2017

【POJ】Symmetric Order

Problem here

Solution

#include <iostream>
#include <string>
#include <vector>
using namespace std;

vector<string> strs;
int n, cnt = 0;

void solve(int a, int b){
    if(b == strs.size() || a == strs.size())
        return;

    cout << strs[a] << endl;
    solve(a+2, b+2);
    if(strs[b] != " ")
        cout << strs[b] << endl;
}

int main(){
    while(cin >> n){
        strs.clear();
        if(n == 0)
            break;
        for(int i = 0; i < n; i++){
            string input;
            cin >> input;
            strs.push_back(input);
        }
        cout << "SET " << ++cnt << endl;
        if(n%2!=0)
            strs.push_back(" ");
        solve(0,1);

    }

    return 0;
}

Sunday, February 5, 2017

【USACO】Barn Repair

Problem here

Solution

dp~~~
/*
ID: LeongHouHeng
PROG: barn1
LANG: C++
 */

 #include <iostream>
 #include <memory.h>
 #include <string.h>
 #include <fstream>
 #include <algorithm>
 using namespace std;

 ifstream fin("barn1.in");
 ofstream fout("barn1.out");

 int cows[201];
 int dp[250][60];
 int MIN_NUM = 2e9;
 int main(){
     int M, S, C;
     fin >> M >> S >> C;

     for(int i = 0; i < C; i++){
         fin >> cows[i];
     }
     sort(cows, cows+C);
     memset(dp, 0x1f, sizeof(dp));
     dp[0][0] = 1;
     for(int i = 1; i < C; i++){
         for(int j = 0; j <= i && j < M; j++){
            dp[i][j] = dp[i-1][j] + (cows[i] - cows[i-1]);
            if(j > 0)
                dp[i][j] = min(dp[i][j], dp[i-1][j-1] + 1);
            if(C-1 == i)
                MIN_NUM = min(MIN_NUM, dp[i][j]);
         }
     }

     fout << MIN_NUM << endl;
 }

Friday, December 2, 2016

【UVa】11636 - Hello World!

Problem here

Solution

water water water water ~~~
#include <iostream>
using namespace std;

int main(){

    int n;
    int kase = 1;
    while(cin >> n){
        if(n < 0)
            break;

        int ans = 1;
        int cnt = 0;
        while(ans < n){
            ans = ans * 2;
            cnt++;
        }
        cout << "Case " << kase++ << ": " << cnt << endl;
    }
    return 0;
}

Installing xAct (Mac)

Download from http : www.xact.es . Before unpack the tar/tgz file, remove attributes with xattr -c xAct_1.x.x.tgz then place the un...