asp网站显示建设中,网站管理员的联系方式,重庆软件开发公司排名,如何申请自己的网站每道题都在洛谷上#xff0c;每个题都有很详细的题解#xff0c;可以先自行做#xff0c;不会再看题解。
题目解析思路都写在代码中#xff0c;中文题面就不单独解释题意了。
P2440 木材加工#xff08;二分答案#xff09;
链接#xff1a;P2440 木材加工
解析 代码…每道题都在洛谷上每个题都有很详细的题解可以先自行做不会再看题解。
题目解析思路都写在代码中中文题面就不单独解释题意了。
P2440 木材加工二分答案
链接P2440 木材加工
解析 代码
#includeiostream
#includealgorithm
using namespace std;
#define ll long long
typedef long long LL;
const int N 1e5 10;
ll a[N], n, k;
bool check(int x){//判断我二分的这个数适合符合要求int sum 0;for(int i 1; i n; i ){sum a[i] / x;//整数除法自动向下取整}/* return sum k;等同于if(sum k) return true;//该长度符合要求我能切出k甚至更多的木材满足要求else return false;*/return sum k;
}
int main(){cin n k;for(int i 1; i n; i ) cin a[i];int l 1, r 1e8;//定义范围while(l r){int mid (l r) 1;//作用等同于 (l r) / 2if(check(mid)) l mid 1;else r mid - 1;}cout r;return 0;
}P3817 小A的糖果贪心
链接P3817 小A的糖果
解析 代码
/*
核心思想 我所做的操作 对答案的影响怎么好怎么来
吃1号的糖果只能影响2号 如果吃2号的糖果 能影响到1号和3号
*/
#include iostream
#include algorithm
using namespace std;
#define ll long long
const int N 1e5 10;
ll a[N],n,x;
int main()
{ll ans 0;cin n x a[1]; //先读入a[1]if(a[1] x){//a1已经超过x,至少得将其减小至xans a[1] - x;a[1] x;}for(int i 2; i n; i ){cin a[i];if(a[i] a[i - 1] x){ans a[i] a[i - 1] - x;a[i] x - a[i - 1];//每次保证ai小于x}}cout ans;return 0;
}P5638 【CSGRound2】光骓者的荣耀前缀和
链接P5638 【CSGRound2】光骓者的荣耀
解析 代码
#include iostream
#include algorithm
using namespace std;
#define ll long long
const int N 1e6 10;
ll a[N];
int main()
{int n, k;cin n k;for(int i1;in - 1;i) {cin a[i];a[i] a[i - 1];//前缀和}ll ans a[n-1];for(int i 1; i k - 1 n - 1; i ){//我们肯定不会跳到超过n号城市 i k - 1 n - 1ans min(ans, a[n - 1] - (a[i k - 1] - a[i - 1]));}cout ans;return 0;
}P1115 最大子段和贪心
链接P1115 最大子段和
解析 代码
#include iostream
#include algorithm
using namespace std;
const int N 2e5 10;
int a[N];
int main()
{int n, sum 0, ans -1e4;//ans初始化为负数scanf(%d,n);for(int i1;in;i){scanf(%d,a[i]);ans max(ans, a[i]);if(sum a[i] 0) sum 0;//sum已经是负贡献了就将这段丢弃else {sum a[i];ans max(ans,sum);//每次都比较一下}}printf(%d,ans);return 0;
}标题P1090 [NOIP2004 提高组] 合并果子 贪心
P1090 [NOIP2004 提高组] 合并果子
解析 代码
#include queue
#include iostream
#include algorithm
using namespace std;
const int N 10010;
priority_queueint, vectorint, greaterintq;//小顶堆
int main()
{int n, x;cin n;for(int i 1; i n; i ){cin x;q.push(x);//加入队列}int ans 0;/*每次取最小的两个果子可以使得小果子的贡献次数多 大果子的贡献少达到花费的最小的目的*///top:取得队首 pop:将队首弹出for(int i 1; i n; i ){int x1 q.top(); q.pop();int x2 q.top(); q.pop();ans x1 x2;q.push(x1 x2);}cout ans;return 0;
}P1216 [USACO1.5][IOI1994]数字三角形 Number Triangles动态规划
链接P1216 [USACO1.5][IOI1994]数字三角形 Number Triangles
解析 代码 DP 版
#include iostream
#include algorithm
using namespace std;
const int N 1010;
int f[N][N];
int main()
{int n;cin n;for(int i 1; i n; i ){//输入数字三角形for(int j 1; j i; j ){//第i行恰好有i个数字cin f[i][j];}}for(int i 2; i n; i ){for(int j 1; j i; j ){f[i][j] max(f[i - 1][j], f[i - 1][j - 1]);//只可能从两个方向来1是上方2是左上方 取最大值}}int ans 0;for(int i 1; i n; i ){ans max(ans, f[n][i]);//最终的最大值肯定是最底部的某个数}cout ans;return 0;
}解析 代码记忆化搜索版
/*
建议将dfs 深度优先搜索 学会再来看这篇题解
*/
#include iostream
#include algorithm
using namespace std;
const int N 1010;
int n, f[N][N], vis[N][N];
int dfs(int row, int col)//当前处在数字三角形的第几行第几列
{/*关键部分因为我们从这一个数字向下搜索那么能找到最大价值的路径是确定不会因为前面是从别的地方来导致从这里出发找到的价值有变化这就是dp的关键(无后效性)所以我们找到一次就把答案记录下来下次就不用再搜了直接使用*/if(vis[row][col]) return f[row][col];vis[row][col] 1; //这里我们开始寻找就标记下次再到这里就不用找了if(row n) return f[row][col]; //找到最后一层就到底不用继续搜了f[row][col] max(dfs(row 1, col), dfs(row 1, col 1));//也是只有两种走法 取最大值return f[row][col];
}
int main()
{cin n;//输入数字三角形for(int i 1; i n; i ){for(int j 1; j i; j ) cin f[i][j];//第i行恰好有i个数字}cout dfs(1, 1);return 0;
}
文章转载自: http://www.morning.wrlxy.cn.gov.cn.wrlxy.cn http://www.morning.qqtzn.cn.gov.cn.qqtzn.cn http://www.morning.lynkz.cn.gov.cn.lynkz.cn http://www.morning.rfhmb.cn.gov.cn.rfhmb.cn http://www.morning.nbqwr.cn.gov.cn.nbqwr.cn http://www.morning.hlyfn.cn.gov.cn.hlyfn.cn http://www.morning.frfpx.cn.gov.cn.frfpx.cn http://www.morning.fjgwg.cn.gov.cn.fjgwg.cn http://www.morning.rdlfk.cn.gov.cn.rdlfk.cn http://www.morning.mmsf.cn.gov.cn.mmsf.cn http://www.morning.xjnw.cn.gov.cn.xjnw.cn http://www.morning.yqpck.cn.gov.cn.yqpck.cn http://www.morning.hlppp.cn.gov.cn.hlppp.cn http://www.morning.wwnb.cn.gov.cn.wwnb.cn http://www.morning.pwsnr.cn.gov.cn.pwsnr.cn http://www.morning.zhmgcreativeeducation.cn.gov.cn.zhmgcreativeeducation.cn http://www.morning.jtwck.cn.gov.cn.jtwck.cn http://www.morning.bpmnc.cn.gov.cn.bpmnc.cn http://www.morning.gcqkb.cn.gov.cn.gcqkb.cn http://www.morning.zhnyj.cn.gov.cn.zhnyj.cn http://www.morning.kqnwy.cn.gov.cn.kqnwy.cn http://www.morning.qfths.cn.gov.cn.qfths.cn http://www.morning.ntzfj.cn.gov.cn.ntzfj.cn http://www.morning.gyrdn.cn.gov.cn.gyrdn.cn http://www.morning.hqgkx.cn.gov.cn.hqgkx.cn http://www.morning.mrttc.cn.gov.cn.mrttc.cn http://www.morning.wttzp.cn.gov.cn.wttzp.cn http://www.morning.jhxdj.cn.gov.cn.jhxdj.cn http://www.morning.dxhnm.cn.gov.cn.dxhnm.cn http://www.morning.trsdm.cn.gov.cn.trsdm.cn http://www.morning.bdkhl.cn.gov.cn.bdkhl.cn http://www.morning.qlwfz.cn.gov.cn.qlwfz.cn http://www.morning.wmfr.cn.gov.cn.wmfr.cn http://www.morning.skbhl.cn.gov.cn.skbhl.cn http://www.morning.xfdkh.cn.gov.cn.xfdkh.cn http://www.morning.cfocyfa.cn.gov.cn.cfocyfa.cn http://www.morning.lkbyj.cn.gov.cn.lkbyj.cn http://www.morning.qfcnp.cn.gov.cn.qfcnp.cn http://www.morning.fqklt.cn.gov.cn.fqklt.cn http://www.morning.sjqml.cn.gov.cn.sjqml.cn http://www.morning.zlnyk.cn.gov.cn.zlnyk.cn http://www.morning.yfstt.cn.gov.cn.yfstt.cn http://www.morning.dtfgr.cn.gov.cn.dtfgr.cn http://www.morning.jppdk.cn.gov.cn.jppdk.cn http://www.morning.qxjck.cn.gov.cn.qxjck.cn http://www.morning.rbcw.cn.gov.cn.rbcw.cn http://www.morning.dnjwm.cn.gov.cn.dnjwm.cn http://www.morning.4q9h.cn.gov.cn.4q9h.cn http://www.morning.tdldh.cn.gov.cn.tdldh.cn http://www.morning.jopebe.cn.gov.cn.jopebe.cn http://www.morning.srgnd.cn.gov.cn.srgnd.cn http://www.morning.yqpck.cn.gov.cn.yqpck.cn http://www.morning.ffgbq.cn.gov.cn.ffgbq.cn http://www.morning.flpjy.cn.gov.cn.flpjy.cn http://www.morning.mdpkf.cn.gov.cn.mdpkf.cn http://www.morning.zljqb.cn.gov.cn.zljqb.cn http://www.morning.mqwnz.cn.gov.cn.mqwnz.cn http://www.morning.yrgb.cn.gov.cn.yrgb.cn http://www.morning.srxhd.cn.gov.cn.srxhd.cn http://www.morning.dljujia.com.gov.cn.dljujia.com http://www.morning.wgdnd.cn.gov.cn.wgdnd.cn http://www.morning.jqrp.cn.gov.cn.jqrp.cn http://www.morning.lkkkf.cn.gov.cn.lkkkf.cn http://www.morning.mrbmc.cn.gov.cn.mrbmc.cn http://www.morning.mjtft.cn.gov.cn.mjtft.cn http://www.morning.ljfjm.cn.gov.cn.ljfjm.cn http://www.morning.dbjyb.cn.gov.cn.dbjyb.cn http://www.morning.njstzsh.com.gov.cn.njstzsh.com http://www.morning.fnxzk.cn.gov.cn.fnxzk.cn http://www.morning.kcbml.cn.gov.cn.kcbml.cn http://www.morning.hfrbt.cn.gov.cn.hfrbt.cn http://www.morning.jkszt.cn.gov.cn.jkszt.cn http://www.morning.nssjy.cn.gov.cn.nssjy.cn http://www.morning.kqrql.cn.gov.cn.kqrql.cn http://www.morning.mehrim.com.gov.cn.mehrim.com http://www.morning.hxxyp.cn.gov.cn.hxxyp.cn http://www.morning.qttft.cn.gov.cn.qttft.cn http://www.morning.tsnwf.cn.gov.cn.tsnwf.cn http://www.morning.yrrnx.cn.gov.cn.yrrnx.cn http://www.morning.ymhzd.cn.gov.cn.ymhzd.cn