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;
}
}
B. Maximum Value

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.

Input

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);
}
D. Kindergarten

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.

Input

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).

Output

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)的更多相关文章

  1. Codeforces Round #276 (Div. 1) D. Kindergarten dp

    D. Kindergarten Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/proble ...

  2. 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 ...

  3. 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 ...

  4. Codeforces Round #276 (Div. 2) 解题报告

    题目地址:http://codeforces.com/contest/485 A题.Factory 模拟.判断是否出现循环,如果出现,肯定不可能. 代码: #include<cstdio> ...

  5. Codeforces Round #276 (Div. 1) E. Sign on Fence (二分答案 主席树 区间合并)

    链接:http://codeforces.com/contest/484/problem/E 题意: 给你n个数的,每个数代表高度: 再给出m个询问,每次询问[l,r]区间内连续w个数的最大的最小值: ...

  6. CF&&CC百套计划4 Codeforces Round #276 (Div. 1) A. Bits

    http://codeforces.com/contest/484/problem/A 题意: 询问[a,b]中二进制位1最多且最小的数 贪心,假设开始每一位都是1 从高位i开始枚举, 如果当前数&g ...

  7. 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的数 维护 ...

  8. codeforces 484C Strange Sorting Codeforces Round #276 (Div. 1) C

    思路:首先 他是对1到k 元素做一次变换,然后对2到k+1个元素做一次变化....依次做完. 如果我们对1到k个元素做完一次变换后,把整个数组循环左移一个.那么第二次还是对1 到 k个元素做和第一次一 ...

  9. 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 ...

  10. Codeforces Round #276 (Div. 2)

    A. Factory 题意:给出a,m,第一天的总量为a,需要生产为a%m,第二天的总量为a+a%m,需要生产(a+a%m)%m 计算到哪一天a%m==0为止 自己做的时候,把i开到1000来循环就过 ...

随机推荐

  1. Redis同步(主从复制)

    目录1.Replication的工作原理2.如何配置Redis主从复制3.应用示例 1.Replication的工作原理在Slave启动并连接到Master之后,它将主动发送一条SYNC命令.此后Ma ...

  2. PHP iconv()编码转换函数用法示例

    PHP iconv()字符编码转换函数的用法,iconv()函数,在php5中是内置的,语法格式:iconv("UTF- 8","GB2312//IGNORE" ...

  3. MySQL添加中文字符集问题 --- java.sql.SQLException: Incorrect string value

    今天在做开关记录的时候,数据库表中description字段用于记录当前版本的描述(需要存储中文),在测试程序的时候发现如果用户输入中文,那么后台会报错.错误信息如下: java.sql.SQLExc ...

  4. Swift—类的继承-备

    Swift中的继承只能发生在类上,不能发生在枚举和结构体上.一个类可以继承另一个类的方法.属性.下标等特征,当一个类继承其他类时,继承类叫子类,被继承类叫父类(或超类).子类继承父类后,可以重写父类的 ...

  5. Google Noto Sans CJK 字体

    下载链接:https://code.google.com/p/noto/source/browse/third_party/noto_cjk 等自己安装完成,再来个体验说明.

  6. angular.forEach

    if($scope.jsList.length>0){ angular.forEach($scope.jsList,function(item){ if(item._id==jsid){ jsn ...

  7. C++ 智能指针auto_ptr

    template<class T> class auto_ptr { public: ); // Item M5 有“explicitfor”// 的描述 template<clas ...

  8. day03_javaEE四成结构

  9. 【HDOJ】1011 Starship Troopers

    第一道树形DP.很容易理解. #include <cstdio> #include <cstring> #include <cstdlib> #define MAX ...

  10. Android UI 之TextView控件属性列表

    在网上收集到了TextView 的属性,在开发过程中还是挺有用的. android:autoLink 设置是否当文本为URL链接/email/电话号码/map时,文本显示为可点击的链接.可选值(non ...