Notes:
- This method should almost always be the first algorithm/solution you consider.
- the order of the moves does not matter: b(ase)k=>kb
- 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
在这里=-=坑了一次