Advertisement
chunkyguy

Recover the sequence

Jan 30th, 2012
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  main.m
  3. //  RecoverSequence
  4. //
  5. //  Created by Sidharth Juyal on 29/01/12.
  6. //  Copyright (c) 2012 whackylabs. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10. //#define D_BUG 1
  11. #define PATH @"/Volumes/FreeAgent GoFlex Drive/Downloads/recover_the_sequence.txt"
  12. typedef struct{
  13.     int low, high;
  14. }Marks;
  15.  
  16. static inline Marks MarksMake(int l, int h){
  17.     Marks m;
  18.     m.low = l;
  19.     m.high = h;
  20.     return m;
  21. }
  22.  
  23. static inline int MarksDistance(Marks m){
  24.     return m.high - m.low;
  25. }
  26.  
  27. static inline NSString *MarksDesc(Marks m){
  28.     return [NSString stringWithFormat:@"%d,%d",m.low,m.high];
  29. }
  30.  
  31. @interface Solver: NSObject{
  32.     int *indices_;
  33.     int count_;
  34.     int *debug_;
  35.     int debug_count_;
  36. }
  37. -(Marks)mergeSortWithMarks:(Marks)m;
  38. -(void) mergeArrayOne:(Marks )one two:(Marks )two;
  39. -(int)checksum;
  40. -(id)initWithCount:(int)n debugStr:(NSString *)dstr;
  41. @end
  42.  
  43. @implementation Solver
  44.  
  45. -(Marks)mergeSortWithMarks:(Marks)m{
  46.     int n = m.high - m.low + 1;
  47.     if (n <= 1)
  48.         return m;
  49.    
  50.     int mid = n/2;
  51.     Marks first_half = [self mergeSortWithMarks:MarksMake(m.low, m.low+mid-1)];
  52.     Marks second_half = [self mergeSortWithMarks:MarksMake(m.low+mid, m.high)];
  53.     [self mergeArrayOne:first_half two:second_half];                        //merge(first_half, second_half);
  54.     return m;
  55. }
  56.  
  57. -(void) mergeArrayOne:(Marks )one two:(Marks )two{ 
  58.     int arr1p = one.low;
  59.     int arr2p = two.low;
  60.     int *cache = (int *)malloc(sizeof(int) * (MarksDistance(one)+MarksDistance(two)));
  61.     int cachep = 0;
  62.     while (arr1p <= one.high && arr2p <= two.high){
  63.         int d_val = debug_[debug_count_++];
  64.         if(d_val == 0){
  65.             cache[cachep++] = indices_[arr1p++];
  66.         }else{
  67.             cache[cachep++] = indices_[arr2p++];
  68.         }
  69.     }
  70.    
  71.     while (arr1p <= one.high) {
  72.         cache[cachep++] = indices_[arr1p++];
  73.     }
  74.     while (arr2p <= two.high) {
  75.         cache[cachep++] = indices_[arr2p++];
  76.     }
  77.    
  78.     arr1p = one.low;
  79.     arr2p = two.low;
  80.     cachep = 0;
  81.     while (arr1p <= one.high) {
  82.         indices_[arr1p++] = cache[cachep++];
  83.     }
  84.     while (arr2p <= two.high) {
  85.         indices_[arr2p++] = cache[cachep++];
  86.     }
  87.     free(cache);
  88. }
  89.  
  90. -(int)checksum{
  91. #ifdef D_BUG
  92.     printf("Indices:\n");
  93. #endif
  94.     int result = 1;
  95.     int i,j;
  96.     int *arr = (int *)malloc(sizeof(int) * count_);
  97.     for(int i = 0; i < count_; i++)
  98.         arr[i] = i+1;
  99.     for(i = 0; i < count_; i++){
  100.         for(j = 0; j < count_; j++)
  101.             if(i == indices_[j])
  102.                 break;
  103. #ifdef D_BUG
  104.         printf("arr[%d] = %d ",i,arr[j]);
  105. #endif
  106.         result = (31 * result + arr[j]) % 1000003;
  107.     }
  108. #ifdef D_BUG
  109.     printf("\n");
  110. #endif
  111.     free(arr);
  112.     return result;
  113. }
  114.  
  115. -(id)initWithCount:(int)n debugStr:(NSString *)dstr{
  116.     self = [super init];
  117.     if(self){
  118.         count_ = n;
  119.         indices_ = (int *)malloc(sizeof(int) * count_);
  120.         for(int i = 0; i < n; i++)
  121.             indices_[i] = i;
  122.         debug_ = (int *)malloc(sizeof(int)*[dstr length]);
  123.         for(int i = 0; i < [dstr length]; i++)
  124.             debug_[i] = [dstr characterAtIndex:i] == '1'?0:1;
  125.         debug_count_ = 0;
  126.         [self mergeSortWithMarks:MarksMake(0, count_-1)];
  127.     }
  128.     return self;
  129. }
  130.  
  131. -(void)dealloc{
  132.     free(indices_);
  133.     free(debug_);
  134.     [super dealloc];
  135. }
  136.  
  137. @end
  138.  
  139. int main (int argc, const char * argv[]){
  140.     @autoreleasepool {
  141.         NSString *str = [[NSString alloc] initWithContentsOfFile:PATH encoding:NSUTF8StringEncoding error:nil];
  142.         NSArray *words = [str componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
  143.         int w_no = 0;
  144.         int cases = [[words objectAtIndex:w_no++] intValue];
  145.         for(int c = 0; c < cases; c++){
  146.             int n = [[words objectAtIndex:w_no++] intValue];
  147.             Solver *s = [[Solver alloc] initWithCount:n debugStr:[words objectAtIndex:w_no++]];
  148.             int checksum = [s checksum];
  149.             [s release];
  150.             printf("Case #%d: %d\n",c+1,checksum);
  151.         }
  152.         [str release];
  153.     }
  154.     return 0;
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement