[hihocoder][Offer收割]编程练习赛50
循环数组
计算a[i]的前缀和s[i],计算l[i]为1~i-1中最小的s值,r[i]为i~n中最大的s值。
则a[i]~a[n]满足性质的条件为r[i]-s[i-1]>0,a[1]~a[i-1]满足性质的条件为l[i]+s[n]-s[i-1]>0
#pragma comment(linker, "/STACK:102400000,102400000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<vector>
#include<algorithm>
#include<iostream>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<functional>
#include<math.h>
//#include<bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef queue<int> QI; void makedata() {
freopen("input.txt", "w", stdout);
fclose(stdout);
} lint a[], s[], l[], r[]; int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
#endif
//makedata();
std::ios::sync_with_stdio(), cin.tie();
int n;
cin >> n; for(int i = ; i <= n; i++) cin >> a[i]; s[] = a[]; for(int i = ; i <= n; i++) s[i] = s[i - ] + a[i]; l[] = , l[] = s[]; for(int i = ; i <= n; i++) l[i] = min(l[i - ], s[i - ]); r[n] = s[n]; for(int i = n - ; i >= ; i--) r[i] = min(s[i], r[i + ]); for(int i = ; i <= n; i++) {
if((r[i] - s[i - ] > ) && (s[n] - s[i - ] + l[i] > )) {
cout << i << endl;
return ;
}
} cout << - << endl;
return ;
}
座位问题
根据题目要求,对于一段区间为l~r的连续空座位,其中最优先的位置应为(l+r)/2。对于多段连续的空座位,选择其中长度最长的那一段,如果有多段都是最长的,选择开始位置最小的那一段。坐下后一段可能变为两段、一段或零段,用一个小根堆维护这些段,新来的直接插在堆顶的段里。
#pragma comment(linker, "/STACK:102400000,102400000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<vector>
#include<algorithm>
#include<iostream>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<functional>
#include<math.h>
//#include<bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef queue<int> QI; void makedata() {
freopen("input.txt", "w", stdout);
fclose(stdout);
}
class segment {
public:
int l, r;
bool operator <(const segment & s) const {
if(r - l < s.r - s.l) return true; if(r - l > s.r - s.l) return false; return l > s.l;
}
};
priority_queue<segment> q;
int a[]; int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
#endif
//makedata();
std::ios::sync_with_stdio(), cin.tie();
int n, m, k;
cin >> n >> m >> k; for(int i = ; i <= m; i++) cin >> a[i]; a[] = , a[m + ] = n + ; for(int i = ; i <= m + ; i++) {
//a[i - 1] + 1, a[i] - 1
if(a[i - ] + <= a[i] - ) {
segment s;
s.l = a[i - ] + ;
s.r = a[i] - ;
q.push(s);
}
} for(int i = ; i < k; i++) {
segment s = q.top(), tmp;
q.pop();
int x = (s.l + s.r) / ;
cout << x << endl; if(s.l <= x - ) {
tmp.l = s.l, tmp.r = x - ;
q.push(tmp);
} if(x + <= s.r) {
tmp.l = x + , tmp.r = s.r;
q.push(tmp);
}
} return ;
}
末尾有最多0的乘积
显然0的个数只与每个数中2和5的因子个数有关。
动态规划:dp[i][j][k]表示从前i个中选出j个得到k个2时能得到的最多的5的个数,值为-1表示该状态不存在。
dp[i][j][k] = max(dp[i - 1][j][k], dp[i - 1][j - 1][k - b[i]] + c[i]);
#pragma comment(linker, "/STACK:102400000,102400000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<vector>
#include<algorithm>
#include<iostream>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<functional>
#include<math.h>
//#include<bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef queue<int> QI; void makedata() {
freopen("input.txt", "w", stdout);
fclose(stdout);
} int dp[][][], b[], c[];
lint a[]; int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
#endif
//makedata();
std::ios::sync_with_stdio(), cin.tie();
int n, m;
cin >> n >> m; for(int i = ; i <= n; i++) {
cin >> a[i];
b[i] = c[i] = ; while(a[i] % == ) a[i] /= , b[i]++; while(a[i] % == ) a[i] /= , c[i]++;
} memset(dp, -, sizeof(dp)); for(int i = ; i <= n; i++) dp[i][][] = ; for(int i = ; i <= n; i++) {
for(int j = ; j <= i; j++) {
for(int k = ; k >= ; k--) {
dp[i][j][k] = dp[i - ][j][k]; if(k >= b[i] && dp[i - ][j - ][k - b[i]] != -) dp[i][j][k] = max(dp[i][j][k], dp[i - ][j - ][k - b[i]] + c[i]);
}
}
} int ans = ; for(int i = ; i < ; i++) if(dp[n][m][i] != -)ans = max(min(dp[n][m][i], i), ans); cout << ans << endl;
return ;
}
[hihocoder][Offer收割]编程练习赛50的更多相关文章
- hihocoder [Offer收割]编程练习赛4
描述 最近天气炎热,小Ho天天宅在家里叫外卖.他常吃的一家餐馆一共有N道菜品,价格分别是A1, A2, ... AN元.并且如果消费总计满X元,还能享受优惠.小Ho是一个不薅羊毛不舒服斯基的人,他希望 ...
- hihocoder [Offer收割]编程练习赛61
[Offer收割]编程练习赛61 A:最小排列 给定一个长度为m的序列b[1..m],再给定一个n,求一个字典序最小的1~n的排列A,使得b是A的子序列. 贪心即可,b是A的子序列,把不在b中的元素, ...
- ACM学习历程—Hihocoder [Offer收割]编程练习赛1
比赛链接:http://hihocoder.com/contest/hihointerview3/problem/1 大概有一个月没怎么打算法了.这一场的前一场BC,也打的不是很好.本来Div1的A和 ...
- hihoCoder[Offer收割]编程练习赛1题目解析
题目1 : 九宫 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描写叙述 小Hi近期在教邻居家的小朋友小学奥数.而近期正好讲述到了三阶幻方这个部分,三阶幻方指的是将1~9不反 ...
- hihocoder offer收割编程练习赛8 C 数组分拆
思路:(引自bfsoyc的回答:http://hihocoder.com/discuss/question/4160) 动态规划.状态dp[i]表示 前i个数的合法的方案数,转移是 dp[i] = s ...
- hihocoder [Offer收割]编程练习赛18 C 最美和弦(dp)
题目链接:http://hihocoder.com/problemset/problem/1532 题解:一道基础的dp,设dp[i][j][k][l]表示处理到第几个数,当前是哪个和弦错了几次初始x ...
- hihoCoder [Offer收割]编程练习赛3 D子矩阵求和
子矩阵求和 http://hihocoder.com/discuss/question/3005 声明一下: n是和x一起的,m是和y一起的 x是横着的,y是纵着的,x往右为正,y往下为正 (非常反常 ...
- hihocoder [Offer收割]编程练习赛52 D 部门聚会
看了题目的讨论才会做的 首先一点,算每条边(u, v)对于n*(n+1)/2种[l, r]组合的贡献 正着算不如反着算 哪些[l, r]的组合没有包含这条边(u, v)呢 这个很好算 只需要统计u这半 ...
- hihocoder [Offer收割]编程练习赛14
A.小Hi和小Ho的礼物 谜之第1题,明明是第1题AC率比C还要低.题目是求在n个不同重量袋子选4袋,2袋给A,2袋给B,使2人获得重量相同,求问方案数. 我也是一脸懵b...o(n2)暴力枚举发现把 ...
随机推荐
- 三、Scrapy中选择器用法
官方示例源码<html> <head> <base href='http://example.com/' /> <title>Example web ...
- (ccf)201709-4 通信网络
#include<iostream> #include<memory.h> #include<stack> #include<string> #incl ...
- Centos下安装mysql(二进制版)
1.下载安装包,选择相应的平台.版本,比如,选择64位Linux平台下的MySQL二进制包“Linux-Generic (glibc 2.5)(x86,64-bit),Compressed” 如:#w ...
- cuda npp库旋转图片
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <assert.h&g ...
- 集群管理软件clustershell
一.简介 1.安装方便.一条指令就能轻松安装. 2.配置方便.很多集群管理软件都需要在所有的服务器上都安装软件,而且还要进行很多的连接操作,clustershell就相当的方便了,仅仅需要所有机器能够 ...
- Anton and Permutation
Anton and Permutation time limit per test 4 seconds memory limit per test 512 megabytes input standa ...
- 如何修改cnblogs的文本编辑器
我发现从别处复制过来的文本,文字上下间隔太大,所以我点击“html”标签,然后,把html内容复杂到记事本里,用记事本替换的功能,把“<p>”替换为“<p style="p ...
- href=#与 href=javascript:void(0) 的区别
<a href="#"> 点击链接后,页面会向上滚到页首,# 默认锚点为 #TOP <a href="javascript:void(0)" ...
- hibernate分表保存日志
@Service("accessLogService")@Transactionalpublic class LogMessageServiceImpl extends BaseD ...
- EPEL reporsitory
在centos 5上yum install git的时候报错说没有git这个package. 这是因为centos的软件策略非常保守,因为它基本就是redhat企业版的copy.所以想在centos5 ...