题意:从数组中选几个(任意),使他们的和modm的值最大

题解:我一开始是直接暴力写,然后就会t

   其实这题可以用二分的方法写,一半数组的值用来遍历,一般数组的值用来查询。

   二分查询就能把时间继续缩短

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=;
const ll inf=1e9;
set<int> st[],temp;
int n,m,a[];
int main(){
//freopen("in.txt","r",stdin);
cin >> n >> m;
for (int i=;i<n;i++) cin >> a[i]; set<int>::iterator it;
for(int i=;i<n/;i++){
temp.clear();
for(it=st[].begin();it!=st[].end();it++){
temp.insert((*it+a[i])%m);
}
for(it=temp.begin();it!=temp.end();it++){
st[].insert(*it);
}
st[].insert(a[i]%m);
}
for(int i=n/;i<n;i++){
temp.clear();
for(it=st[].begin();it!=st[].end();it++){
temp.insert((*it+a[i])%m);
}
for(it=temp.begin();it!=temp.end();it++){
st[].insert(*it);
}
st[].insert(a[i]%m);
}
st[].insert();
st[].insert(); int ans = ;
for (it=st[].begin();it!=st[].end();it++){
//在st[1]中找有无和i配对使值为m-1(或更小)
int x = m - *it - ;
set<int>::iterator itt = st[] . upper_bound(x);
itt--;
ans=max(ans,*itt+*it);
}
cout << ans << endl;
return ;
}

Educational Codeforces Round 32 E 二分的更多相关文章

  1. Educational Codeforces Round 32

    http://codeforces.com/contest/888 A Local Extrema[水] [题意]:计算极值点个数 [分析]:除了第一个最后一个外,遇到极值点ans++,包括极大和极小 ...

  2. Educational Codeforces Round 32 Maximum Subsequence CodeForces - 888E (meet-in-the-middle,二分,枚举)

    You are given an array a consisting of n integers, and additionally an integer m. You have to choose ...

  3. Educational Codeforces Round 61 D 二分 + 线段树

    https://codeforces.com/contest/1132/problem/D 二分 + 线段树(弃用结构体型线段树) 题意 有n台电脑,只有一个充电器,每台电脑一开始有a[i]电量,每秒 ...

  4. Educational Codeforces Round 15 C 二分

    C. Cellular Network time limit per test 3 seconds memory limit per test 256 megabytes input standard ...

  5. Educational Codeforces Round 32 E. Maximum Subsequence

    题目链接 题意:给你两个数n,m,和一个大小为n的数组. 让你在数组找一些数使得这些数的和模m最大. 解法:考虑 dfs但是,数据范围不允许纯暴力,那考虑一下折半搜索,一个从头开始往中间搜,一个从后往 ...

  6. Educational Codeforces Round 32 Problem 888C - K-Dominant Character

    1) Link to the problem: http://codeforces.com/contest/888/problem/C 2) Description: You are given a ...

  7. Educational Codeforces Round 32:E. Maximum Subsequence(Meet-in-the-middle)

    题目链接:E. Maximum Subsequence 用了一个Meet-in-the-middle的技巧,还是第一次用到这个技巧,其实这个技巧和二分很像,主要是在dfs中,如果数量减小一半可以节约很 ...

  8. Educational Codeforces Round 32 Almost Identity Permutations CodeForces - 888D (组合数学)

    A permutation p of size n is an array such that every integer from 1 to n occurs exactly once in thi ...

  9. [Educational Codeforces Round 16]D. Two Arithmetic Progressions

    [Educational Codeforces Round 16]D. Two Arithmetic Progressions 试题描述 You are given two arithmetic pr ...

随机推荐

  1. LOJ6053 简单的函数

    题目传送门 分析: 对于这道题来说,当\(x\)为质数时: \(~~~~f(x)=x-1+2[x=2]\) 因为除2以外的质数都是奇数,它们与1异或就是减一,然后2就是加一 然后我们先来康康怎么快速求 ...

  2. pku-3321 Apple Tree(dfs序+树状数组)

    Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow ...

  3. 2020年薪30W的Java程序员都要求熟悉JVM与性能调优!

    前言 作为Java程序员,你有没有被JVM伤害过?面试的时候是否碰到过对JVM的灵魂拷问?   一.JVM 内存区域划分 1.程序计数器(线程私有) 程序计数器(Program Counter Reg ...

  4. day03_流程控制语句

    day03_流程控制语句 建议: ​ 凡是次数确定的场景多用for循环,否则用while循环. 三元运算符 ​ 由?:符号表示的,具体的含义其实就和if-else结构的含义差不多,这种运算符会将某个条 ...

  5. YUM安装软件提示[Errno 14] curl#6 - "Could not resolve host: mirrors.cloud.aliyuncs.com; 未知的错误"

    报错如下: 翻译过来就是 所以问题很简单: 第一种情况:本地DNS服务错误 第二种情况:远程主机挂了 第三种情况:远程主机地址配置错误 第一种解决方法:换DNS 首先验证一下是不是本地DNS错误 从上 ...

  6. Dubbo学习系列之十七(微服务Soul网关)

    论起微服务,哪能不谈网关,老将有Zuul,后继有Gateway,但这些都和SpringCloud关系密切,其他网关如Kong,因Lua原因,玩起来略不顺手.这不,就来了个Soul,我顺便拿来整进了我在 ...

  7. Java中的8种基本数据类型

    JAVA中的8种基本数据类型:byte short int long float double char boolean 特别说明: 1)char类型占2个字节,可以表示汉字.汉字和英文字符都占2个字 ...

  8. WeChall_Encodings: URL (Training, Encoding)

    Your task is to decode the following: %59%69%70%70%65%68%21%20%59%6F%75%72%20%55%52%4C%20%69%73%20%6 ...

  9. HDU_2510_打表

    http://acm.hdu.edu.cn/showproblem.php?pid=2510 dfs打表. #include<iostream> #include<cstdio> ...

  10. <七>对于之前的一些遗漏的地方的补充

    1.线程的状态: 我们可以通过wait,start,notify等关键字来切换线程的状态,但是我们如何知道线程目前是处于哪一种状态呢?使用Thread.getState()来获取,有下面几种常见的状态 ...