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. Backbone学习笔记

    model model的get和set是对model.attributes进行操作,并不是直接对model进行操作 collection collection.set()会触发相应的add,remov ...

  2. yarn资源调度(网络搜集)

    本文转自:http://www.itweet.cn/2015/07/24/yarn-resources-manager-allocation/ Hadoop YARN同时支持内存和CPU两种资源的调度 ...

  3. cat查看proc下文件帮助

    cat boot_mode 查看cpu版本 cat cmdline cat cpuinfo 查看cup详细信息 cat devices cat diskstats cat dma-mappings c ...

  4. c# 6.0 学习笔记

    refer : http://www.cnblogs.com/yinrq/p/5600530.html http://www.cnblogs.com/wolf-sun/p/5168217.html h ...

  5. LeetCode _ Copy List with Random Pointer

    A linked list is given such that each node contains an additional random pointer which could point t ...

  6. 一道考验你设计能力的C++编程题

    http://www.cppblog.com/weiym/archive/2012/06/12/178472.html

  7. Android 自定义属性(attrs.xml,TypedArray)

    做Android布局是件很享受的事,这得益于他良好的xml方式.使用xml可以快速有效的为软件定义界面.可是有时候我们总感觉官方定义的一些基本组 件不够用,自定义组件就不可避免了.那么如何才能做到像官 ...

  8. HDFS文件读取详解

    客户端与HDFS文件读取 创建HDFS文件系统实例 FileSystem fs = FileSystem.get(new URI("hdfs://ns1"), new Config ...

  9. 转:Java生成图片验证码(有点仿QQ验证码的意思)

    http://blog.csdn.net/ruixue0117/article/details/22829557 java: VerifyCodeUtils.java package com.fro. ...

  10. C++编程规范之20:避免函数过长,避免嵌套过深

    摘要: 短胜于长,平胜于优,过长的函数和嵌套过深的代码块的出现,经常是因为没能赋予一个函数以一个紧凑的职责所致,这两种情况通常都能够通过更好的重构予以解决. 每个函数都应该顾其名而能知其义,易于理解的 ...