【NOIP模拟题】【贪心】【动态规划DP】2016.11.12第二题题解
来源:互联网 发布:中国网络电视台纪实台 编辑:程序博客网 时间:2024/05/22 14:19
B
这道题有两种解法,贪心和DP。
1.贪心
我们先将正数合并,负数记录,当取不下时交换最小的负数
#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<vector>#include<queue>#include<stack>#include<map>#include<set>#include<string>#include<iomanip>#include<ctime>#include<climits>#include<cctype>#include<algorithm>#define LL long long#ifdef WIN32#define AUTO "%I64d"#else#define AUTO "%lld"#endifusing namespace std; const int maxn = 1005;int n,m,q;int tot_a,nod;LL a[maxn];template <class T> inline void read(T &xx){ xx = 0; T flag = 1; char ch = (char)getchar(); while(ch<'0' || ch>'9') { if(ch == '-') flag = -1; ch = (char)getchar(); } while(ch>='0' && ch<='9') { xx = (xx<<1) + (xx<<3) + ch - '0'; ch = (char)getchar(); } xx *= flag;}void inti(){ read(n); read(m); for(int i = 1; i <= n; i++) { read(a[++tot_a]); if(a[tot_a] >= 0) { if(a[tot_a-1] >= 0 && tot_a != 1) { --tot_a; a[tot_a] += a[tot_a+1]; } if(nod != 0) { sort(a+nod,a+tot_a); nod = 0; } } if(a[tot_a] < 0 && nod == 0) nod = tot_a; }}void work(){ while(m--) { int tot = 0; read(q); for(int i = 1; i <= tot_a; i++) if(q + a[i] >= 0) q += a[i]; else tot++; printf("%d\n",tot); }}int main(){ freopen("b.in","r",stdin); freopen("b.out","w",stdout); inti(); work(); printf("Time used=%0.2lf\n",(double)clock()/CLOCKS_PER_SEC); return 0;}
2.DP
从前往后考虑,要记录当前和信息与最小值,所以从后往前考虑。dp[i][j] 表示i到n去了几个j个数字,前缀和的最小值是多少。dp[i][j]=max(dp[i+1][j-1],a[i]+dp[i+1][j])
#include <iostream>using namespace std;const int maxn=1055;long long dp[maxn][maxn];int a[maxn];int work(long long num,int r){ int l=0,ans; while(l<=r) { int mid=(l+r)>>1; if(dp[1][mid]+num>=0){ ans=mid;r=mid-1; } else l=mid+1; } return ans;}int main() { freopen("b.in","r",stdin); freopen("b.out","w",stdout); int n,m; scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) scanf("%d",a+i); dp[n+1][0]=0; for(int i=n;i;i--) { dp[i][0]=min(1LL*a[i],dp[i+1][0]+a[i]); dp[i][n-i+1]=0; for(int j=1;j<n-i+1;j++) { dp[i][j]=min(a[i]+dp[i+1][j],1LL*a[i]); dp[i][j]=max(dp[i][j],dp[i+1][j-1]); } } while(m--) { long long x; scanf("%I64d",&x); printf("%d\n",work(x,n)); } return 0;}
0 0
- 【NOIP模拟题】【贪心】【动态规划DP】2016.11.12第二题题解
- 【NOIP模拟题】【动态规划DP】2016.11.11第二题Landscaping题解
- 【NOIP模拟题】【DP】2016.11.10 第二题题解
- NOIP模拟题 2016.11.12 [Catalan数] [贪心] [动态规划] [DLX] [数独]
- NOIP模拟题 2016.9.10 [动态规划] [概率] [贪心]
- NOIP模拟题 2016.11.14 [动态规划] [线段树优化DP] [字符串的复制粘贴DP]
- 【NOIP模拟题】【图论】2016.11.18 第二题 心 题解
- NOIP模拟题[贪心][DP][数论]
- 【NOIP模拟题】【DP】【同余最短路】【暴力剪枝】2016.11.15 第二题 小L的牛栏 题解
- 【NOIP模拟题】【DP】【快速幂】2016.11.16 第二题
- NOIP模拟题题解
- 【NOIP模拟题】【模拟】【DP】【JOI】2016.11.14第一题 复制&粘贴2 题解
- NOIP模拟题 2016.8.27 [贪心] [DP] [计数问题]
- NOIP模拟题 River Path Word[排序][贪心][DP]
- JZOJ3418. 【NOIP动态规划专题】选课(2017.8DP&贪心专题)
- 【NOIP模拟题】【暴力求解法】【JOI】2016.11.14第二题 愉快的logo设计 题解
- 11-1 noip模拟 第二题 SPFA+状压dp
- 【NOIP模拟题】【最短路】【DP】2016.11.11第一题tractor题解
- 11.11后的一天开通了我的博客
- C#中对数据库的基本操作(增删改以及调用存储过程)
- markdown与各种网页的各种地方改变字号颜色的方法
- java多线程
- 最长公共回文子序列 【NOIP2016提高A组集训第14场11.12】
- 【NOIP模拟题】【贪心】【动态规划DP】2016.11.12第二题题解
- 株洲新程IT 教育 李赞红老师 第五章 阴影、渐变和位图的运算
- 从源码角度解析android APP启动过程中各类及其方法的调用
- 一点数学知识
- VC++下使用ADO连接Access数据库
- c++ map的用法
- 写出一个不能被继承的类
- Neural Networks and Deep Learning 资料整理
- uml——序列图和协作图