Codeforces Round #276 (Div. 1)
a.
给俩数, 求他俩之间二进制数中1最多的,有多个输出最小的;
贪心,从小到大加能加就加,最后可能碰到一个不能加了但是当前数比l小,那么就加上这个数,然后从大到小,能减就减,见到符合条件
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int main(){
long long n, l, r;
long long a[];
long long s = , maxa = 1e18;
long long u;
for(long long i = ;; i++){
a[i] = s;
s*=;
u = i;
if(s > maxa)break;
}
//for(int i = 0; i <= u; i++)printf("%lld ", a[i]);
cin>>n;
while(n--){
cin>>l>>r;//printf("*%d", u);
long long sum = ;
for(int i = ; i <= u; i++){
if(sum + a[i] <= r){//cout<<sum<<" "<<a[i]<<endl;
sum += a[i];
}else if(sum < l){
sum += a[i];
for(int k = i-; k >= ; k--){
if(sum - a[k] >= l){
sum -= a[k];
}
if(l <= sum && sum <= r)
break;
}break;
}
}
cout<<sum<<endl;
}
}
You are given a sequence a consisting of n integers. Find the maximum possible value of (integer remainder of ai divided by aj), where 1 ≤ i, j ≤ n and ai ≥ aj.
The first line contains integer n — the length of the sequence (1 ≤ n ≤ 2·105).
The second line contains n space-separated integers ai (1 ≤ ai ≤ 106).
想了半天决定看别人代码, 果然在ai的数据范围上做文章了,就是每次处理出来小于等于i的数中最大的保存到一个数组里,然后找每个小于ai的n倍-1的最大数字,取膜
#include<iostream>
#include<stdio.h>
using namespace std;
const int maxa = ;
int a[maxa], b[maxa];
int main(){
int n;
scanf("%d", &n);
while(n--){int x;scanf("%d", &x);a[x] = x;};
for(int i = ; i < maxa; i++) b[i] = max(b[i-],a[i]);
int maxn = ;
for(int i = ; i < maxa; i++) if(a[i])
for(int k = *i; k < maxa; k+= i) maxn = max(maxn, b[k-] % i);
printf("%d", maxn);
}
In a kindergarten, the children are being divided into groups. The teacher put the children in a line and associated each child with his or her integer charisma value. Each child should go to exactly one group. Each group should be a nonempty segment of consecutive children of a line. A group's sociability is the maximum difference of charisma of two children in the group (in particular, if the group consists of one child, its sociability equals a zero).
The teacher wants to divide the children into some number of groups in such way that the total sociability of the groups is maximum. Help him find this value.
The first line contains integer n — the number of children in the line (1 ≤ n ≤ 106).
The second line contains n integers ai — the charisma of the i-th child ( - 109 ≤ ai ≤ 109).
Print the maximum possible total sociability of all groups.
简单的来说题意就是给你一段数字,让你分成任意段,每段的值是这一段中的最大值减去最小值,求所有段的值的最大和;
好吧这题也是看别人的代码看了半天才懂;
由于每段的值是最大值-最小值
我们这样定义三个状态,dp[i][1]代表到以i结尾的值;
dp[i][0]代表当前段的最大值已经确定的最大值(这里的值是确定的最大值+前面区段的值)
dp[i][2]代表当前段的最小值是a[i]的值减去a[i];(同上)
那么dp[i][0]可以由dp[i-1][0]过来代表此数字被放弃了,也可以由dp[i-1][1]推过来代表当前区段的最大值为当前数,在这两个中取最大值;
dp[i][1],dp[i][2]同上
#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
const long long maxa = ;
long long a[maxa];
long long dp[maxa][];
int main(){
long long n;
cin>>n;
long long x;
//scanf("%d", &x);
cin>>x;
dp[][] = x;
dp[][] = -x;
for(long long i = ; i <= n; i++){
cin>>x;
for(long long k = ; k < ; k++){
dp[i][k] = dp[i-][k];
if(k > ) dp[i][k] = max(dp[i][k], dp[i-][k-] - x);
if(k < ) dp[i][k] = max(dp[i][k], dp[i-][k+] +x);
//printf("%d ", dp[i][k]);
}//puts("");
}cout<<dp[n][]<<endl;
}
Codeforces Round #276 (Div. 1)的更多相关文章
- Codeforces Round #276 (Div. 1) D. Kindergarten dp
D. Kindergarten Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/proble ...
- Codeforces Round #276 (Div. 1) B. Maximum Value 筛倍数
B. Maximum Value Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/484/prob ...
- Codeforces Round #276 (Div. 1) A. Bits 二进制 贪心
A. Bits Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/484/problem/A Des ...
- Codeforces Round #276 (Div. 2) 解题报告
题目地址:http://codeforces.com/contest/485 A题.Factory 模拟.判断是否出现循环,如果出现,肯定不可能. 代码: #include<cstdio> ...
- Codeforces Round #276 (Div. 1) E. Sign on Fence (二分答案 主席树 区间合并)
链接:http://codeforces.com/contest/484/problem/E 题意: 给你n个数的,每个数代表高度: 再给出m个询问,每次询问[l,r]区间内连续w个数的最大的最小值: ...
- CF&&CC百套计划4 Codeforces Round #276 (Div. 1) A. Bits
http://codeforces.com/contest/484/problem/A 题意: 询问[a,b]中二进制位1最多且最小的数 贪心,假设开始每一位都是1 从高位i开始枚举, 如果当前数&g ...
- CF&&CC百套计划4 Codeforces Round #276 (Div. 1) E. Sign on Fence
http://codeforces.com/contest/484/problem/E 题意: 给出n个数,查询最大的在区间[l,r]内,长为w的子区间的最小值 第i棵线段树表示>=i的数 维护 ...
- codeforces 484C Strange Sorting Codeforces Round #276 (Div. 1) C
思路:首先 他是对1到k 元素做一次变换,然后对2到k+1个元素做一次变化....依次做完. 如果我们对1到k个元素做完一次变换后,把整个数组循环左移一个.那么第二次还是对1 到 k个元素做和第一次一 ...
- Codeforces Round #276 (Div. 1) E. Sign on Fence 二分+主席树
E. Sign on Fence Bizon the Champion has recently finished painting his wood fence. The fence consi ...
- Codeforces Round #276 (Div. 2)
A. Factory 题意:给出a,m,第一天的总量为a,需要生产为a%m,第二天的总量为a+a%m,需要生产(a+a%m)%m 计算到哪一天a%m==0为止 自己做的时候,把i开到1000来循环就过 ...
随机推荐
- validate插件深入篇
1.使用valid()来验证表单是否填写正确: <form id="mainform"> <button id="check">< ...
- apache 2.4.9 配置其他客户端访问 required all granted
<Directory /> AllowOverride all #修改地方 Require all granted </Directory> # # Note that fro ...
- xml约束之schema
使用名称空间引入Schema : 通常需要在Xml文档中的根结点中使用schemaLocation属性来指定. <itcast:书架 xmlns:itcast="http://www. ...
- C语言学习笔记--指针与字符串
字符类型 char(character)是一种整数,也是一种特殊的类型:字符.这是因为 ① 用单引号表示的字符字符字面量:‘a’,'1' ②‘’也是一个字符 ③printf和scanf里用%c来输入. ...
- ListView的getFirstVisiblePosition等方法返回的是哪个对象
int firstPosition = lisView.getFirstVisiblePosition(); int lastPosition = lisView.getLastVisiblePosi ...
- 排序功能实现 jQuery实现排序 上移 下移
效果 思路, 跟相邻元素,互换sort. 前提是每一个元素都有自己的sort值,不为零. <tr id="{sh:$vo.id}"> <td> <sp ...
- EF 5.0 帮助类 增删改查
原文地址:http://www.cnblogs.com/luomingui/p/3362813.html EF 5.0 帮助类 加入命名空间: using System; using System.D ...
- HDU_2015——偶数求和
Problem Description 有一个长度为n(n<=100)的数列,该数列定义为从2开始的递增有序偶数,现在要求你按照顺序每m个数求出一个平均值,如果最后不足m个,则以实际数量求平均值 ...
- Raid1源代码分析--写流程
正确写流程的总体步骤是,raid1接收上层的写bio,申请一个r1_bio结构,将其中的所有bios[]指向该bio.假设盘阵中有N块盘.然后克隆N份上层的bio结构,并分别将每个bios[]指向克隆 ...
- JAVA的四种引用,强弱软虚用到的场景
1.强引用 最常用的引用类型,如Object object = new Object(),只要强引用存在,GC必定 不回收,即使当前内存空间不足,jAVA虚拟机宁愿抛出OutofMemoryError ...