usaco 1.2 complete search

26 篇文章 0 订阅

Training Test

Notes:

  1. This method should almost always be the first algorithm/solution you consider.
  2. the order of the moves does not matter: b(ase)k=>kb
  3. doing each move m times is the same as doing it no times, that no move will be done more than m-1 times. Thus, there are only (k%m)b

1.间隔题

sort一遍扫过去.
(虽然一开始想线段树的= =

qsort

int milkcmp(const void *va, const void *vb)
{
    Milking *a, *b;

    a = (Milking*)va;
    b = (Milking*)vb;

    if(a->begin > b->begin)
    return 1;
    if(a->begin < b->begin)
    return -1;
    return 0;
}

qsort(milking, nmilking, sizeof(Milking), milkcmp);
int eventcmp (const event *a, const event *b){
    if (a->seconds != b->seconds)
      return (a->seconds - b->seconds); /* 300 before 500 */
    return (b->ss - a->ss); /* 1 (start) before -1 (stop) */
}

 qsort(events, num_events, sizeof(event),(int(*)(const void*, const void*)) eventcmp)

/*
qsort最基础版本:
int compare(const void *p, const void *q) { return *(int *)p-*(int *)q; }
*/

题解二

  • 标记每个时间点是beg还是eng后sort
  • 判断也非常有趣
for (i = 0; i < num_intervals; ++i){
  in >> events[2*i  ].seconds; events[2*i  ].ss = 1;
  in >> events[2*i+1].seconds; events[2*i+1].ss = -1;
}
...
for (i = 0; i < num_events; ++i){
  num_milkers += events[i].ss;
  if (!num_milkers && !was_none){
   /* there are suddenly no milkers. */
   ...
  }
  else if (num_milkers && was_none){
   /* there are suddenly milkers. */
   ...
   istart = events[i].seconds;
  }
}

2.旋转对称

180°错了一次
if (blocks[i][j]!=aim[n-i-1][n-j-1])
题解里面发现不仅可以(自己写的= =

bool five(){
    for (int i=0; i<n; i++)
        reverse(blocks[i].begin(), blocks[i].end());
    if (one()||two()||three())
        return true;
    return false;
}

还可以把旋转写成函数

Board rotate(Board b){
    Board nb;
    int r, c;
    nb.n = b.n;
    for(r=0; r<b.n; r++)
        for(c=0; c<b.n; c++)
            nb.b[c][b.n-1 - r] = b.b[r][c];
    return nb;
}

3字典找词

每个输出换一行= =2A

二分

if (charloc > 0) {
        for (int j=low; j <= high; j++){
            if (sol[charloc-1] == dict[j][charloc-1]) {
                low=j;
                while (sol[charloc-1] == dict[j][charloc-1])
                    j++;
                high=j;
                break;
            }
            if (j == high) return;//low?
        }
}
if (low > high) return;

4 Palindromic

平方进制回文

s[len] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[n%b];

5进制回文

strictly greater than S
在这里=-=坑了一次

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值