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 ...
随机推荐
- 面试题目——《CC150》数组与字符串
面试题1.1:实现一个算法,确定一个字符串的所有字符是否全都不同.假使不允许使用额外的数据结构,又该如何处理? 注意:ASCII字符共有255个,其中0-127的字符有字符表 第一种解法:是<C ...
- 在.net中使用GAC
转自:http://blog.log4d.com/2011/01/gac/ GAC GAC是什么?是用来干嘛的?GAC的全称叫做全局程序集缓存,通俗的理解就是存放各种.net平台下面需要使用的dll的 ...
- jQuery知识点一 each()和toggleClass()
jQuery的一些东东比较容易忘,所以在这里整理一下... ... 1. each (1) $(selector).each(function(index,element)) inde ...
- Storm 单机版环境搭建
1 需要安装的软件 要使用storm首先要安装以下工具:python.zookeeper.zeromq.jzmq.storm 1.1 安装zeromq wget http://download.zer ...
- openjdk 完全编译指南
从openjdk.java.net下载openjdk的软件包,你就获得了所有相关的源码. 强烈建议首先仔细看懂 README-builds.html 指南. 在执行 make all 之前,首先要 执 ...
- I18N 国际化
http://blog.sina.com.cn/s/blog_6c7e59770101p7w9.html 一.I18N 在 J2EE 中的应用 [转载:http://blog.csdn.net/cha ...
- 双击导航栏自动滑动ListView到顶部
有些app都实现了双击导航栏让页面的list自动滑动到顶部的feature. 先实现一个继承于OnTouchListener的监听多次点击事件的监听器,通过callback把连续点击的次数返回给客户代 ...
- Java连接mysql数据库并插入中文数据显示乱码
连接数据库设置编码 jdbc:mysql://地址:3306/数据库名?characterEncoding=utf8
- HTML5安全:CORS(跨域资源共享)简介。。。ie67不要想了。。。
来源:http://blog.csdn.net/hfahe/article/details/7730944 前言:像CORS对于现代前端这么重要的技术在国内基本上居然很少有人使用和提及,在百度或者Go ...
- robotframework----模板的使用
模板只能放在测试用例里,可以测试大数据量,每一行模板的值,都做为用户关键字的输入参数,发下图: 删除特定证书2 的用户关键字如下: