Wednesday, August 30, 2017

【UVa】11729 - Commando War

【UVa】11729 - Commando War

Problem here

Solution


N年冇寫
先水一題
xdd


按執行時間由大到小排序
找完成所有任務的總時間

#include <iostream>
#include <algorithm>
#include <vector>
#include <utility>
using namespace std;
bool cmp(pair<int, int>a , pair<int, int> ai){
    return a.second > ai.second;
}
int main(){
    int n, b, j;
    int count = 0;
    while(cin >> n){
        vector<pair<int, int> > s;
        if(n == 0)
            break;

        for(int i = 0; i < n; i++){
            cin >> b >> j;
            s.push_back(make_pair(b,j));
        }
        sort(s.begin(), s.end(), cmp);
        int time = 0,tb = 0;
        for(int i = 0; i < s.size(); i++){
            tb += s[i].first;
            time = max(time, tb+s[i].second);
        }
        cout << "Case " << ++count << ": " << time << endl;
    }
    return 0;
}

Sunday, May 21, 2017

【CodeChef】Fit Squares in Triangle

Problem here

Solution

water

#include <iostream>
using namespace std;

int main(){
    int n;
    while(cin >> n){
        while(n--){
            int l;
            cin >> l;
            int t = (l/2-1)*((l/2-1)+1) / 2;
            if(t)
                cout << t << endl;
            else 
                cout << 0 << endl;
        }
    }
    return 0;
}

Tuesday, May 16, 2017

【UVa】Brick Wall Patterns

Problem here

Solution

*記得要開long long int
#include <iostream>
#include <memory.h>
using namespace std;
long long int dp[51];
int main(){
    int n;
    memset(dp, 0, sizeof(dp));
    dp[0] = 0;
    dp[1] = 1;
    dp[2] = 2;
    for(int i = 3; i <= 50; i++)
        dp[i] = dp[i-1] + dp[i-2];
    while(cin >> n){
        if(n == 0)
            break;
        cout << dp[n] << endl;
    }
    return 0;
}

Saturday, May 13, 2017

【UVa】11015 Rendezvous

Problem here

Solution

輸入是從1開始數的,所以讀入時將u, v 都-1
先刷所有最短路
再找最小總和
#include <iostream>
#include <string>
#include <memory.h>
#include <vector>
#define max_num 9999
using namespace std;
int rat[30][30];
vector<string> names;
int main(){

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

        for(int i = 0; i < n; i++)
            for(int j = 0; j < n; j++)
                if(i==j)
                    rat[i][j] = rat[j][i] = 0;
                else
                    rat[i][j] = rat[j][i] = max_num;

        for(int i = 0; i < n; i++){
            string input;
            cin >> input;
            names.push_back(input);
        }
        for(int i = 0; i < m; i++){
            int u, v, k;
            cin >> u >> v >> k;
            rat[u-1][v-1] = rat[v-1][u-1] = k;
        }

        for(int i = 0; i < n; i++)
            for(int j = 0; j < n; j++)
                for(int k = 0; k < n; k++)
                    if(rat[j][k] >  rat[j][i] + rat[i][k])
                        rat[j][k] =  rat[j][i] + rat[i][k]; 

        int min = max_num;
        int rec;
        for(int i = 0; i < n; i++){
            int sum = 0;
            for(int j = 0; j < n; j++){
                sum += rat[i][j];
            }
            if(sum < min){
                min = sum;
                rec = i;
            }
        }
        cout << "Case #" << kase++ << " : " << names[rec] << endl;
        names.clear();
    }

    return 0;
}

【UVa】11463 - Commandos

Problem here

Solution

floyd刷所有點到其他點的最短路
再在上面找開始點到集合點的最大值
  1. #include <iostream>
  2. #include <memory.h>
  3. #define MAX_NUM 1<<29
  4. using namespace std;
  5. int main(){
  6. int kase;
  7. cin >> kase;
  8. int cnt = 1;
  9. while(kase--){
  10. int n, r;
  11. int rat[101][101];
  12. cin >> n >> r;
  13. for(int i = 0; i < n; i++)
  14. for(int j = 0; j < n; j++)
  15. if(i==j)
  16. rat[i][j] = rat[j][i] = 0;
  17. else
  18. rat[i][j] = MAX_NUM;
  19. int s, e;
  20. while(r--){
  21. int x, y;
  22. cin >> x >> y;
  23. rat[x][y] = rat[y][x] = 1;
  24. }
  25. for(int i = 0; i < n; i++)
  26. for(int j = 0; j < n; j++)
  27. for(int k = 0; k < n; k++)
  28. if(rat[j][i] + rat[i][k] < rat[j][k])
  29. rat[j][k] = rat[j][i] + rat[i][k];
  30. cin >> s >> e;
  31. int ans = 0;
  32. for(int i = 0; i < n; i++)
  33. if(ans < rat[s][i] + rat[i][e])
  34. ans = rat[s][i] + rat[i][e];
  35. cout << "Case " << cnt++ << ": " << ans << endl;
  36. }
  37. return 0;
  38. }

Friday, May 12, 2017

【UVa】Expanding Fractions

Problem here

Solution

  1. #include <iostream>
  2. #include <memory.h>
  3. #include <string>
  4. using namespace std;
  5. int main(){
  6. int n, m;
  7. int len[1001];
  8. while(cin >> n >> m){
  9. if(n == 0 && m == 0)
  10. break;
  11. memset(len, -1, sizeof(len));
  12. len[n] = 0;
  13. string output = ".";
  14. while(n){
  15. n *= 10;
  16. output += (n/m) + '0';
  17. n %= m;
  18. if(len[n%m] != -1)
  19. break;
  20. len[n] = output.length()-1;
  21. }
  22. for(int i = 0; i < output.length(); i++){
  23. if(i > 0 && (i%50)==0 )
  24. cout << endl;
  25. cout << output[i];
  26. }
  27. cout << endl;
  28. if(n != 0)
  29. cout << "The last " << output.length() - len[n%m] - 1 << " digits repeat forever." << endl;
  30. else
  31. cout << "This expansion terminates." << endl;
  32. //UVa 要再output一空行
  33. //cout << endl
  34. }
  35. return 0;
  36. }

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. }