runnig

[codejam] veterans-2013 oceanview (not complete)

Mar 10th, 2013
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.89 KB | None | 0 0
  1. // https://code.google.com/codejam/contest/2334486/dashboard#s=p2
  2.  
  3. #include "stdafx.h"
  4. #include <iostream>
  5. #include <vector>
  6. #include <tuple>
  7. #include <unordered_map>
  8. #include <fstream>
  9.  
  10. using namespace std;
  11.  
  12. typedef size_t hash_t;
  13. typedef tuple<int,int,int> mykey_t;
  14.  
  15.  
  16. template<typename T>
  17. inline void hash_combine(hash_t & H, const T v)
  18. {
  19.     std::hash<T> hash_fn; // standard hash function
  20.     H ^= hash_fn(v) + 0x9e3779b9 + (H<<6) + (H>>2);
  21. }
  22.  
  23. // hash function for partial hash
  24. class mykey_hash_fn {
  25. public:
  26.     size_t operator ()(const mykey_t &v) const
  27.     {
  28.         hash_t H = 0;
  29.         hash_combine(H, std::get<0>(v));
  30.         hash_combine(H, std::get<1>(v));
  31.         hash_combine(H, std::get<2>(v));
  32.        
  33.         return H;
  34.     }
  35. };
  36.  
  37. class mykey_eq{
  38. public:
  39.     bool operator ()(const mykey_t &a, const mykey_t &b) const
  40.     {
  41.         return std::get<0>(a) == std::get<0>(b) &&
  42.             std::get<1>(a) == std::get<1>(b) &&
  43.             std::get<2>(a) == std::get<2>(b);
  44.     }
  45. };
  46.  
  47. typedef unordered_map<mykey_t, int, mykey_hash_fn, mykey_eq> cache_t;
  48.  
  49.  
  50. int maxIncrSubSeq(vector<int> & A, size_t pos, int maxSoFar, int lenSoFar, cache_t & cache)
  51. {
  52.     if(pos >= A.size()) { return lenSoFar; }
  53.  
  54.     auto key = make_tuple(pos, maxSoFar, lenSoFar);
  55.     cache_t::iterator it = cache.find(key);
  56.     if(it != cache.end()) { return it->second; }
  57.  
  58.     int L1 = 0, L2 = 0;
  59.  
  60.     if(A[pos] > maxSoFar)
  61.     {
  62.         L1 = maxIncrSubSeq(A, pos + 1, A[pos], lenSoFar + 1, cache);
  63.     }
  64.  
  65.     L2 = maxIncrSubSeq(A, pos + 1, maxSoFar, lenSoFar, cache);
  66.     int ret = max(L1, L2);
  67.     cache[key] = ret;
  68.     return ret;
  69.  
  70. }
  71.  
  72. int _tmain(int argc, _TCHAR* argv[])
  73. {
  74.     int T;
  75.  
  76.     ifstream fin(argv[1]);
  77.  
  78.     fin >> T;
  79.  
  80.     for(int t = 1; t <= T; ++t)
  81.     {
  82.         int N;
  83.         fin >> N;
  84.         vector<int> A(N);
  85.         for(int i = 0; i < N; ++i)
  86.         {
  87.             int h;
  88.             fin >> h;
  89.             A[i] = h;
  90.         }
  91.         int L = maxIncrSubSeq(A, 0, 0, 0, cache_t());
  92.         cout << "Case #"<< t << ": " << N - L << '\n';
  93.     }
  94.  
  95.     return 0;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment