CSP-S模拟41

news/2025/10/28 20:19:22/文章来源:https://www.cnblogs.com/Wy-x/p/19172653

CSP-S模拟41

A. 数轴取物(axis)

显然可以有一个 \(O(n^3)\) 的背包 dp,设 \(dp[i][j][k]\) 表示选区间 \([i,j]\),背包容量为 \(k\) 时可获得的最大价值。每次枚举固定左端点从小到大枚举右端点,发现 \(dp[i][j]\) 可以由 \(dp[i][j-1]\) 转移而来,那么直接背包做就完了。

然后你再设一个 \(f[i][j]\) 表示用前 \(i\) 个包走到位置 \(j\) 时可获得的最大价值。然后有一个 \(O(n^2 m)\) 的 dp 转移:枚举右端点,再枚举这个包所选物品区间左端点,直接可以转移。

然后 TLE 了。

然后你直接根据复杂度钦定固定右端点时指针从右往左动的最大次数,即可通过本题。

其实发现只有后 \(n\) 个包有用,前面的包可以直接跳过。那么 \(O(n^3)\) 即可通过。

Code:

    #include<bits/stdc++.h>#define int long longusing namespace std;const int Size=(1<<20)+1;char buf[Size],*p1=buf,*p2=buf;char buffer[Size];int op1=-1;const int op2=Size-1;#define getchar()                                                              \(tt == ss && (tt=(ss=In)+fread(In, 1, 1 << 20, stdin), ss == tt)     \? EOF                                                                 \: *ss++)char In[1<<20],*ss=In,*tt=In;inline int read(){int x=0,c=getchar(),f=0;for(;c>'9'||c<'0';f=c=='-',c=getchar());for(;c>='0'&&c<='9';c=getchar())x=(x<<1)+(x<<3)+(c^48);return f?-x:x;}inline void write(int x){if(x<0) x=-x,putchar('-');if(x>9)  write(x/10);putchar(x%10+'0');}// #ifndef ONLINE_JUDGE// #define ONLINE_JUDGE// #endifint n,m;int a[205],b[205];int dp2[205][205][205];int dp[205][205];int r[205];int maxn[205];// #define max(x,y) ((x)>(y)?(x):y)inline int max(int x,int y) { return x>y?x:y; }signed main(){// axis// #ifndef ONLINE_JUDGEfreopen("axis.in","r",stdin);freopen("axis.out","w",stdout);n=read();m=read();for(int i=1;i<=n;i++){a[i]=read();b[i]=read();}for(int i=1;i<=n;i++)for(int j=i;j<=n;j++)for(int totval=0;totval<=200;totval++){dp2[totval][j][i]=dp2[totval][j-1][i];if(totval>=a[j]) dp2[totval][j][i]=max(dp2[totval][j][i],dp2[totval-a[j]][j-1][i]+b[j]);}// const int MAXN=min(200ll,max(10ll,(int)(3e8/(m*n))));// cout<<MAXN<<endl;for(int i=1;i<=m-n;i++) int x=read();m=min(n,m);for(int i=1;i<=m;i++){int x=read();for(int j=1;j<=n;j++)for(int k=j+1;k>0;k--)dp[i][j]=max(dp[i][j],dp[i-1][k-1]+dp2[x][j][k]);// for(int j=1;j<=n;j++) // maxn[j]=max(maxn[j-1],dp[i][j]),dp[i][j]=maxn[j];}int ans=0;Rfor(int i=1;i<=m;i++)for(int j=1;j<=n;j++)ans=max(ans,dp[i][j]);cout<<ans<<"\n";// #endif//mt19937_64 myrand(time(0));return 0;}Z

B. 排列变环(circle)

打表发现规律直接做。

Checker:

