Codeforces Round #FF(255) DIV2
A - DZY Loves Hash
水题,开辟一个数组即可
#include <iostream>
#include <vector>
#include <algorithm>
#include <string> using namespace std; int main(){
int p,n;
cin >> p >> n;
vector<bool> buckets(,false);
bool flag = false;
vector<int > x(n);
for(int i = ; i < n ; ++i) cin >> x[i];
int i = ;
for(i = ; i < n; ++i){
if(!buckets[x[i]%p]) buckets[x[i]%p] = true;
else{ cout<<i+<<endl; break;}
}
if(i >= n) cout<<-<<endl;
}
开辟一个数组即可
B - DZY Loves Strings
先把给定的字符的值求出来,然后插入权重最大的值即可
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
using namespace std; int main(){
string s;
int k;
vector<int> w(,);
cin >>s >> k;
for(int i = ; i < ; ++ i) cin >>w[i];
long long res = ;
for(int i = ;i < s.length(); ++ i){
res+=w[s[i]-'a']*(i+);
}
sort(w.begin(),w.end());
for(int i =s.length(); i < s.length()+k; ++ i){
res+=w[]*(i+);
}
cout<<res<<endl;
}
C - DZY Loves Sequences
题目的意思是给定一个序列,然后找出一个子序列,改变子序列的一个值,使该子序列严格单调递增,求出满足上述要求最长子序列的长度。
注意一定要是单调递增
思路是将数组分块,每一块都是严格单调递增的
如 7 2 3 1 5 6
分组后为 [7], [2,3], [1,5,6],影响长度的是组与组之间的间隔
现在记录下每一个组的开始索引和结束索引,以及长度,所求最大子序列长度有三种可能
(1)如果该数组只有一个分组,则该长度就是所求结果的长度
(2)max(每个分组的长度+1),即就是每个分组的长度+改变与其相邻元素的值的最大值
(3)两个分组合并后的值即max(分组 i + 分组 i+1 )的值,注意这里分组有两种情况种情况
假设分组后两组元素为[astart1 .... aend1], [astart2 ..... aend2],注意这两组元素是相邻的即 start2 == end1+1
要满足严格单调递增的情况必须满足 astart2+1-aend1 > 1 或者 astart2 - aend1-1 >1, 要像下面的用例一样[1,2,5],[4,5,7]即可
如果下面的用例
a、[1,2,4],[3,6,7]这两个分组无法合并 ,因为astart2 -aend1-1 <=1
b、[1,2,5],[3,5,7]这两个分组无法合并, 因为astart2+1-aend1 <=1
#include <iostream>
#include <vector>
#include <algorithm>
#include <string> using namespace std; struct Node{
int startIdx;
int endIdx;
Node(int a = ,int b = ): startIdx(a),endIdx(b){};
int getLength(){return endIdx-startIdx+;}
}; int main(){
int n;
cin >>n;
vector<int> a(n+,);
vector<Node> aux;
for(int i = ;i <=n ; ++i) cin >> a[i];
int startIdx = , maxLength = ;
bool flag = false;
for(int i = ; i < n ; ++i){
if(a[i] < a[i+]){
if(!flag) {startIdx = i;flag = true;}
}else{
aux.push_back(Node(startIdx,i));
maxLength = max(maxLength,i-startIdx+);
startIdx = i+;
flag = false;
}
}
if(startIdx == n ) {aux.push_back(Node(n,n));maxLength = max(maxLength,);}
else {aux.push_back(Node(startIdx,n));maxLength=max(maxLength,n-startIdx+);}
for(int i = ; i < aux.size()-; ++ i){
if(aux[i+].startIdx+<=aux[i+].endIdx && a[aux[i+].startIdx+]-a[aux[i].endIdx] > ) maxLength =max(maxLength,aux[i+].getLength()+aux[i].getLength());
if(aux[i].endIdx->=aux[i].startIdx && a[aux[i+].startIdx]-a[aux[i].endIdx-] > ) maxLength =max(maxLength,aux[i+].getLength()+aux[i].getLength());
maxLength =max(maxLength,aux[i].getLength()+);
}
if(aux.size() > ) maxLength =max(maxLength,aux[aux.size()-].getLength()+);
cout<<maxLength<<endl;
}
D - DZY Loves Modification
题目的意思是有一个nxm的矩阵,通过k次操作修改这个矩阵,每次操作包含下面任何一个:
- 挑选某行,然后累加该行获得pleasure值,然后该行的每个元素都减去p
- 挑选某列,然后累加该列获得pleasure值,然后该列的每个元素都减去p
通过k次操作后,求所有操作pleasure值最大是多少?注意k的范围是10^6,不能通过暴力解决
解题思路:
这种题目可以通过手动模拟小数据,了解其过程。每次选取的行或列都应该根据贪心从大到小选取,选择行和选择列顺序是无关的
设选取了 i 行,则选取了k-i 列,
假设先选取了i行,然后选取列,则选取列的时候跟选取行相交的元素多减去了p,故选取列的时候在原有列的基础上少了i*p(该列与i行肯定相交),由于列与列选取互不相影响
选取k-i列比初始列的值少了i*p*(k-i)
故只需要在初始矩阵上求出选取i行,选取k-i行的pleasure值,然后减去i*p*(k-i)即可,最后取个最大值
需要注意的地方是:每行可以操作多次
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <queue>
#include <functional>
#include <utility>
#define LL long long
using namespace std; struct Node{
int index;
LL sum;
Node(int idx = , LL su = ):index(idx), sum(su){}
bool operator < (const Node& a)const{
return sum < a.sum;
}
}; int main(){
LL n,m,k,p;
cin >>n >> m >> k >>p;
vector<vector<int> > a(n,vector<int>(m,));
for(int i = ; i <n ; ++ i){
for(int j = ; j < m ; ++ j){
cin >> a[i][j];
}
} vector<Node> rowSum(n),colSum(m);
for(int i = ; i < n; ++ i ){
LL sum = ;
for(int j = ; j < m ; ++ j) sum+=a[i][j];
rowSum[i]=Node(i,sum);
}
for(int j = ; j < m; ++ j){
LL sum = ;
for(int i = ; i < n ; ++ i) sum+=a[i][j];
colSum[j]=Node(j,sum);
} priority_queue<Node> que;
vector<LL> rowRes(k+,),colRes(k+,);
for(int i = ; i < n; ++i) que.push(rowSum[i]);
int cnt = ;
LL res = ;
while(!que.empty()&& cnt< k){
Node row =que.top(); que.pop();
cnt ++;
res+=row.sum;
rowRes[cnt] = res;
row.sum-=m*p;
que.push(row);
}
que=priority_queue<Node>();
for(int i = ; i < m; ++i) que.push(colSum[i]);
cnt = ;res = ;
while(!que.empty()&& cnt< k){
Node col =que.top(); que.pop();
cnt ++;
res+=col.sum;
colRes[cnt] = res;
col.sum-=n*p;
que.push(col);
}
LL maxRes = -1e18;
for(int i = ; i <= k; ++i){
maxRes = max(maxRes,rowRes[i]+colRes[k-i]-i*(k-i)*p);
}
cout<<maxRes<<endl; }
Codeforces Round #FF(255) DIV2的更多相关文章
- Codeforces Round #FF/#255 D DZY Loves Modification --贪心+优先队列
题意:给你一个矩阵,每次选某一行或者某一列,得到的价值为那一行或列的和,然后该行每个元素减去p.问连续取k次能得到的最大总价值为多少. 解法: 如果p=0,即永远不减数,那么最优肯定是取每行或每列那个 ...
- DP Codeforces Round #FF (Div. 1) A. DZY Loves Sequences
题目传送门 /* DP:先用l,r数组记录前缀后缀上升长度,最大值会在三种情况中产生: 1. a[i-1] + 1 < a[i+1],可以改a[i],那么值为l[i-1] + r[i+1] + ...
- Codeforces Round #FF (Div. 1) A. DZY Loves Sequences
题目链接: http://www.codeforces.com/contest/446/problem/A 题解: dp1[x]表示以x结尾的最大严格升序连续串,dp2[x]表示以x开头的最大严格升序 ...
- Codeforces Round #FF (Div. 2)__E. DZY Loves Fibonacci Numbers (CF447) 线段树
http://codeforces.com/contest/447/problem/E 题意: 给定一个数组, m次操作, 1 l r 表示区间修改, 每次 a[i] + Fibonacci[i-l ...
- Codeforces Round #FF (Div. 2) 题解
比赛链接:http://codeforces.com/contest/447 A. DZY Loves Hash time limit per test 1 second memory limit p ...
- Codeforces Round #FF (Div. 1) B. DZY Loves Modification 优先队列
B. DZY Loves Modification 题目连接: http://www.codeforces.com/contest/446/problem/B Description As we kn ...
- Codeforces Round #FF (Div. 1) A. DZY Loves Sequences 动态规划
A. DZY Loves Sequences 题目连接: http://www.codeforces.com/contest/446/problem/A Description DZY has a s ...
- Codeforces Round #329(Div2)
CodeForces 593A 题意:n个字符串,选一些字符串,在这些字符串中使得不同字母最多有两个,求满足这个条件可选得的最多字母个数. 思路:用c[i][j]统计文章中只有i,j对应两个字母出现的 ...
- Codeforces Round #328(Div2)
CodeForces 592A 题意:在8*8棋盘里,有黑白棋,F1选手(W棋往上-->最后至目标点:第1行)先走,F2选手(B棋往下-->最后至目标点:第8行)其次.棋子数不一定相等,F ...
随机推荐
- myBatis foreach详解【转】
MyBatis的foreach语句详解 foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合.foreach元素的属性主要有 item,index,collection,ope ...
- 一个类似宣传的H5页面
趁着闲置 做了一个H5的页面 感觉不错. 具体效果如下 框架上我选择 zepto(其实这个可有可无,推荐用原生的最好) FullPage (感觉挺好用的一个全屏滚动插件 ) pageResponse ...
- Segment set
题目大意: 在一个平面上,给定N根线段,若某条线段与另一条线段相交,则将它们归于同个集合,给定k,问第k条线段所在的集合中线段的数量. 题目分析: 问题主要考察计算几何和并查集. 首先我们要判断两条线 ...
- sscanf提取字符串中的数据php
1.需求 理解sscanf的作用 2.例子 $str = "age:30 weight:60kg"; sscanf($str,"age:%d weight:%dkg&qu ...
- Xcode7建立自己的自定义工程和类模板
首先进入系统模板的目录 /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library ...
- asp:DataGrid之添加asp:CheckBox做全选功能时涉及到绑值问题解决
最大的意图是为asp:CheckBox的value绑定上自己需要的value值,而不是默认的字符串"on" 参考了这篇文章带Value属性的扩展CheckBox控件,意义不大,换了 ...
- nodejs开发指南demo
由于手上拿的教程是2012年出版的,到如今已历经N个版本,所以在写代码时报过一堆错.这是解决错误后的版本. 源码下载
- Deep Learning入门视频(下)之关于《感受神经网络》两节中的代码解释
代码1如下: #深度学习入门课程之感受神经网络(上)代码解释: import numpy as np import matplotlib.pyplot as plt #matplotlib是一个库,p ...
- LVS集群之NAT模式实现
LVS集群之NAT模式实现 一.集群的种类 集群系统主要分为 1.HA:高可用集群,又叫双机热备. (a)原理 2台机器A,B,正常是A提供服务,B待命闲置,当A宕机或服务宕掉,会切换至 ...
- yii2.0 框架邮件的发送
第一步: 在main-local.php中的components中配置mailer: $config = [ 'components' => [ 'mailer' => [ 'class' ...