码迷,mamicode.com
首页 > 其他好文 > 详细

Educational Codeforces Round 32 E 二分

时间:2020-02-14 16:56:43      阅读:73      评论:0      收藏:0      [点我收藏+]

标签:直接   def   col   ++   out   main   就会   inf   cat   

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

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

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

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

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

 

 

Educational Codeforces Round 32 E 二分

标签:直接   def   col   ++   out   main   就会   inf   cat   

原文地址:https://www.cnblogs.com/amitherblogs/p/12307606.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!