Codeforces 79B Colorful Field

B. Colorful Field
 

Fox Ciel saw a large field while she was on a bus. The field was a n × m rectangle divided into 1 × 1 cells. Some cells were wasteland, and other each cell contained crop plants: either carrots or kiwis or grapes.

After seeing the field carefully, Ciel found that the crop plants of each cell were planted in following procedure:

  • Assume that the rows are numbered 1 to n from top to bottom and the columns are numbered 1 to m from left to right, and a cell in row i and column j is represented as (i, j).
  • First, each field is either cultivated or waste. Crop plants will be planted in the cultivated cells in the order of (1, 1) → ... → (1, m) → (2, 1) → ... → (2, m) → ... → (n, 1) → ... → (n, m). Waste cells will be ignored.
  • Crop plants (either carrots or kiwis or grapes) will be planted in each cell one after another cyclically. Carrots will be planted in the first cell, then kiwis in the second one, grapes in the third one, carrots in the forth one, kiwis in the fifth one, and so on.

 The following figure will show you the example of this procedure. Here, a white square represents a cultivated cell, and a black square represents a waste cell.

 Now she is wondering how to determine the crop plants in some certain cells.

Input

In the first line there are four positive integers n, m, k, t (1 ≤ n ≤ 4· 104, 1 ≤ m ≤ 4· 104, 1 ≤ k ≤ 103, 1 ≤ t ≤ 103), each of which represents the height of the field, the width of the field, the number of waste cells and the number of queries that ask the kind of crop plants in a certain cell.

Following each k lines contains two integers a, b (1 ≤ a ≤ n, 1 ≤ b ≤ m), which denotes a cell (a, b) is waste. It is guaranteed that the same cell will not appear twice in this section.

Following each t lines contains two integers i, j (1 ≤ i ≤ n, 1 ≤ j ≤ m), which is a query that asks you the kind of crop plants of a cell (i, j).

Output

For each query, if the cell is waste, print Waste. Otherwise, print the name of crop plants in the cell: either Carrots or Kiwis or Grapes.

Sample test(s)
Input
4 5 5 6
4 3
1 3
3 3
2 5
3 2
1 3
1 4
2 3
2 4
1 1
1 1














 
 
 
 
Output
Waste
Grapes
Carrots
Kiwis
Carrots
Carrots

 

 





Analysis

之前思路 大数组[40000][40000]    采用数据位标记 Waste :0   Carrots:1 Kiwis: 2 Grapes: 3   然后就是简单的查询 。但结果显而易见 ,各种TLE:Time : 2000ms   Memory:    7300k

 仔细想想之后的思路: 只需记录 Waste 的位置 ,然后简单排序 sorting 。 最后就是查询。

View Code
 1 //---------------Accepted------------------------
 2 //            Time: 50  Memory : 1400K
 3 
 4 
 5 #include <stdio.h>
 6 #include <stdlib.h>
 7 #include <string.h>
 8 #include <math.h>
 9 #include <ctype.h> 
10 
11 int main()
12 {
13     int n,m,k1,t,i,j,c,a,b;
14     int k[1000][2]={0};
15     scanf("%d%d%d%d",&n,&m,&k1,&t);
16     for(i=0;i<k1;i++)
17          scanf("%d%d",&k[i][0],&k[i][1]);
18     for(i=0;i<k1-1;i++)
19       for(j=i+1;j<k1;j++)
20       {
21         if(k[i][0]>k[j][0])
22         {
23            int tempa=k[i][0],tempb=k[i][1];
24            k[i][0]=k[j][0];
25            k[i][1]=k[j][1];
26            k[j][0]=tempa;
27            k[j][1]=tempb;
28         }
29         if(k[i][0]==k[j][0])
30         {
31                if(k[i][1]>k[j][1])  
32                {
33                    int tempa=k[i][0],tempb=k[i][1];
34                    k[i][0]=k[j][0];
35                    k[i][1]=k[j][1];
36                    k[j][0]=tempa;
37                    k[j][1]=tempb;
38                }
39         }
40       }
41 //      for(i=0;i<k1;i++)
42 //        printf("%d %d\n",k[i][0],k[i][1]);   
43     while(t--)
44     {
45         int xx,yy,count=0,countb=0;
46         scanf("%d%d",&xx,&yy);
47         int a=(xx-1)*m+yy;
48         for(i=0;i<k1;i++)
49         { 
50             if((k[i][0]-1)*m+k[i][1]<a)
51                count++;
52              if(k[i][0]==xx&&k[i][1]==yy)
53                 countb=1;
54         }
55        if(countb==1)
56           printf("Waste\n");
57        else
58        {
59         int b=(a-count)%3;
60         switch(b)
61         {
62            case 1: printf("Carrots\n");break;
63            case 2: printf("Kiwis\n");break;
64            case 0: printf("Grapes\n");break;
65         }
66        }
67     }    
68     return 0;
69 }

 

本文还在本人百度空间http://hi.baidu.com/jackie_lee1989/item/c993ac2ed91b66d00f37f952上发布。

posted @ 2012-07-25 17:34  Double_win  阅读(360)  评论(0编辑  收藏  举报