zoukankan      html  css  js  c++  java
  • N^2取N

    序列合并

    有两个长度都是N的序列A和B,在A和B中各取一个数相加可以得到N^2个和,求这N^2个和中最小的N个。

    先把A B排序 然后pushA[1]+B[i](1<=i<=n)每次取出一个最小的之后把b[i]和i对应的下个a相加push

    #include <iostream>
    #include <cstdio>
    #include <vector>
    #include <queue>
    using namespace std;
    typedef pair<int,int> pii;
    const int maxn = 1e5+7;
    int a[maxn],b[maxn],mark[maxn];
    int main(){
        priority_queue<pii,vector<pii>,greater<pii> >Q;
        int N;
        cin>>N;
        for(int i = 1;i <= N;++i)
            scanf("%d",&a[i]);
        for(int i = 1;i <= N;++i)
            scanf("%d",&b[i]);
        for(int i = 1;i <= N;++i){
            mark[i] = 1;
            Q.push(make_pair(a[1]+b[i],i));
        }
        while(N--){
            pii p = Q.top();
            Q.pop();
            printf("%d ",p.first);
            p.first = b[p.second] + a[++mark[p.second]];
            Q.push(p);
        }
    }
    View Code

    N^2相乘的话 加个log

    取最大的话就加负号

    最小函数值

    有n个函数,分别为F1,F2,...,Fn。定义Fi(x)=Ai*x^2+Bi*x+Ci (x∈N*)。给定这些Ai、Bi和Ci,请求出所有函数的所有函数值中最小的m个(如有重复的要输出多个)。

    #include <cstdio>
    #include <queue>
    #include <cmath>
    #include <vector>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    
    using namespace std;
    
    const int maxn = 100010;
    
    priority_queue < pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > q; //让优先队列从小到大弹出
    int n, m,x[maxn];
    
    struct node
    {
        int a, b, c;
    }f[maxn];
    
    int jisuan(int i, int x)
    {
        return x * x * f[i].a + x * f[i].b + f[i].c;
    }
    
    int main()
    {
        scanf("%d%d", &n, &m);
        for (int i = 1; i <= n; i++)
            scanf("%d%d%d", &f[i].a, &f[i].b, &f[i].c);
        for (int i = 1; i <= n; i++)
            x[i] = 1;
        for (int i = 1; i <= n; i++)
        {
            int temp = jisuan(i, 1);
            q.push(pair<int,int>(temp,i));
        }
        for (int i = 1; i <= m; i++)
        {
            pair<int,int> temp = q.top();
            q.pop();
            int nextx = temp.second;
            printf("%d ", temp.first);
            x[nextx]++;
            q.push(pair<int, int>(jisuan(nextx, x[nextx]), nextx));
        }
    
        return 0;
    }
    View Code
  • 相关阅读:
    TortoiseSVN 使用详细步骤(三):安装
    TortoiseSVN使用详细步骤(二)
    TortoiseSVN使用详细步骤(一)
    IIS7下访问ashx页面,显示404
    Learning Python 008 正则表达式-003 search()方法
    Learning Python 008 正则表达式-002 findall()方法
    Learning Python 008 正则表达式-001
    Learning Python 007 基本语句
    Learning Python 006 list(列表) 和 tuple(元组)
    Learning Python 005 字符串和编码
  • 原文地址:https://www.cnblogs.com/Aragaki/p/8661032.html
Copyright © 2011-2022 走看看