Codeforces Round #142 (Div. 2)

     这次CF做的真是太生疏了,好久没做比赛了,果断CF被虐惨了。。。A题裸贪心,因为对C++的sort还不大熟,敲了12分钟,然后更悲剧的是结构体里面的一个符号写错了,让我怀疑算法正确性了。。。这场注定要悲剧了,20分钟才过了第一个题,然后B题,求判断是否是含有3个因子,前些天刚思考一个问题,那就是完全平方数的因子一定是奇数个,所以很快就发现就是素数的完全平方啊,自己又SB了,第一次,筛选素数敲错,第二次,盲查数据,发现1错了。。。要不是这样又要被查爆了,磕磕绊绊来到了C,题意还是挺好理解(注意特殊情况),然后觉得可做,感觉像是二分,一开始算法就想错了,我把第一行定住,然后再去找,肯定错了。。。然后悲剧了一个半小时,总之,悲剧啊。。。

贴一下代码:

A. Dragons

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 using namespace std;
 6 #define N 10000000
 7 struct nn
 8 {
 9     int x;
10     int y;
11 } p[100001];
12 int cmp(const nn &a, const nn &b)
13 {
14     if(a.x < b.x)
15         return 1;
16     else if(a.x > b.x)
17         return 0;
18     else if(a.y > b.y)
19         return 1;
20     else
21         return 0;
22 }
23 int main()
24 {
25     int i,j,n,s,z;
26     scanf("%d%d",&s,&n);
27     for(i = 1; i <= n; i ++)
28     {
29         scanf("%d%d",&p[i].x,&p[i].y);
30     }
31     sort(p+1,p+n+1,cmp);
32     z = 1;
33     for(i = 1; i <= n; i ++)
34     {
35         if(s > p[i].x)
36             s = s+p[i].y;
37         else
38         {
39             z = 0;
40             break;
41         }
42     }
43     if(z)
44         printf("YES\n");
45     else
46         printf("NO\n");
47     return 0;
48 
49 }

B. T-primes

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 using namespace std;
 7 #define ll __int64
 8 #define N 1000000
 9 int p[1000001];
10 int main()
11 {
12     int t,i,j;
13     ll n,ans;
14     for(i = 2;i <= N;i ++)
15     {
16         if(!p[i])
17         {
18             for(j = i+i;j <= N;j += i)
19             {
20                 p[j] = 1;
21             }
22         }
23     }
24     scanf("%d",&t);
25     while(t--)
26     {
27         scanf("%I64d",&n);
28         if(n == 1)
29         {
30             printf("NO\n");
31             continue;
32         }
33         ans = sqrt(n);
34         if(ans*ans == n&&!p[ans])
35         {
36             printf("YES\n");
37         }
38         else
39         printf("NO\n");
40     }
41     return 0;
42 }

C. Shifts

这个题目的正解应该是枚举m,然后计算去二分求上界和下界,取小即可,最后注意从反方向翻转过来的,特判,WA到死了。。。

代码多次乱搞后,终于A了。

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <cmath>
  5 #include <algorithm>
  6 using namespace std;
  7 #define ll __int64
  8 #define N 100000000
  9 int que[101][10001],o[101];
 10 char p[101][10001];
 11 int Abs(int x)
 12 {
 13     return x > 0 ? x:-x;
 14 }
 15 int Min(int a,int b)
 16 {
 17     return a > b ? b:a;
 18 }
 19 int main()
 20 {
 21     int n,m,i,j,str,end,mid,ans,t1,t2,t,z;
 22     scanf("%d%d",&n,&m);
 23     for(i = 0; i <= n-1; i ++)
 24     {
 25         scanf("%s",p[i]);
 26     }
 27     z = 0;
 28     for(i = 0;i <= n-1;i ++)
 29     {
 30         o[i] = 0;
 31         for(j = 0;j <= m-1;j ++)
 32         {
 33             if(p[i][j] == '1')
 34             {
 35                 que[i][o[i]++] = j;//记录下标
 36             }
 37         }
 38         if(o[i] == 0)
 39         {
 40             z = 1;
 41             break;
 42         }
 43     }
 44     if(z)
 45     {
 46         printf("-1\n");
 47         return 0;
 48     }
 49     ans = N;
 50     for(j = 0;j < m;j ++)
 51     {
 52         t = 0;
 53         for(i = 0;i < n;i ++)
 54         {
 55             str = 0;
 56             end = o[i]-1;
 57             while(str < end)//求上界
 58             {
 59                 mid = (str+end+1)/2;
 60                 if(que[i][mid] > j)
 61                 {
 62                     end = mid-1;
 63                 }
 64                 else
 65                 {
 66                     str = mid;
 67                 }
 68             }
 69             t1 = Abs(que[i][end]-j);
 70             if(j < m/2)//特判
 71             {
 72                 if(t1 > Abs(que[i][o[i]-1]-j-m))
 73                 t1 = Abs(que[i][o[i]-1]-j-m);
 74             }
 75             else
 76             {
 77                 if(t1 > Abs(que[i][0]+m-j))
 78                 t1 = Abs(que[i][0]+m-j);
 79             }
 80             str = 0;
 81             end = o[i]-1;
 82             while(str < end)//求下界
 83             {
 84                 mid = (str+end)/2;
 85                 if(que[i][mid] < j)
 86                 {
 87                     str = mid+1;
 88                 }
 89                 else
 90                 {
 91                     end = mid;
 92                 }
 93             }
 94             t2 = Abs(que[i][str]-j);
 95             t += Min(t1,t2);
 96         }
 97         if(ans > t)
 98         ans = t;
 99     }
100     printf("%d\n",ans);
101     return 0;
102 }

 

 

 

 

   

posted @ 2012-10-02 13:41  Naix_x  阅读(175)  评论(0编辑  收藏  举报