主编:yqx34
制作人员:yqx34、yyq3、Starlight_Footprint
5.5 比赛总结2
0 赛后感
大家一定不要学我啊!因为两个小细节(不开 long long 和不写 return )没了 80 分
Problem A - One Half
评价:NPC
考点:二分(Binary Search) & 前缀和(Prefix Sum)
一道看似复杂实则并不简单的题目
请不要把它的西格玛公式看的很复杂,我们先拆一下这个西格玛公式:
然后,我们就可以把那两个西格玛给扔了:
扔完西格玛拆完公式,就可以发现这道题目的本质了;实际上,题目是想让我们求对于每一个 与 ,取一个值 ,使得 符合上面的公式。
现在,我们就可以开始求解了:
NO.1 暴力
暴力求出 ,并枚举每一个 ,使得符合公式。时间复杂度 ,约等于 。
(不展示代码 反正也不会有人看 )
NO.2 暴力优化
暴力太慢了,可以维护一个值 ,存储这段区间(请注意这两个字)之和,少掉一层循环。时间复杂度
(同样不展示代码)
NO.3 二分在此!
正解二分
二分枚举 ,再用一层循环求解区间和(请再次注意这三个字),比暴力快了好多。时间复杂度
(依旧不展示代码(原因是实在没必要,反正马上到正解了))
NO.4 加入一点数学小优化
可以注意到, 一定大于 ,那么二分区间还可以再少一些。但时间复杂度几乎没变。
NO.5 前缀和大法好!!!
前缀和永远的神!
看到我提醒的区间了吗?
直接求前缀和, 求出区间和
综合代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 3;
int n,q,x,y,l,r,mid,ans;
long long a[N],s[N];
void Binary_Search()
{
l = x,r = n;
while (l <= r)
{
mid = l + (r - l) / 2;
if (s[mid] - s[x - 1] > (s[y] - s[x - 1]) / 2) ans = mid,r = mid - 1;
else l = mid + 1;
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n;
for (int i = 1;i <= n;s[i] = s[i - 1] + a[i],i++) cin >> a[i];
cin >> q;
while (q--)
{
cin >> x >> y;
Binary_Search();
cout << ans << "\n";
}
return 0;
}前缀和大法好!
Problem B - Buy an Integer
评价:NPC
考点:二分(Binary Search) | 数学(Maths)
一道比 T1 思路还简单,可细节多的题目
O(n)解法(暴力解法)
暴力虽烂,但可骗分!
代码:
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
int a,b,ans,num;
long long x,sum;
bool check(long long op)
{
num = 0,sum = 1ll * a * op;
while (op != 0) num++,op /= 10;
return (sum + 1ll * b * num <= x);
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> a >> b >> x;
for (int i = INF;i >= 1;i--) if (check(i))
{
cout << i << "\n";
return 0;
}
cout << "0\n";
return 0;
}竟然有 pts!(欸难道出题人不卡卡暴力的吗?)
O(logn)解法(二分解法)
二分枚举答案,尝试是否可以购买整数 :若可以,查找更大区间(尝试更优解);若不可,查找更小区间(尝试更小解)。
综合代码:
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
int a,b,l,r,mid,ans,num;
long long x,sum;
bool check(long long op)
{
num = 0,sum = 1ll * a * op;
while (op != 0) num++,op /= 10;
return (sum + 1ll * b * num <= x);
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
r = INF;
cin >> a >> b >> x;
while (l <= r)
{
mid = l + (r - l) / 2;
if (check(mid)) ans = mid,l = mid + 1;
else r = mid - 1;
}
cout << ans << "\n";
return 0;
}O(1)解法(数学解法)
若 固定,则有拆分得出的公式:
从大到小(必须得是这个,想想为什么)枚举 ,使得是否有 ,有则返回;无就继续判断。
综合代码:
#include <bits/stdc++.h>
using namespace std;
const long long N = 1e9;
long long a,b,x,l,r,t,maxn,num,ans;
long long pow10(int k)
{
t = 1;
while (k--) t *= 10;
return t;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> a >> b >> x;
for (int i = 1;i <= 10;i++)
{
if (i < 10) l = pow10(i - 1),r = pow10(i) - 1;
else l = r = N;
if (x < b * i) continue;
maxn = (x - b * i) / a;
if (maxn < l) continue;
num = min(maxn,r);
if (a * num + b * i <= x) ans = max(ans,num);
}
cout << ans << "\n";
return 0;
}Problem C - Numbering Blocks
评价:普通
考点:深度优先搜索(DFS)(没错,你没有看错,没有剪枝!) | 枚举(Enumeration)
一道超水题!(可没开return……)
NO.1 流氓法:打表!
这题看上去复杂得很,但实际能打表!
没错你没看错,能打表!
综合代码:(非正解)
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int a1,a2,a3;
cin >> a1 >> a2 >> a3;
if (a1 == 1) cout << "1\n";
else if (a1 == 2)
{
if (a2 == 1) cout << "3\n";
else cout << "5\n";
}
else
{
if (a2 == 1) cout << "6\n";
else if (a2 == 2)
{
if (a3 == 1) cout << "16\n";
else cout << "21\n";
}
else
{
if (a3 == 1) cout << "21\n";
else cout << "42\n";
}
}
return 0;
}NO.2 正解:DFS
考虑正解 DFS (不要再谈什么打表了!那都是流氓!)
对于每一个位置,若不是三列的最高层,就一定有下一层,选完这层直接遍历下一层就行。
for (int i = max(a[x - 1][y],a[x][y - 1]) + 1;i <= sum;i++)
{
if (vis[i]) continue;
vis[i] = true,a[x][y] = i;
dfs(x + 1,y);
vis[i] = false,a[x][y] = 0;
}若是第一、二列最高层,遍历下一列即可。
if (x == a1 + 1 && y == 1)
{
dfs(1,2);
return ;
}
else if (x == a2 + 1 && y == 2)
{
dfs(1,3);
return ;
}若是第三列最高层,增加答案数即可。
else if (x == a3 + 1 && y == 3)
{
ans++;
return ;
}综合代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 5,M = 13;
int a1,a2,a3,ans,sum,a[N][N];
bool vis[M];
void dfs(int x,int y)
{
if (x == a1 + 1 && y == 1)
{
dfs(1,2);
return ;
}
else if (x == a2 + 1 && y == 2)
{
dfs(1,3);
return ;
}
else if (x == a3 + 1 && y == 3)
{
ans++;
return ;
}
for (int i = max(a[x - 1][y],a[x][y - 1]) + 1;i <= sum;i++)
{
if (vis[i]) continue;
vis[i] = true,a[x][y] = i;
dfs(x + 1,y);
vis[i] = false,a[x][y] = 0;
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> a1 >> a2 >> a3;
sum = a1 + a2 + a3;
dfs(1,1);
cout << ans << "\n";
return 0;
}关于一些问题
做题目,尤其是比赛时,做题的目的不是抢最优解,而是把这题AC。
比赛时,哪怕是用打表,只要能解出来,就都是好方法!
Problem D - CF193B Xor
评价:顶级
考点:深度优先搜索(DFS) & 剪枝(Pruning)
一道……暴力加上一个剪枝就AC了……
(一怒之下怒了一下:无话可说)
NO.1 暴力(模拟)
先按照题意模拟,不要多想。
代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 33,INF = 1e18;
int n,u,r,b[N],k[N],p[N];
long long val,ans;
long long sum(vector <int> a)
{
val = 0;
for (int i = 0;i < n;i++) val += a[i] * k[i + 1];
return val;
}
void dfs(int now,vector <int> a)
{
if (now == u + 1)
{
ans = max(ans,sum(a));
return ;
}
vector <int> a1(n),a2(n);
for (int i = 0;i < n;i++) a1[i] = a[i],a1[i] ^= b[i + 1];
dfs(now + 1,a1);
for (int i = 0;i < n;i++) a2[i] = a[p[i + 1] - 1] + r;
dfs(now + 1,a2);
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> u >> r;
vector <int> a(n);
for (int i = 0;i < n;i++) cin >> a[i];
for (int i = 1;i <= n;i++) cin >> b[i];
for (int i = 1;i <= n;i++) cin >> k[i];
for (int i = 1;i <= n;i++) cin >> p[i];
ans = -INF;
dfs(1,a);
cout << ans << "\n";
return 0;
}其实已经有 pts 了,对于不会数学的 OIer 们这样已经够了。
NO.2 数学小优化(剪枝)
根据 xor 运算性质:
所以,做完一次 xor 运算下一次就不应该进行 xor 运算。
为什么做完一次 xor 运算下一次就不应该进行 xor 运算
那不然整个数组相当于没变,能是最优解吗?
综合代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 33,INF = 1e18;
int n,u,r,b[N],k[N],p[N];
long long val,ans;
long long sum(vector <int> a)
{
val = 0;
for (int i = 0;i < n;i++) val += a[i] * k[i + 1];
return val;
}
void dfs(int now,vector <int> a,bool check)
{
if (now == u + 1)
{
ans = max(ans,sum(a));
return ;
}
vector <int> a1(n),a2(n);
if (check)
{
for (int i = 0;i < n;i++) a1[i] = a[i],a1[i] ^= b[i + 1];
dfs(now + 1,a1,false);
}
for (int i = 0;i < n;i++) a2[i] = a[p[i + 1] - 1] + r;
dfs(now + 1,a2,true);
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> u >> r;
vector <int> a(n);
for (int i = 0;i < n;i++) cin >> a[i];
for (int i = 1;i <= n;i++) cin >> b[i];
for (int i = 1;i <= n;i++) cin >> k[i];
for (int i = 1;i <= n;i++) cin >> p[i];
ans = -INF;
dfs(1,a,true);
cout << ans << "\n";
return 0;
}