清明节入门模拟赛题解
T1
解法1(30分):
- 对于操作1,我们暴力给每个
a_i+k 。O(n) - 对于操作2,我们暴力枚举左右端点,再一个个加起来,并取最大值。
O(n^3) - 时间复杂度
O(n^3q) 。
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,q;
int a[10005];
template<typename T>
inline void read(T &x) {
x = 0;
register char c = getchar();
register short f = 1;
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
x *= f;
}
template <typename T, typename... Args>
inline void read(T &x, Args &...temps)
{
read(x), read(temps...);
}
signed main(){
// freopen("test10.in","r",stdin);
// freopen("test10.out","w",stdout);
cin>>n>>q;
for(int i=1;i<=n;i++){
read(a[i]);
}
int l,r,k,p;
while(q--){
read(p);
if(p==1){
read(l,r,k);
for(int i=l;i<=r;i++){
a[i]+=k;
}
}
else{
read(l,r);
int maxn=0;
int ans=0;
for(int x=l;x<=r;x++){
for(int y=x;y<=r;y++){
int now=0;
for(int i=x;i<=y;i++){
now+=a[i];
}
ans=max(ans,now);
}
}
printf("%d\n",ans);
}
// cout<<q<<endl;
}
return 0;
}
解法2(30分):
- 对于操作1,我们暴力给每个
a_i+k 。O(n) - 对于操作2,我们暴力枚举左右端点,再利用前缀和
O(1) 取区间和。O(n^2) - 时间复杂度
O(n^2q) 。 - 感觉其实没什么思想难度,就只给
30 分了。
解法2(100分):
- 对于操作1,我们暴力给每个
a_i+k 。O(n) - 对于操作2,我们考虑从左往右枚举全并加起来,
- 若加起来为正数,则后面取这一段一定更优。
- 若加起来为负数,则后面不取这一段一定更优,此时将累加器变为
0 即可。 - 答案即过程中累加器取到的最大值。
标程
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,q;
int a[10005];
template<typename T>
inline void read(T &x) {
x = 0;
register char c = getchar();
register short f = 1;
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
x *= f;
}
template <typename T, typename... Args>
inline void read(T &x, Args &...temps)
{
read(x), read(temps...);
}
signed main(){
// freopen("test10.in","r",stdin);
// freopen("test10.out","w",stdout);
cin>>n>>q;
for(int i=1;i<=n;i++){
read(a[i]);
}
int l,r,k,p;
while(q--){
read(p);
if(p==1){
read(l,r,k);
for(int i=l;i<=r;i++){
a[i]+=k;
}
}
else{
read(l,r);
int maxn=0;
int ans=0;
for(int i=l;i<=r;i++){
maxn+=a[i];
if(maxn<0) maxn=0;
ans=max(ans,maxn);
}
ans=max(ans,maxn);
printf("%d\n",ans);
}
}
return 0;
}
T2
解法1(30分):
- 对于操作1,我们暴力给每个
a_i+k 。O(n) - 对于操作2,我们暴力枚举左右端点,再一个个加起来,并取最大值,遇到长度大于
m 时不取。O(n^3) - 时间复杂度
O(n^3q) 。#include<bits/stdc++.h> #define int long long using namespace std; int n,q; template<typename T> inline void read(T &x) { x = 0; register char c = getchar(); register short f = 1; while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') { x = (x << 1) + (x << 3) + (c ^ 48); c = getchar(); } x *= f; } template <typename T, typename... Args> inline void read(T &x, Args &...temps) { read(x), read(temps...); } int sum[10005]; int b[10005]; int qq[10005]; int a[10005]; signed main(){ // freopen("test10.in","r",stdin); // freopen("test.out","w",stdout); cin>>n>>q; for(int i=1;i<=n;i++){ read(a[i]); } int ls,rs,k,p; while(q--){ read(p); if(p==1){ read(ls,rs,k); for(int i=ls;i<=rs;i++){ a[i]+=k; } } else{ read(ls,rs,k); int ans=-1e9; for(int x=ls;x<=rs;x++){ for(int y=x;y<=rs;y++){ if(y-x+1>k) continue; int now=0; for(int i=x;i<=y;i++){ now+=a[i]; } ans=max(ans,now); } } printf("%d\n",ans); } } return 0; } /* 5 5 2 3 5 6 1 1 2 3 1 2 2 4 2 2 4 5 2 2 1 5 3 2 3 3 2
12 7 16 6
*/
### 解法2(30分):
* 对于操作1,我们暴力给每个$a_i+k$。$O(n)$
* 对于操作2,我们暴力枚举左右端点,再利用前缀和$O(1)$取区间和。遇到长度大于$m$时不取。$O(n^2)$
* 时间复杂度$O(n^2q)$。
* 感觉其实没什么思想难度,就只给$30$分了。
### 解法3(100分)
* 对于操作1,我们暴力给每个$a_i+k$。$O(n)$
* 对于操作2的序列,先进行前缀和预处理,再枚举右端点$i$,左端点从1开始。
* 比较一下左端点可能在的两个位置$j,k$,若$k<j<i$并且$sum[j] \leq sum[k]$,则$k$永远不会被取。因此我们使用单调队列维护。
```c++
/**
*
* ┏┓ ┏┓
* ┏┛┻━━━┛┻┓
* ┃ ┃
* ┃ ━ ┃
* ┃ ┳┛ ┗┳ ┃
* ┃ ┃
* ┃ ┻ ┃
* ┃ ┃
* ┗━┓ ┏━┛Code is far away from bug with the animal protecting
* ┃ ┃ 神兽保佑,代码无bug
* ┃ ┃
* ┃ ┗━━━┓
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛
*
*/
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,q;
template<typename T>
inline void read(T &x) {
x = 0;
register char c = getchar();
register short f = 1;
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
x *= f;
}
template <typename T, typename... Args>
inline void read(T &x, Args &...temps)
{
read(x), read(temps...);
}
int sum[10005];
int b[10005];
int qq[10005];
int a[10005];
signed main(){
// freopen("test10.in","r",stdin);
//freopen("test10.out","w",stdout);
cin>>n>>q;
for(int i=1;i<=n;i++){
read(a[i]);
}
int ls,rs,k,p;
while(q--){
read(p);
if(p==1){
read(ls,rs,k);
for(int i=ls;i<=rs;i++){
a[i]+=k;
}
}
else{
read(ls,rs,k);
int ans=-1e9;
int l=1,r=1;
for(int i=ls;i<=rs;i++){
b[i-ls+1]=a[i];
}
int len=rs-ls+1;
for(int i=1;i<=len;i++){
sum[i]=sum[i-1]+b[i];
}
memset(qq,0,sizeof qq);
qq[1]=0;
for(int i=1;i<=len;i++){
while(l<=r&&qq[l]<i-k) l++;
ans=max(ans,sum[i]-sum[qq[l]]);
while(l<=r&&sum[qq[r]]>=sum[i]){
r--;
}
qq[++r]=i;
}
printf("%lld\n",ans);
}
}
return 0;
}
/*
5 5
2 3 5 6 1
1 2 3 1
2 2 4 2
2 4 5 2
2 1 5 3
2 3 3 2
12
7
16
6
*/
T3
-
n$个节点生成二叉树的数量为$Cat_n=\frac{C_{2n}^{n}}{n+1} 此题是弱化版,直接计算即可。
#include<bits/stdc++.h> #define int long long using namespace std; int n,m; template<typename T> inline void read(T &x) { x = 0; register char c = getchar(); register short f = 1; while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') { x = (x << 1) + (x << 3) + (c ^ 48); c = getchar(); } x *= f; } template <typename T, typename... Args> inline void read(T &x, Args &...temps) { read(x), read(temps...); } int fac[25]; signed main(){ //freopen("test3.in","r",stdin); //freopen("test3.out","w",stdout); read(n); double ans=1; fac[1]=1; for(int i=2;i<=20;i++){ fac[i]=fac[i-1]*i; } for(int i=2*n-8;i>n-4;i--) ans=ans*1.0*i; ans=ans*1.0/(1.0*fac[n-4]); ans=ans*1.0/(1.0*n-3.0); double ans1=1; for(int i=2*n;i>n;i--){ ans1=ans1*1.0*i; } ans1=ans1*1.0/(1.0*fac[n]); ans1=ans1*1.0/(1.0*n+1.0); printf("%.4f",ans/ans1); return 0; }T4
此题有3个要求:
1、要求
2、要求维护一个值出现的时间刻。
3、要求找到已知值的接近值。
- 对于前两个操作,一个
map 即可维护,但对于第3个操作来说略显吃力。 - 因此,我们用一个
map 来应对第2个操作,用一个multiset 应对对应值查找、修改、插入和二分查询,以保证每个操作在O(logn) 以内。
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,q;
template<typename T>
inline void read(T &x) {
x = 0;
register char c = getchar();
register short f = 1;
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
x *= f;
}
template <typename T, typename... Args>
inline void read(T &x, Args &...temps)
{
read(x), read(temps...);
}
int a[100005];
multiset<int>g;
map<int,int >mp;
signed main(){
// freopen("test10.in","r",stdin);
// freopen("test10.out","w",stdout);
cin>>n>>q;
for(int i=1;i<=n;i++){
read(a[i]);
g.insert(a[i]);
mp[a[i]]=-1;
}
for(int i=1;i<=q;i++){
int p,l,r,k,w,c,d;
read(p);
if(p==1){
read(k,w);
auto pos=g.find(k);
if(pos!=g.end()) g.erase(pos);
g.insert(k*w+w);
mp[k*w+w]=i;
}
else if(p==2){
read(c);
if(mp[c]==0) printf("%d\n",-1);
else if(mp[c]==-1) printf("%d\n",0);
else printf("%lld\n",mp[c]);
}
else{
read(d);
auto it=g.upper_bound(d);
int res;
if(it==g.end()){
res=*(--it);
}
else if(it==g.begin()){
res=*it;
}
else{
int next = *it;
int prev = *(--it);
if (d - prev <= next - d) {
res = prev;
} else {
res = next;
}
}
printf("%lld\n",res);
}
}
return 0;
}
/*
5 3
3 2 1 4 5
1 2 1
1 4 2
2 4
*/