2010/02/01

Hartals

計算在N天裡的因罷工而浪費的天數,假日不算。反正就是一個一個比較那天有沒有罷工就對了。

速度雖然比前一個人快一點,但是花的記憶體卻是快五倍...

import java.util.LinkedList;

public class JAVA {
      // 輸入緩衝區 (緩衝空間需要依照題目調整)
      public static byte[] cinbuf = new byte[1024];
     
      // 讀取一個單字 (英文姓名包含空白時請不要用)
      public static String readToken() {
        int offset = 0;
        int bytedata = -1;
        
        try {
          // 略過非單字的字元 '\t','\n','\r',' '
          bytedata = System.in.read();
          while(bytedata==9||bytedata==10||bytedata==13||bytedata==32) {
            bytedata = System.in.read();
          }
     
          // 載入單字的字元
          while(bytedata!=-1) {
            if(bytedata==9||bytedata==10||bytedata==13||bytedata==32) {
              break;
            } else {
              cinbuf[offset++] = (byte)bytedata;
            }
            bytedata = System.in.read();
          }
        } catch(Exception e) {}
        
        if(offset+bytedata==-1) return null; // 串流結束
        return new String(cinbuf,0,offset);
      }
      
      // 讀取一行
      public static String readLine() {
        int offset = 0;
        int bytedata = -1;
        
        try {
          // 載入整行
          bytedata = System.in.read();
          while(bytedata!=-1) {
            if(bytedata==10) {
              break;
            } else {
              cinbuf[offset++] = (byte)bytedata;
            }
            bytedata = System.in.read();
          }
        } catch(Exception e) {}
     
        if(offset+bytedata==-1) return null; // 串流結束
        if(cinbuf[offset-1]=='\r') offset--; // window 要去除 '\r' 字元
        return new String(cinbuf,0,offset);
      }
      
      // 轉成 int 型態 (比 Integer.parseInt() 快)
      public static int parseInt(String s) {
        int i;
        int mul = 10;
        int value = s.charAt(s.length()-1)-48;
        
        for(i=s.length()-2;i>=0;i--) {
          value += (s.charAt(i)-48)*mul;
          mul *= 10;
        }
        
        return value;
      }
      
    public static void main(String[] args) {
        
        int testCase = parseInt(readLine());
        
        for(int i = 0; i < testCase; i++) {
            
            int days = parseInt(readLine());
            
            boolean[] calendar = new boolean[days+1]; 
            
            int parties = parseInt(readLine());
            
            LinkedList<Integer> partiesList = new LinkedList<Integer>();
            
            for(int j = 0; j < parties; j++) {
                Integer tmp = parseInt(readLine());
                partiesList.add(tmp);
            }
            
            // start processing.
            for(int j = 0; j < partiesList.size(); j++) {
            
                int now_party = partiesList.get(j);
                
                if(now_party == 0)
                    continue;
                
                for(int k = 1; k < calendar.length; k++) {
                    if(k % now_party == 0) {
                        calendar[k] = true;
                    }
                }
            }
            
            int count = 0;
            // start counting strike days on non-yasumi day
            for(int j = 1; j < calendar.length; j++) {
                
                if(!isYasumi(j) && calendar[j] == true) {
                    count++;
                }
            }
            
            System.out.println(count);
            
        }
        
    }
    
    public static boolean isYasumi(int day) {
        
        // first day is Sunday 
        if(day == 0)
            return true;
        
        // Sunday
        if(day % 7 == 0)
            return true;
        
        // Saturday
        while(day > 7) {
            day -= 7;
        }
        
        if(day == 6)
            return true;
        
        return false;
    }

}

No comments:

Post a Comment