栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

poj 3399 Product

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

poj 3399 Product

#include "iostream"#include "cstdio"#include "algorithm"#include "vector"using namespace std;int N,K;vector<int> pos,neg;vector<int> ans;void input(){    scanf("%d%d",&N,&K);    for(int i = 0 ;i < N; ++i){        int tmp;        scanf("%d",&tmp);        if(tmp >= 0) pos.push_back(tmp);        else neg.push_back(tmp);    }}void calc(){    int ret = 1;    sort(pos.begin(),pos.end());    sort(neg.begin(),neg.end());    int i = pos.size()-1;    int j = 0,cnt = 0;    if(neg.size() == 0){        while(cnt < K){ ans.push_back(pos[i]); --i; ++cnt;        }        return;    }else if(pos.size() == 0){        if(K & 1){ for(int  k = neg.size() -1; cnt < K; --k){     ans.push_back(neg[k]);     ++cnt; }        }else{ for(int k = 0 ; cnt < K; ++k){     ans.push_back(neg[k]);     ++cnt; }        }        return;    }    if(K % 2){        ans.push_back(pos[i]);        --i;        cnt = 1;    }    while(cnt < K && i >= 1 && j < neg.size() -1){        if(pos[i] * pos[i-1] >= neg[j] * neg[j +1]){ ans.push_back(pos[i]); ans.push_back(pos[i-1]); cnt += 2; i-=2;        }else{ ans.push_back(neg[j]); ans.push_back(neg[j+1]); cnt += 2; j+=2;        }    }    while(cnt < K){        while(i == 0){ if(j < neg.size() -1 && neg[j] * neg[j + 1] >= pos[i]){     ans.push_back(neg[j]);     ans.push_back(neg[j + 1]);     j += 2;     cnt +=2; }else{     ans.push_back(pos[i]);     ++cnt;     --i; }        }        while(cnt < K){ ans.push_back(neg[j]); ++j; ++cnt;        }    }}int main(){    input();    calc();    for(int i = 0 ;i < ans.size(); ++i){        if(i == ans.size() -1) printf("%dn",ans[i]);        else printf("%d ",ans[i]);    }    return 0;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/379639.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号