事情起因
2023.2.4模拟赛, T3
该题当时考场上真的想不出来什么正解,于是就开始打暴力
第一次提交
结果如下图
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
inline int read()
{
int x = 0, f = 1;
char ch = getchar();
while(ch < '0' || ch > '9')
{
if(ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9')
{
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
const int N = 5e5 + 10;
int a[N];
int n, m, t;
int find(int len)
{
int l, cnt = 0;
for(int i = 1; i <= n; i ++ )
{
if(a[i] == 1) cnt = 0;
if(!cnt && a[i] == 0) l = i;
if(a[i] == 0) cnt ++;
if(cnt == len) return l;
}
return 0;
}
int main()
{
#ifdef LOCAL
freopen("in.in", "r", stdin);
freopen("out.out", "w", stdout);
#endif
n = read(), m = read();
while(m -- )
{
int op = read(), x = read(), y;
if(op == 1)
{
int l = find(x);
if(l != 0)
for(int i = l; i <= l + x - 1; i ++ )
a[i] = 1;
cout << l << endl;
}
else
{
y = read();
for(int i = x; i < x + y; i ++ )
a[i] = 0;
}
}
return 0;
}
PS: 本人有加快读的习惯
只拿了一半分(说实话我也没想到能拿一半分)
改代码
拿了一半分可不行(确信)
然后我就开始着力改一些细节
首先把所有的for循环中加入register, 函数前加上inline
于是代码变成了
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
inline int read()
{
int x = 0, f = 1;
char ch = getchar();
while(ch < '0' || ch > '9')
{
if(ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9')
{
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
const int N = 5e5 + 10;
int a[N];
int n, m, t;
inline int find(int len)
{
int l, cnt = 0;
for(register int i = 1; i <= n; i ++ )
{
if(a[i] == 1) cnt = 0;
if(!cnt && a[i] == 0) l = i;
if(a[i] == 0) cnt ++;
if(cnt == len) return l;
}
return 0;
}
int main()
{
#ifdef LOCAL
freopen("in.in", "r", stdin);
freopen("out.out", "w", stdout);
#endif
n = read(), m = read();
while(m -- )
{
int op = read(), x = read(), y;
if(op == 1)
{
int l = find(x);
if(l != 0)
for(register int i = l; i <= l + x - 1; i ++ )
a[i] = 1;
cout << l << endl;
}
else
{
y = read();
for(register int i = x; i < x + y; i ++ )
a[i] = 0;
}
}
return 0;
}
分数+8
我狂喜直接(bushi)
但还是没拿更高的分(真贪啊)
接着改
改了一些小地方, 比如什么
int a = read()
变成了int a(read())
i ++
变为++ i
改cout为printf
但是还是过不了别的点
然后进行了一些位运算优化
比如把x == y
改为!(x ^ y)
这种异或
最终
又2个点!
直接干到82分
代码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
inline int read()
{
int x = 0, f = 1;
char ch = getchar();
while(ch < '0' || ch > '9')
{
if(ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9')
{
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
inline void write(int x)
{
if(x<0){
putchar('-');
x=-x;
}
if(x>9) write(x/10);
putchar(x%10+'0');
}
const int N = 5e4 + 9;
int a[N];
int n, m, t;
inline int find(int len)
{
int l, cnt = 0;
for(register int i(1); i <= n; ++ i )
{
if(a[i]) cnt = 0;
if(!cnt && !a[i]) l = i;
if(!a[i]) cnt ++;
if(!(cnt ^ len)) return l;
}
return 0;
}
int main()
{
#ifdef LOCAL
freopen("in.in", "r", stdin);
freopen("out.out", "w", stdout);
#endif
n = read(), m = read();
while(m -- )
{
int op(read()), x(read()), y;
if(!(op ^ 1))
{
int l(find(x));
if(l)
for(register int i(l); i <= l + x - 1; ++ i )
a[i] = 1;
printf("%d\n", l);
}
else
{
y = read();
int t(x + y);
for(register int i(x); i < t; ++ i )
a[i] = 0;
}
}
return 0;
}
还没完
在写这篇的时候, 我突然想到LYT昨晚说的:
O2跑的飞快
然后我就去测试了下
????????????
直接起飞了就
(怪不得考试不加O2)
标签:typedef,ch,int,long,while,卡常,getchar From: https://www.cnblogs.com/crimsonawa/p/17093535.html