跳到正文

题解

题解:P12667 [KOI 2023 Round 2] 傻瓜锁

zhaopengyao123约 2 分钟
原文
文章信息与版本

文章信息

发布
2025年6月1日
收录
2025年12月12日
更新
2026年11月11日
篇幅
约 2 分钟
获赞
1
题目
P12667

版本

  1. v1当前

first, 任何使用 long long、unsigned long long 甚至 double 的暴力 ×2 做法肯定是过不了这题的。long double都 not OK !!!

我们可以开一个结构体,一个存数字 a,一个存指数 n。那么 this number 就能表示为 a×2

当数字读入进来,我们先将它的 tow 进制下最 high 位 1 对齐到 1<<20 这位(方便比 big and small ),然后计算它的指数(原来最 high 位的 1 是 2 的几次方就存几)。

由于它要 bigger than 前一个数,所以它最终的指数肯定要大于等于前一个数的指数,所以我们先加上这一段。

此时这个数可能还 smaller than 前面那个数。不过这好办,再乘一次 tow 就行了。

C++
#include<bits/stdc++.h>
using std::cout;
static char buf[1048576], * pa(buf), * pb(buf);
#define gc pa == pb && (pb = (pa = buf) + fread(buf, 1, 1048576, stdin), pa == pb) ? EOF : *pa++
int cnt=0,n,x;
long long ans;
struct node{
    int a,b;
}a[250005];
char ch;
inline void read(int&x) {
  x=0;
  ch=gc;
  while (!isdigit(ch)) {
    ch = gc;
  }
  while (isdigit(ch)) {
    x = (x << 3) + (x << 1) + (ch ^ 48);
    ch = gc;
  }
}
int main() {
    read(n);
    for (int i = 0; i < n; i++) {
        read(a[i].a);
        cnt=10;
        while(a[i].a>>cnt)
          ++cnt;
        a[i].a<<=(20-cnt)+2,a[i].b=cnt;
        if(!i)continue;
        cnt-=10;
        if(a[i-1].b>a[i].b)
          ans+=a[i-1].b-a[i].b,a[i].b=a[i-1].b;
        if(a[i-1].b==a[i].b&&a[i-1].a>a[i].a)
          a[i].b++,ans++;
    }
    cout<<ans;

}

参考神犇 @complete_binary_tree 在文章广场的题解。

原文
存档评论1 条抓取于