#include<bits/stdc++.h>
#define int long longusing namespace std;const int Size=(1<<20)+1;
char buf[Size],*p1=buf,*p2=buf;
char buffer[Size];
int op1=-1;
const int op2=Size-1;
#define getchar()                                                              \
(tt == ss && (tt=(ss=In)+fread(In, 1, 1 << 20, stdin), ss == tt)     \? EOF                                                                 \: *ss++)
char In[1<<20],*ss=In,*tt=In;
inline int read()
{int x=0,c=getchar(),f=0;for(;c>'9'||c<'0';f=c=='-',c=getchar());for(;c>='0'&&c<='9';c=getchar())x=(x<<1)+(x<<3)+(c^48);return f?-x:x;
}
inline void write(int x)
{if(x<0) x=-x,putchar('-');if(x>9)  write(x/10);putchar(x%10+'0');
}// #ifndef ONLINE_JUDGE
// #define ONLINE_JUDGE
// #endifconst int n=20;
int a[n+1];
int p[n+1],top;int find(int *a)
{int cnt=0;for(int i=2;i<=n;i++)for(int j=1;j<i;j++)cnt+=(*(a+j))>(*(a+i));return cnt;
}int do1()
{for(int i=top;i>1;i--){int nw=p[i],pre=p[i-1];swap(a[nw],a[pre]);}// cout<<"Final for 1: ";// for(int i=1;i<=n;i++) cout<<a[i]<<" ";// cout<<"\n";int cnt=find(a);for(int i=2;i<=top;i++){int nw=p[i],pre=p[i-1];swap(a[nw],a[pre]);}// cout<<"del for 1: ";// for(int i=1;i<=n;i++) cout<<a[i]<<" ";// cout<<"\n";return cnt;
}
int do2()
{for(int i=2;i<=top;i++){int nw=p[i],pre=p[i-1];swap(a[nw],a[pre]);}// cout<<"Final for 2: ";// for(int i=1;i<=n;i++) cout<<a[i]<<" ";// cout<<"\n";int cnt=find(a);for(int i=top;i>1;i--){int nw=p[i],pre=p[i-1];swap(a[nw],a[pre]);}// cout<<"del for 2: ";// for(int i=1;i<=n;i++) cout<<a[i]<<" ";// cout<<"\n";return cnt;
}void dfs(int pos)
{if(pos>n){if(top<2) return;// cout<<"\nChange position: ";// for(int i=1;i<=top;i++) cout<<p[i]<<" ";// cout<<"\n";int c1=do1();int c2=do2();// cout<<"cnt="<<c1<<"\n";if(c1!=(p[top]-p[1])*2-(top-1)) { cout<<"Egg!\n";// cout<<"Change position: ";// for(int i=1;i<=top;i++) cout<<p[i]<<" ";// cout<<"\n";// cout<<"c1="<<c1<<" c2="<<c2<<"\n";exit(0);}// cout<<pos<<" "<<top<<" "<<c1<<" "<<c2<<"\n";// if(c1==c2) { cout<<"It is the same!"<<endl; return; }// else if(c1<c2) { cout<<"Plan 1 is the winner"<<endl; return; }// else { cout<<"Plan 1 is the winner"<<endl; return; }return;}dfs(pos+1);p[++top]=pos;dfs(pos+1);p[top]=0;top--;
}signed main()
{// #ifndef ONLINE_JUDGE// freopen(".in","r",stdin);freopen("Checking.out","w",stdout);cout<<"Checking Perm "<<n<<":"<<endl;for(int i=1;i<=n;i++) a[i]=i;dfs(1);// #endif//mt19937_64 myrand(time(0));return 0;
}

Code:

#include<bits/stdc++.h>
#define int long longusing namespace std;const int Size=(1<<20)+1;
char buf[Size],*p1=buf,*p2=buf;
char buffer[Size];
int op1=-1;
const int op2=Size-1;
#define getchar()                                                              \
(tt == ss && (tt=(ss=In)+fread(In, 1, 1 << 20, stdin), ss == tt)     \? EOF                                                                 \: *ss++)
char In[1<<20],*ss=In,*tt=In;
inline int read()
{int x=0,c=getchar(),f=0;for(;c>'9'||c<'0';f=c=='-',c=getchar());for(;c>='0'&&c<='9';c=getchar())x=(x<<1)+(x<<3)+(c^48);return f?-x:x;
}
inline void write(int x)
{if(x<0) x=-x,putchar('-');if(x>9)  write(x/10);putchar(x%10+'0');
}// #ifndef ONLINE_JUDGE
// #define ONLINE_JUDGE
// #endifint n,k;
int a[1<<20];
int ans=0x3f3f3f3f;
// priority_queue<int> p1,p2;
int b[1<<20];
int val[1<<20];
unordered_map<int,int> mp;const int N=1e4+5;
struct Tree{int sum,cnt;
}t[N<<2];#define lp (p<<1)
#define rp ((p<<1)|1)const int MINN=0,MAXN=1e3+1;
void build(int l,int r,int p)
{t[p]={0,0};if(l==r) return;int mid=(l+r)>>1;build(l,mid,lp);build(mid+1,r,rp);
}void pushup(int p)
{t[p].cnt=t[lp].cnt+t[rp].cnt;t[p].sum=t[lp].sum+t[rp].sum;
}void add(int l,int r,int x,int p)
{if(l==r){t[p].sum=val[x];t[p].cnt=1;return;}int mid=(l+r)>>1;if(x<=mid) add(l,mid,x,lp);else add(mid+1,r,x,rp);pushup(p);
}int query(int l,int r,int sum,int p)
{if(l==r) return t[p].cnt&&((t[p].sum+sum)>=0);int mid=(l+r)>>1,ans=0;if(t[rp].sum+sum>=0){sum+=t[rp].sum;ans+=t[rp].cnt;ans+=query(l,mid,sum,lp);}else ans+=query(mid+1,r,sum,rp);return ans;
}signed main()
{// #ifndef ONLINE_JUDGEfreopen("circle.in","r",stdin);freopen("circle.out","w",stdout);n=read();	k=read();for(int i=1;i<=n;i++) b[i]=a[i]=read();sort(b+1,b+1+n);b[0]=1e9+1;for(int i=1;i<=n;i++){if(b[i]!=b[i-1]) mp[b[i]]=i;val[i]=b[i];}int sum=0;for(int i=1;i<=n;i++) {sum+=a[i];int nw=a[i];a[i]=mp[nw];mp[nw]++;}// cout<<sum<<"\n";sum=0;// for(int i=1;i<=n;i++) cout<<a[i]<<" ";// cout<<"\n";// for(int i=1;i<=n;i++) sum+=val[i];// // cout<<sum<<"\n";// for(int i=1;i<=n;i++)// {// 	int sum=0;// 	for(int j=i;j<=n;j++)// 	{// 		sum+=val[a[j]];// 		if(sum>=k)// 		{// 			int len=j-i;// 			if(len==76)// 			{// 				cout<<i<<" "<<j<<"\n";// 			}// 		}// 	}// }/*6 867 87472 552473 55381*/for(int i=1;i<=n;i++){if(val[a[i]]>=k) ans=0;build(MINN,MAXN,1);int sum=val[a[i]];int cnt=1;for(int j=i+1;j<=n;j++){int nw=sum+val[a[j]];// cout<<nw<<"\n";if(nw>=k){int nwcnt=cnt+1+query(MINN,MAXN,nw-k,1);int nwans=(j-i)*2-nwcnt+1;ans=min(ans,nwans);// cout<<"\n["<<i<<","<<j<<"] sum="<<nw<<" query="<<query(MINN,MAXN,nw-k,1)<<" cnt="<<nwcnt<<" nwans="<<nwans<<"\n";// break;}if(val[a[j]]>=0){sum+=val[a[j]];cnt++;}else add(MINN,MAXN,a[j],1);//,cout<<"val="<<val[a[j]]<<" p="<<a[j]<<"\n";}// cout<<"\n\n\n";}if(ans>=0x3f3f3f3f) ans=-1;cout<<ans<<"\n";// #endif//mt19937_64 myrand(time(0));return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/949113.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Linux双中文编码笔记

Linux双中文编码笔记/etc/locale.gen zh_CN.GB18030 GB18030zh_CN.GBK GBK 上面两行默认是被注释掉的,要打开。 /usr/sbin/dpkg-reconfigure/usr/sbin不在普通用户的PATH里,再说运行它也需要root权限。 如果dkpg-rec…

C++类和对象(1) - 详解

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

人工智能之编程基础 Python 入门:第二章 Python 的编辑器 VS Code

人工智能之编程基础 Python 入门:第二章 Python 的编辑器 VS Code人工智能之编程基础 Python 入门 第二章 Python 的编辑器 VS Code@目录人工智能之编程基础 Python 入门前言一、VS Code安装二、配置PythonVS Code 汉…

2019 福建省队集训录

退役前最后的贡献\(\scr{Day}\ 1\) T1 (sort)Source:$\bf{solution}$$\bf{code}$T2 (sort)Source:$\bf{solution}$$\bf{code}$T3 (sort)Source:$\bf{solution}$$\bf{code}$

AIX multibos bootlist

Check bash bootlist -m normal -o hdisk0 blv=hd5 pathid=0 lspv hdisk0 00cc4bc0964f315a rootvg active hdisk1 00cc4bc028d6260c altinst_root…

记录一次nginx能通但是请求一直不了的问题

今天在公司碰到这样一个问题:开发后在测试环境进行部署,部署后有个调用其他部门的接口,需要通过nginx来代理请求转发到另一个部门。运维把nginx配置完成后,本地和开发测试都正常,但是通过测试环境访问一直是不同的…

【嵌入式】PWM DAC的滤波器设计

PWM DAC PWM概念本身很简单,具体可以参考各网上资料。PWM:脉冲宽度调制(英语:Pulse-width modulation,缩写:PWM),简称脉宽调制,是用脉冲来输出模拟信号的一种技术,一般变换后脉冲的周期固定,但脉冲的工作周…

被称作遗憾之物 爬满了脊骨 又把控了痛楚 被称作无用之物 修筑了唯一的通路

test30 前两题都 0pts,nbm(? 2-A 飞船制造 (spaceship.cpp) 怎么有傻子没开 c++11 写了 rank 然后 re 惹 /fad 考虑依次枚举 \(s=i+j+k\),计算出 \(s\) 一定的方案数就能确定唯一的 \(s\),方案数计算好像只能考虑…

neovim在windwos11下snack.nvim的问题

问题复现 首先确定有 find 命令,在执行之后,会出现下面的问题: Command failed: - cmd: `find . -type f -not -path */.git/* -not -path */.*`几乎百分百。 查找原因 查阅之后得知,问题为调用了linux风格的find命…

完整教程:Java 集合 “List + Set”面试清单(含超通俗生活案例与深度理解)

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

禁用 IPython 历史记录 history.sqlite

Windows 在 %UserProfile%\.ipython\profile_default\ 文件夹中或 Linux 在 ~/.ipython/profile_default/ 目录中(默认配置文件名为profile_default),新建ipython_config.json文件,填入以下内容即可禁用 IPython 历…

Luogu P7914 [CSP-S 2021] 括号序列 题解 [ 蓝 ] [ 区间 DP ] [ 前缀和优化 ] [ 调试技巧 ]

括号序列:无聊,感觉做过类似的拼接类区间 DP 就直接秒了。 注意到这个超级括号序列定义很复杂,除了两边没有 \(\texttt{*}\) 没有啥很好的性质。于是直接考虑暴力区间 DP:定义 \(dp_{l, r}\) 表示 \(l\sim r\) 的合…

扩展BaseMapper类 - 详解

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

《程序员修炼之道:从小工到专家》前五分之二观后感

读完《程序员修炼之道:从小工到专家》前五分之二的内容,我对“程序员如何成长”有了更清晰的认知。书中开篇便强调“职业主义”,打破了我对“码农”的刻板印象——程序员不应只是被动执行需求的工具人,而应像匠人般…

矩阵快速幂章节笔记(这里主要介绍的是我的错题)

矩阵加速的递推 1.1维k阶 f(n)=f(n-1)+f(n-2)+f(n-i)可以添加系数 那么矩阵的第一列就是系数了,其它用未知数,然后计算。注意start数组,就是开始的数组是倒着来的,请看代码(斐波那契) 2.k维1阶 dp[i][j]=dp[i-…

实验二 现代C++编程初体验

任务一: 代码:#pragma once#include <string>// 类T: 声明 class T { // 对象属性、方法 public:T(int x = 0, int y = 0); // 普通构造函数T(const T &t); // 复制构造函数T(T &&t); //…

P5322 [BJOI2019] 排兵布阵

P5322 [BJOI2019] 排兵布阵 题解题目传送门 博客传送门 我们浏览一遍测试点,发现了一个 \(s=1\) 的特殊性质。先考虑这一性质。 \(s=1\)特殊性质 如果我们当前第 \(i\) 座城市的兵力数量足够时,添加兵力显然不优。而…

题解:P9292 [ROI 2018] Robomarathon

题目传送门 题目大意: 有 \(N\) 名机器人选手参加马拉松,选手编号为 \(1 \dots N\),分道编号也为 \(1 \dots N\)。选手 \(i\) 占据分道 \(i\),跑完全程需要 \(a_i\) 秒。设 \(S \subseteq \{1, 2, \dots, N\}\) 表…

[题解]P5322 [BJOI2019] 排兵布阵

P5322 [BJOI2019] 排兵布阵 我们可以预处理出第 \(i\) 个城堡分配 \(j\) 的兵力能获得多少的得分,记为 \(w[i][j]\)。 则每一个 \(w[i]\) 都是一个泛化物品,即价值(\(w[i][j]\))随着分配体积(\(j\))变化的物品。…