74d1f45c5c 2014-10-14 kinaba: #include <iostream> 74d1f45c5c 2014-10-14 kinaba: #include <sstream> 74d1f45c5c 2014-10-14 kinaba: #include <iomanip> 74d1f45c5c 2014-10-14 kinaba: #include <vector> 74d1f45c5c 2014-10-14 kinaba: #include <string> 74d1f45c5c 2014-10-14 kinaba: #include <map> 74d1f45c5c 2014-10-14 kinaba: #include <set> 74d1f45c5c 2014-10-14 kinaba: #include <algorithm> 74d1f45c5c 2014-10-14 kinaba: #include <numeric> 74d1f45c5c 2014-10-14 kinaba: #include <iterator> 74d1f45c5c 2014-10-14 kinaba: #include <functional> 74d1f45c5c 2014-10-14 kinaba: #include <complex> 74d1f45c5c 2014-10-14 kinaba: #include <queue> 74d1f45c5c 2014-10-14 kinaba: #include <stack> 74d1f45c5c 2014-10-14 kinaba: #include <cmath> 74d1f45c5c 2014-10-14 kinaba: #include <cassert> 74d1f45c5c 2014-10-14 kinaba: #include <tuple> 74d1f45c5c 2014-10-14 kinaba: using namespace std; 74d1f45c5c 2014-10-14 kinaba: typedef long long LL; 74d1f45c5c 2014-10-14 kinaba: typedef complex<double> CMP; 74d1f45c5c 2014-10-14 kinaba: 74d1f45c5c 2014-10-14 kinaba: class SimilarRatingGraph { public: 74d1f45c5c 2014-10-14 kinaba: double maxLength(vector <int> date, vector <int> rating) 74d1f45c5c 2014-10-14 kinaba: { 74d1f45c5c 2014-10-14 kinaba: const int N = date.size(); 74d1f45c5c 2014-10-14 kinaba: double best = 0.0; 74d1f45c5c 2014-10-14 kinaba: 74d1f45c5c 2014-10-14 kinaba: for(int s=0; s+1<N; ++s) 74d1f45c5c 2014-10-14 kinaba: for(int t=s+1; t+1<N; ++t) 74d1f45c5c 2014-10-14 kinaba: { 74d1f45c5c 2014-10-14 kinaba: // a d[s] + b = d[t] 74d1f45c5c 2014-10-14 kinaba: // a d[s+1] + b = d[t+1] 74d1f45c5c 2014-10-14 kinaba: // a r[s] + c = r[t] 74d1f45c5c 2014-10-14 kinaba: // a r[s+1] + c = r[t+1] 74d1f45c5c 2014-10-14 kinaba: 74d1f45c5c 2014-10-14 kinaba: double a = (date[t+1]-date[t]) / double(date[s+1] - date[s]); 74d1f45c5c 2014-10-14 kinaba: double b = date[t] - a*date[s]; 74d1f45c5c 2014-10-14 kinaba: double c = rating[t] - a*rating[s]; 74d1f45c5c 2014-10-14 kinaba: 74d1f45c5c 2014-10-14 kinaba: double l1=0.0, l2=0.0; 74d1f45c5c 2014-10-14 kinaba: for(int k=1; t+k<N; ++k) { 74d1f45c5c 2014-10-14 kinaba: if( !eq(a*date[s+k]+b, date[t+k]) ) break; 74d1f45c5c 2014-10-14 kinaba: if( !eq(a*rating[s+k]+c, rating[t+k]) ) break; 74d1f45c5c 2014-10-14 kinaba: l1 += sqrt(pow(date[s+k]-date[s+k-1],2) + pow(rating[s+k]-rating[s+k-1],2)); 74d1f45c5c 2014-10-14 kinaba: l2 += sqrt(pow(date[t+k]-date[t+k-1],2) + pow(rating[t+k]-rating[t+k-1],2)); 74d1f45c5c 2014-10-14 kinaba: } 74d1f45c5c 2014-10-14 kinaba: best = max(best, max(l1, l2)); 74d1f45c5c 2014-10-14 kinaba: } 74d1f45c5c 2014-10-14 kinaba: return best; 74d1f45c5c 2014-10-14 kinaba: } 74d1f45c5c 2014-10-14 kinaba: 74d1f45c5c 2014-10-14 kinaba: double eq(double x, int v) { 74d1f45c5c 2014-10-14 kinaba: return int(floor(x+0.5)) == v; 74d1f45c5c 2014-10-14 kinaba: } 74d1f45c5c 2014-10-14 kinaba: }; 74d1f45c5c 2014-10-14 kinaba: 74d1f45c5c 2014-10-14 kinaba: // BEGIN CUT HERE 74d1f45c5c 2014-10-14 kinaba: #include <ctime> 74d1f45c5c 2014-10-14 kinaba: double start_time; string timer() 74d1f45c5c 2014-10-14 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 74d1f45c5c 2014-10-14 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 74d1f45c5c 2014-10-14 kinaba: { os << "{ "; 74d1f45c5c 2014-10-14 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 74d1f45c5c 2014-10-14 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 74d1f45c5c 2014-10-14 kinaba: void verify_case(const double& Expected, const double& Received) { 74d1f45c5c 2014-10-14 kinaba: bool ok = (abs(Expected - Received) < 1e-9); 74d1f45c5c 2014-10-14 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 74d1f45c5c 2014-10-14 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 74d1f45c5c 2014-10-14 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 74d1f45c5c 2014-10-14 kinaba: #define END verify_case(_, SimilarRatingGraph().maxLength(date, rating));} 74d1f45c5c 2014-10-14 kinaba: int main(){ 74d1f45c5c 2014-10-14 kinaba: 74d1f45c5c 2014-10-14 kinaba: CASE(0) 74d1f45c5c 2014-10-14 kinaba: int date_[] = {1,2,4,8,16,32}; 74d1f45c5c 2014-10-14 kinaba: vector <int> date(date_, date_+sizeof(date_)/sizeof(*date_)); 74d1f45c5c 2014-10-14 kinaba: int rating_[] = {1,2,4,8,16,32}; 74d1f45c5c 2014-10-14 kinaba: vector <int> rating(rating_, rating_+sizeof(rating_)/sizeof(*rating_)); 74d1f45c5c 2014-10-14 kinaba: double _ = 42.42640687119285; 74d1f45c5c 2014-10-14 kinaba: END 74d1f45c5c 2014-10-14 kinaba: CASE(1) 74d1f45c5c 2014-10-14 kinaba: int date_[] = {81,104,120,124,134,137}; 74d1f45c5c 2014-10-14 kinaba: vector <int> date(date_, date_+sizeof(date_)/sizeof(*date_)); 74d1f45c5c 2014-10-14 kinaba: int rating_[] = {1866,2332,2510,2678,2876,3002}; 74d1f45c5c 2014-10-14 kinaba: vector <int> rating(rating_, rating_+sizeof(rating_)/sizeof(*rating_)); 74d1f45c5c 2014-10-14 kinaba: double _ = 168.04761230080004; 74d1f45c5c 2014-10-14 kinaba: END 74d1f45c5c 2014-10-14 kinaba: CASE(2) 74d1f45c5c 2014-10-14 kinaba: int date_[] = {10,11,13,15,19}; 74d1f45c5c 2014-10-14 kinaba: vector <int> date(date_, date_+sizeof(date_)/sizeof(*date_)); 74d1f45c5c 2014-10-14 kinaba: int rating_[] = {10,14,15,23,25}; 74d1f45c5c 2014-10-14 kinaba: vector <int> rating(rating_, rating_+sizeof(rating_)/sizeof(*rating_)); 74d1f45c5c 2014-10-14 kinaba: double _ = 12.7183472062349; 74d1f45c5c 2014-10-14 kinaba: END 74d1f45c5c 2014-10-14 kinaba: CASE(3) 74d1f45c5c 2014-10-14 kinaba: int date_[] = {1,2,3,4}; 74d1f45c5c 2014-10-14 kinaba: vector <int> date(date_, date_+sizeof(date_)/sizeof(*date_)); 74d1f45c5c 2014-10-14 kinaba: int rating_[] = {1700,1800,1750,1850}; 74d1f45c5c 2014-10-14 kinaba: vector <int> rating(rating_, rating_+sizeof(rating_)/sizeof(*rating_)); 74d1f45c5c 2014-10-14 kinaba: double _ = 100.00499987500625; 74d1f45c5c 2014-10-14 kinaba: END 74d1f45c5c 2014-10-14 kinaba: CASE(4) 74d1f45c5c 2014-10-14 kinaba: int date_[] = {1,2,3,4}; 74d1f45c5c 2014-10-14 kinaba: vector <int> date(date_, date_+sizeof(date_)/sizeof(*date_)); 74d1f45c5c 2014-10-14 kinaba: int rating_[] = {1,4,9,16}; 74d1f45c5c 2014-10-14 kinaba: vector <int> rating(rating_, rating_+sizeof(rating_)/sizeof(*rating_)); 74d1f45c5c 2014-10-14 kinaba: double _ = 0.0; 74d1f45c5c 2014-10-14 kinaba: END 74d1f45c5c 2014-10-14 kinaba: CASE(5) 74d1f45c5c 2014-10-14 kinaba: int date_[] = {5,11,25,58,92,162,255,350,458,566,677,792,919,1051,1189,1331,1489,1673,1882,2093,2315,2541,2771,3012,3254,3524,3797,4087,4379,4675,4973,5278,5588,5904,6225,6550,6888,7249,7612,8018,8428,8847,9267,9688,10109,10530,10964,11407,11870,12340,12811,13288,13768,14249,14734,15242,15774,16306,16847,17400,17966,18533,19108,19692,20278,20871,21471,22074,22679,23297,23916,24553,25190,25829,26472,27135,27814,28497,29181,29865,30555,31272,31994,32729,33487,34246,35005,35764,36537,37326,38119,38913,39725,40538,41360,42185,43010,43840,44671,45509,46350,47205,48063,48932,49807,50691,51577,52464,53289,54119,54950,55788,56629,57484,58342,59211,60086,60970,61856,62743,63568,64398,65388}; 74d1f45c5c 2014-10-14 kinaba: vector <int> date(date_, date_+sizeof(date_)/sizeof(*date_)); 74d1f45c5c 2014-10-14 kinaba: int rating_[] = {1505,1462,1436,1416,1463,1421,1411,1450,1497,1465,1423,1394,1391,1367,1358,1323,1310,1279,1268,1279,1311,1342,1359,1387,1414,1376,1424,1382,1373,1335,1359,1318,1275,1266,1227,1203,1168,1163,1184,1144,1169,1207,1250,1235,1209,1162,1124,1148,1168,1202,1190,1155,1179,1194,1195,1195,1203,1240,1218,1245,1220,1190,1208,1180,1182,1148,1139,1126,1152,1159,1147,1158,1112,1091,1101,1116,1123,1086,1126,1110,1128,1085,1132,1145,1135,1140,1117,1081,1120,1131,1081,1032,1071,1102,1071,1065,1068,1027,980,947,987,968,959,980,990,974,1003,996,999,958,911,878,918,899,890,911,921,905,934,927,930,889,844}; 74d1f45c5c 2014-10-14 kinaba: vector <int> rating(rating_, rating_+sizeof(rating_)/sizeof(*rating_)); 74d1f45c5c 2014-10-14 kinaba: double _ = 11940.179271014536; 74d1f45c5c 2014-10-14 kinaba: END 74d1f45c5c 2014-10-14 kinaba: /* 74d1f45c5c 2014-10-14 kinaba: CASE(6) 74d1f45c5c 2014-10-14 kinaba: int date_[] = ; 74d1f45c5c 2014-10-14 kinaba: vector <int> date(date_, date_+sizeof(date_)/sizeof(*date_)); 74d1f45c5c 2014-10-14 kinaba: int rating_[] = ; 74d1f45c5c 2014-10-14 kinaba: vector <int> rating(rating_, rating_+sizeof(rating_)/sizeof(*rating_)); 74d1f45c5c 2014-10-14 kinaba: double _ = ; 74d1f45c5c 2014-10-14 kinaba: END 74d1f45c5c 2014-10-14 kinaba: CASE(7) 74d1f45c5c 2014-10-14 kinaba: int date_[] = ; 74d1f45c5c 2014-10-14 kinaba: vector <int> date(date_, date_+sizeof(date_)/sizeof(*date_)); 74d1f45c5c 2014-10-14 kinaba: int rating_[] = ; 74d1f45c5c 2014-10-14 kinaba: vector <int> rating(rating_, rating_+sizeof(rating_)/sizeof(*rating_)); 74d1f45c5c 2014-10-14 kinaba: double _ = ; 74d1f45c5c 2014-10-14 kinaba: END 74d1f45c5c 2014-10-14 kinaba: */ 74d1f45c5c 2014-10-14 kinaba: } 74d1f45c5c 2014-10-14 kinaba: // END CUT HERE