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来循环就过 ...
随机推荐
- 光盘卡在MacBook里退不出来咋办?
如果光盘推不出来了怎么办?很多同学想到的是:上针!不过这招对MacBook Pro毫无用处,因为没有给你插针的地方,没有机械按键,只有键盘右上角一个推出的快捷键,不过在光盘卡在光驱里时,按此健基本无效 ...
- android使用apktool反编译出现Input file (d:\t) was not found or was not readable
Input file (d:\t) was not found or was not readable 出现这个错误是因为apktool压缩包下载错误,我是下成首页的那个压缩包了 正确下载地址:htt ...
- hdu 2992 Hotel booking
http://acm.hdu.edu.cn/showproblem.php?pid=2992 #include <cstdio> #include <cstring> #inc ...
- C#控制生成图片的大小
private void button1_Click(object sender, EventArgs e) { using (Bitmap bitmap = new Bitmap("d:\ ...
- 让自己的C++程序(非服务程序)运行为一个windows service
因为项目的一些变化和原因,需要把数据处理的一个后台程序创建为一个windows服务,运行以下命令能创建成功: sc create "MyApp Service Name" binP ...
- HttpClient 发送 HTTP、HTTPS 请求的简单封装
import org.apache.commons.io.IOUtils; import org.apache.http.HttpEntity; import org.apache.http.Http ...
- BZOJ3297: [USACO2011 Open]forgot
3297: [USACO2011 Open]forgot Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 54 Solved: 38[Submit][ ...
- win7下自写驱动导致开机蓝屏调试过程
之前没有接触过驱动调试.这里上手就要解决一个因为某个自定义驱动导致的系统登陆后蓝屏问题,记录下来. 问题: 从客户那边弄来的一个虚拟机,已知是加了我们的驱动之后才会导致蓝屏. 解决过程: 使用 ...
- hdu3534,个人认为很经典的树形dp
题目大意为,求一个树的直径(最长路),以及直径的数量 朴素的dp只能找出某点开始的最长路径,但这个最长路径却不一定是树的直径,本弱先开始就想简单了,一直wa 直到我看了某位大牛的题解... 按照那位大 ...
- oracle修改服务器端编码
因为装的是oracle 11g免费版,没有装oracle客户端,然后从网上找了免客户端使用pl/sql的教程,具体可以看链接,这里不再累述:但打开pl/sql的时候提示客户端和服务端编码不一致:网上一 ...