Saturday, February 15, 2014

UVa 12032 - The Monkey and the Oiled Bamboo

Accepted date: 2014-02-15
Ranking (as of 2014-02-15): 27 out of 595
Language: C++

/*
  UVa 12032 - The Monkey and the Oiled Bamboo

  To build using Visual Studio 2012:
    cl -EHsc -O2 UVa_12032_The_Monkey_and_the_Oiled_Bamboo.cpp
*/

#include <algorithm>
#include <cstdio>
using namespace std;

const int n_max = 100000;
int rungs[n_max];

int main()
{
  int T;
  scanf("%d", &T);
  for (int t = 1; t <= T; t++) {
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; i++)
      scanf("%d", &rungs[i]);
    int k = rungs[0];
    for (int i = 1; i < n; i++)
      k = max(k, rungs[i] - rungs[i - 1]);
    int j = k;
    if (rungs[0] == j)
      j--;
    for (int i = 1; i < n; i++) {
      int d = rungs[i] - rungs[i - 1];
      if (d == j)
        j--;
      else if (d > j) {
        k++; break;
      }
    }
    printf("Case %d: %d\n", t, k);
  }
  return 0;
}

Tuesday, February 11, 2014

UVa 11549 - Calculator Conundrum

Accepted date: 2014-02-11
Ranking (as of 2014-02-11): 275 out of 702
Language: C++

/*
  UVa 11549 - Calculator Conundrum

  To build using Visual Studio 2012:
    cl -EHsc -O2 UVa_11549_Calculator_Conundrum.cpp
*/

#include <set>
#include <cstdio>
#include <cmath>
using namespace std;

int main()
{
  const int power_of_10s[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 
    10000000, 100000000, 1000000000};
  int t;
  scanf("%d", &t);
  while (t--) {
    int n, k;
    scanf("%d %d", &n, &k);
    if (k < 2)
      printf("%d\n", k);
    else {
      int power_of_10 = power_of_10s[n];
      int k_max = power_of_10 - 1, displayed_max = 0;
      set<int> displayed_numbers;
      while (true) {
        if (k == k_max) {
          displayed_max = k; break;
        }
        pair<set<int>::iterator, bool> result = displayed_numbers.insert(k);
        if (!result.second)
          break;
        if (k > displayed_max)
          displayed_max = k;
        long long lk = k;
        lk *= lk;
        int nr_digits = static_cast<int>(log10(static_cast<double>(lk)) + 1.0);
        if (nr_digits > n)
          lk /= power_of_10s[nr_digits - n];
        k = static_cast<int>(lk);
#ifdef DEBUG
        printf("%d\n", k);
#endif
      }
      printf("%d\n", displayed_max);
    }
  }
  return 0;
}