假如做个XX网, 当然要为金牌会员提供最优秀的资源(people)服务。
优秀资源比较规则是:
1. 有籍贯(籍贯莫有的, 不敢要)
2. 钱多
3. 籍贯为'jx'的优先, 其他...
下面程序就是求最优资源实现:
package algorithm.max;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;
/**
* max n algorithm
*
* @author yangwm Oct 23, 2010 11:36:54 PM
*/
public class MaxNAlgorithm<E> {
/**
* max + n + comparator
*
* @param <E>
* @param input
* @param n
* @param comparator
* @return
*/
public static <E> E[] max(E[] input, int n, Comparator<? super E> comparator) {
PriorityQueue<E> priorityQueue = new PriorityQueue<E>(n, comparator);
for (int i = 0; i < input.length; i++) {
priorityQueue.add(input[i]);
if (i >= n) {
priorityQueue.poll();
}
}
E[] result = newGenericArray(input[0], priorityQueue.size());
for (int i = result.length - 1; i >= 0; i--) {
result[i] = priorityQueue.poll();
}
return result;
}
/**
* max + n
*
* @param <E>
* @param input
* @param n
* @return
*/
public static <E> E[] max(E[] input, int n) {
PriorityQueue<E> priorityQueue = new PriorityQueue<E>(n);
for (int i = 0; i < input.length; i++) {
priorityQueue.add(input[i]);
if (i >= n) {
priorityQueue.poll();
}
}
E[] result = newGenericArray(input[0], priorityQueue.size());
for (int i = result.length - 1; i >= 0; i--) {
result[i] = priorityQueue.poll();
}
return result;
}
@SuppressWarnings("unchecked")
static <E> E[] newGenericArray(E e, int size) {
Class<E> type = (Class<E>) e.getClass();
return (E[]) Array.newInstance(type, size);
}
/**
* @param args
*/
public static void main(String[] args) {
Integer[] input = new Integer[] { 2, 7, 6, 8, 9, 18, 11, 5, 3, 20 };
int n = 3;
System.out.println("------MaxNAlgorithm Array max n-------");
Integer[] result = max(input, n);
System.out.println(Arrays.toString(result));
}
}
/*
------MaxNAlgorithm Array max n-------
[20, 18, 11]
*/
/**
*
*/
package algorithm.max;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;
/**
* max n algorithm beanch2
*
* @author yangwm Oct 23, 2010 11:58:58 PM
*/
public class MaxNAlgorithmBeanch2 {
/**
* @param args
*/
public static void main(String[] args) {
/*
*
*/
int inputTotal = 1000000;
int n = 100;
/*
* test data
*/
List<People> inputList = new ArrayList<People>(inputTotal);
for (int i = 0; i < inputTotal; i++) {
People people = null;
int money = i / 10;
int factor = new Random().nextInt(5) + 1;
if (factor == 1) {
people = new People(i, "jx", money);
} else if (factor == 2) {
people = new People(i, "bj", money);
} else if (factor == 3) {
people = new People(i, "sh", money);
} else {
people = new People(i, null, money);
}
inputList.add(people);
}
Collections.shuffle(inputList);
System.out.println("test begin inputTotal: " + inputTotal + ", n: " + n);
System.out.println("test data inputList.size(): " + inputList.size());
//-----------------------------------------------------------------
People[] inputArray = new People[inputList.size()];
for (int i = 0; i < inputArray.length; i++) {
inputArray[i] = inputList.get(i);
}
System.out.println("-------JDK Arrays.sort max n with Comparable-------");
/*
* max and test print
*/
long begin = System.currentTimeMillis();
Arrays.sort(inputArray);
People[] resultArray = (People[]) Array.newInstance(People.class, n);
for (int i = inputArray.length - 1, resultIdx = 0; i >= inputArray.length - n; i--) {
resultArray[resultIdx++] = inputArray[i];
}
long end = System.currentTimeMillis();
System.out.println("cosume: " + (end - begin) + " ms");
System.out.println("resultArray.length: " + resultArray.length);
System.out.println("resultArray of first ten : " + Arrays.toString(Arrays.copyOf(resultArray, 10)));
//-----------------------------------------------------------------
inputArray = new People[inputList.size()];
for (int i = 0; i < inputArray.length; i++) {
inputArray[i] = inputList.get(i);
}
System.out.println("-------MaxNAlgorithm Array max n with Comparable-------");
/*
* max and test print
*/
begin = System.currentTimeMillis();
resultArray = MaxNAlgorithm.<People>max(inputArray, n);
end = System.currentTimeMillis();
System.out.println("cosume: " + (end - begin) + " ms");
System.out.println("resultArray.length: " + resultArray.length);
System.out.println("resultArray of first ten : " + Arrays.toString(Arrays.copyOf(resultArray, 10)));
}
/**
* test object for max n algorithm beanch
*
* @author yangwm Oct 24, 2010 4:16:58 PM
*/
static class People implements Comparable<People> {
public int id;
public String province;
public int money;
public People(int id, String province, int money) {
this.id = id;
this.province = province;
this.money = money;
}
@Override
public int compareTo(People o) {
if (province != null && o.province == null) {
return 1;
} else if (province == null && o.province != null) {
return -1;
} else {
if (money > o.money) {
return 1;
} else if (money < o.money) {
return -1;
} else {
if (province == null && o.province == null) {
return 0;
} if (province.equalsIgnoreCase("jx")) {
return 1;
} else if (o.province.equalsIgnoreCase("jx")) {
return -1;
} else {
return province.compareToIgnoreCase(o.province);
}
}
}
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("id=");
sb.append(id);
sb.append(" province=");
sb.append(province);
sb.append(" money=");
sb.append(money);
return sb.toString();
}
}
}
/*
test begin inputTotal: 1000000, n: 100
test data inputList.size(): 1000000
-------JDK Arrays.sort max n with Comparable-------
cosume: 1218 ms
resultArray.length: 100
resultArray of first ten : [id=999997 province=jx money=99999, id=999993 province=jx money=99999, id=999992 province=sh money=99999, id=999996 province=sh money=99999, id=999991 province=bj money=99999, id=999990 province=bj money=99999, id=999995 province=bj money=99999, id=999989 province=jx money=99998, id=999980 province=bj money=99998, id=999987 province=bj money=99998]
-------MaxNAlgorithm Array max n with Comparable-------
cosume: 578 ms
resultArray.length: 100
resultArray of first ten : [id=999997 province=jx money=99999, id=999993 province=jx money=99999, id=999996 province=sh money=99999, id=999992 province=sh money=99999, id=999990 province=bj money=99999, id=999991 province=bj money=99999, id=999995 province=bj money=99999, id=999989 province=jx money=99998, id=999985 province=bj money=99998, id=999980 province=bj money=99998]
*/
/**
*
*/
package algorithm.max;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Random;
/**
* max n algorithm beanch3
*
* @author yangwm Oct 24, 2010 4:16:12 PM
*/
public class MaxNAlgorithmBeanch3 {
/**
* @param args
*/
public static void main(String[] args) {
/*
*
*/
int inputTotal = 1000000;
int n = 100;
/*
* test data
*/
List<People> inputList = new ArrayList<People>(inputTotal);
for (int i = 0; i < inputTotal; i++) {
People people = null;
int money = i / 10;
int factor = new Random().nextInt(5) + 1;
if (factor == 1) {
people = new People(i, "jx", money);
} else if (factor == 2) {
people = new People(i, "bj", money);
} else if (factor == 3) {
people = new People(i, "sh", money);
} else {
people = new People(i, null, money);
}
inputList.add(people);
}
Collections.shuffle(inputList);
System.out.println("test begin inputTotal: " + inputTotal + ", n: " + n);
System.out.println("test data inputList.size(): " + inputList.size());
//-----------------------------------------------------------------
People[] inputArray = new People[inputList.size()];
for (int i = 0; i < inputArray.length; i++) {
inputArray[i] = inputList.get(i);
}
System.out.println("-------JDK Arrays.sort max n with Comparable-------");
/*
* max and test print
*/
long begin = System.currentTimeMillis();
Arrays.sort(inputArray, new PeopleComparator());
People[] resultArray = (People[]) Array.newInstance(People.class, n);
for (int i = inputArray.length - 1, resultIdx = 0; i >= inputArray.length - n; i--) {
resultArray[resultIdx++] = inputArray[i];
}
long end = System.currentTimeMillis();
System.out.println("cosume: " + (end - begin) + " ms");
System.out.println("resultArray.length: " + resultArray.length);
System.out.println("resultArray of first ten : " + Arrays.toString(Arrays.copyOf(resultArray, 10)));
//-----------------------------------------------------------------
inputArray = new People[inputList.size()];
for (int i = 0; i < inputArray.length; i++) {
inputArray[i] = inputList.get(i);
}
System.out.println("-------MaxNAlgorithm Array max n with Comparable-------");
/*
* max and test print
*/
begin = System.currentTimeMillis();
resultArray = MaxNAlgorithm.<People>max(inputArray, n, new PeopleComparator());
end = System.currentTimeMillis();
System.out.println("cosume: " + (end - begin) + " ms");
System.out.println("resultArray.length: " + resultArray.length);
System.out.println("resultArray of first ten : " + Arrays.toString(Arrays.copyOf(resultArray, 10)));
}
/**
* test object for max n algorithm beanch
*
* @author yangwm Oct 24, 2010 4:16:58 PM
*/
static class People {
public int id;
public String province;
public int money;
public People(int id, String province, int money) {
this.id = id;
this.province = province;
this.money = money;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("id=");
sb.append(id);
sb.append(" province=");
sb.append(province);
sb.append(" money=");
sb.append(money);
return sb.toString();
}
}
static class PeopleComparator implements Comparator<People> {
@Override
public int compare(People o1, People o2) {
if (o1 != null && o2.province == null) {
return 1;
} else if (o1.province == null && o2.province != null) {
return -1;
} else {
if (o1.money > o2.money) {
return 1;
} else if (o1.money < o2.money) {
return -1;
} else {
if (o1.province == null && o2.province == null) {
return 0;
} else if (o1.province.equalsIgnoreCase("jx")) {
return 1;
} else if (o2.province.equalsIgnoreCase("jx")) {
return -1;
} else {
return o1.province.compareToIgnoreCase(o2.province);
}
}
}
}
}
}
/*
test begin inputTotal: 1000000, n: 100
test data inputList.size(): 1000000
-------JDK Arrays.sort max n with Comparable-------
cosume: 1078 ms
resultArray.length: 100
resultArray of first ten : [id=999990 province=jx money=99999, id=999993 province=jx money=99999, id=999991 province=sh money=99999, id=999999 province=sh money=99999, id=999982 province=jx money=99998, id=999984 province=jx money=99998, id=999981 province=sh money=99998, id=999989 province=sh money=99998, id=999988 province=bj money=99998, id=999983 province=bj money=99998]
-------MaxNAlgorithm Array max n with Comparable-------
cosume: 547 ms
resultArray.length: 100
resultArray of first ten : [id=999990 province=jx money=99999, id=999993 province=jx money=99999, id=999991 province=sh money=99999, id=999999 province=sh money=99999, id=999984 province=jx money=99998, id=999982 province=jx money=99998, id=999981 province=sh money=99998, id=999989 province=sh money=99998, id=999980 province=bj money=99998, id=999988 province=bj money=99998]
*/
相关推荐
别再到处找资源了!WinCC 7.5 SP2官方下载与Windows 10保姆级安装避坑指南
本文提供了WinCC 7.5 SP2在Windows 10上的官方下载与安装全流程指南,重点解决了资源获取难、系统兼容性问题及分卷压缩处理等常见痛点。通过详细的步骤和实用技巧,帮助用户顺利完成安装并优化配置,适用于工控领域的学习者和从业者。
HDU 1009(贪心)
#include<cstdio> #include<cstring> #include<string> #include<cmath> #include<vector> #include<algorithm> #include<iostream> #include<time.h> using namespace std;struct node { int Javabean; int
手把手教你用EGM96模型校正DEM数据(含在线工具推荐)
本文详细介绍了如何使用EGM96地球重力场模型校正DEM数据中的高程偏差。通过对比WGS84椭球面与大地水准面的差异,解释了校正原理,并提供了从在线工具快速验证到Python脚本批量处理的全流程实战指南,帮助GIS开发者和数据分析师确保高程数据基准一致,提升空间分析精度。
资源调度之最大最小分配算法java实现
Mini-max算法是一种递归或回溯算法,用于决策和博弈论。它为资源分配提供了一个最佳的分配方式,最近在项目中将这种算法应用到了资源分配中 package com.yzf.task.decision.utils; import java.util.*; /** * @Author PSY * @Date 2022/3/17 20:34 * 最大最小公平算法分配,不带权 * @Desc */ public class MaxMinAllocate { public stat...
Plot3d 网格数据格式
PLOT3D是一个计算机图形程序,旨在可视化计算流体力学的网格和解决方案。PLOT3D文件可以是ASCII, 也可是Fortran unformatted 或 C binary形式。PLOT3D文件分为网格文件(XYZ 文件), 空气动力学结果文件 (Q 文件)和通用结果文件(函数文件 + 函数名称文件)。网格文件中可加入所谓的IBlank参数。
MTK pwm beeper配置记录
如果正常,系统启动后会在/dev/input下生成 一个event节点,使用方法参考标准linux使用。dts相关修改如下,全都在里面,注意一下位置。对应的配置文件中增加如下定义。
N!高效算法
阶乘递归代码:#include<stdio.h> int f(int n); void main() { printf("%d\n",f(5)); } int f(int n) { if(n==0)return 1; return n*f(n-1); } 大数阶乘问题: 10000以内的阶乘及位数:代码一:#include <stdio.h>
python cartopy绘制扇形区域图/cartopy绘制北极部分区域
实际上,这个问题原因还是由于投影转换的问题,在set_extend时,绘制的上下边界仍然是方形、未被正确投影的边界,与我们的set_boundary存在冲突,最根本的原因还是在于cartopy对于投影计算的一些缺陷。另外我们需要指出的是:**该方法不适用于极地投影,即NorthPolarStereo,由于NorthPolarStereo本身投影特性只需一个参数,本身并不适合。我们绘制极地投影时,同样也是使用set_boundary绘制圆形边界,那么当我们想要绘制扇形时,可以通过。请根据喜好自行选择,
经典Top N算法
01TOP N的处理方式top n 如果是直接针对的可排序的数据集合,并且不需要二次计算的话,则全局的top n就是可以分散到局部的top n再聚合。如果数据集不是可排序...
树莓派Bookworm系统避坑指南:3种方法解决pip install报错externally-managed-environment
本文详细解析树莓派Bookworm系统中pip install报错externally-managed-environment的成因,并提供三种实用解决方案:使用虚拟环境(venv)隔离项目依赖、通过pipx管理全局Python工具,以及理解风险后的系统安装方法。帮助开发者安全高效地管理Python环境,避免系统冲突。
保姆级教程:用Fiddler+夜神模拟器搞定抖音App抓包(附SSL Pinning绕过实战)
本文提供了一套完整的移动端抓包解决方案,详细介绍了如何使用Fiddler和夜神模拟器进行抖音App抓包,并重点讲解了SSL Pinning绕过技术。通过Xposed框架和JustTrustMe模块的组合,实现高效抓包,适用于产品分析、竞品研究和安全测试等场景。
java 算法 n_算法(Java)
一、排序1.简单选择排序(O(n*n))【思路】找出数组中最小的元素,将其与第一个元素交换位置,然后再在剩下的元素中找出最小的元素,并与数组的第二个元素交换位置。【特点】原地排序,不稳定。运行时间和输入无关。一个已经有序的数组和一个元素随机排列的数组所用的时间一样。移动数据最少。交换次数和数组大小是线性关系,每次交换都有一个元素位于正确的位置。import java.util.Arrays;pub...
Windows 11 上 WSL2 安装 Ubuntu 22.04 保姆级教程(含桌面环境配置)
本文提供在Windows 11上通过WSL2安装Ubuntu 22.04并配置桌面环境的详细教程。涵盖WSL2核心安装、性能调优、Xfce桌面环境部署以及开发环境集成,帮助开发者构建高效、无缝的Windows-Linux混合开发工作站。
资源分配问题
描述:某厂根据计划安排,拟将n 台相同的设备分配给m 个车间,各车间获得这种设备后,可以为国家提供盈利Cij(j 台设备提供给i 号车间将得到的利润,1≤i≤m,1≤j≤n) 。问如何分配,才使国家得到最大的盈利? 算法基本思路: 1.求最大利润 输入:c[i][j] = 将 j+1 台设备分配给 i+1 号车间得到的利润。 定义:将 j 台设备分配给前 i 个车间的最大利...
ssm框架搭建
这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载
怀旧版电脑GBC模拟器
再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。
从相亲到最优匹配:图解匈牙利算法与KM算法的生活化理解
本文通过相亲配对、任务指派等生活化场景,生动图解了匈牙利算法与KM算法的核心原理。匈牙利算法通过寻找增广路解决二分图最大匹配问题,而KM算法则通过可行顶标与相等子图机制,在考虑权重的情况下寻找最优匹配。文章深入浅出地对比了两种算法的适用场景与工程实践,帮助读者掌握这一关键的图论与最优匹配算法。
霍尼韦尔SoftMaster使用说明
霍尼韦尔SoftMaster使用说明
蓝桥杯——最大最小公倍数
资源限制 时间限制:1.0s 内存限制:256.0MB 问题描述 已知一个正整数N,问从1~N中任选出三个数,他们的最小公倍数最大可以为多少。 输入格式 输入一个正整数N。 输出格式 输出一个整数,表示你找到的最小公倍数。 样例输入 9 样例输出 504 数据规模与约定 1 <= N <= 106。 import java.util.Scanner; public class ...
Statistics and Machine Learning Toolbox
Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具
贪心算法—活动选择问题
Sij=ak∈S:fi≤sk
poj3281-二分图-最大流
题意就是给N头牛,F个食物,D个饮料,每头牛喜欢的食物和饮料各不相同。一头牛需要吃到饭和饮料才能满足,问你最多满足多少头牛。 其实二分图难的并不是算法,而是其构图过程。 如果是简单的分配食物直接用二分图匹配就行,但现在要同时给一头牛分配饮料和食物,我们可以通过题目中给的信息,先画出来牛与食物和饮料的关系图。如图: 看到这个图大家可能有一点感觉了,这在加一个s, t不就是一个求权值为1的最大流问题...
HDU 2255 奔小康赚大钱 求最大匹配值(入门)
点击打开链接 奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2228 Accepted Submission(s): 974 Problem Description 传说在遥远的地方有一个非常富
SDUT 2278----商人的诀窍
由于某仓库的所有苹果不必要买完,所以先挑最便宜的买 题目描述 E_star和von是中国赫赫有名的两位商人,俗话说的好无商不奸,最近E_star需要进一批苹果。可是他需要的苹果只有von才有,von的苹果都存在他的传说中很牛叉的仓库里,每个仓库都存了不同种类的苹果,而且每个仓库里的苹果的价钱不同。如果E_star想要买仓库i里的所有重量为f[i]的苹果他必须付m[i]的金钱。E_sta
HDU - 2255 奔小康赚大钱 (二分图的最佳完美匹配)
奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 13366 Accepted Submission(s): 5827 Problem Description 传说在遥远的地方...
图的最小割:Graph-cut:Min-Cut Problem
基于图的最小割算法 论文:Interactive Graph Cuts for Optimal Boundary & Region Segmentation of Objects in N-D Images 是YYB这个牛人在2000年提出的一种经典的基于的图的最小割算法,现在被广泛使用,现在在这里发表个人拙见,欢迎大家提意见。 算法的核心其实就是这个图: 构造一个图,可以把所有n
Newcoder 141 A.PACM Team(01背包)
Description 有四种类型花费一种收益的nnn件物品,问在四种类型花费不超过各自上限的最大收益 Input 第一行一整数nnn表示物品数量,之后nnn行每行五个整数pi,ai,ci,mi,gipi,ai,ci,mi,gip_i,a_i,c_i,m_i,g_i分别表示该物品的四种花费和收益,最后输入四个整数P,A,C,MP,A,C,MP,A,C,M表示四种花费的上限 (1≤n≤36,...
HDU 3572 Task Schedule(最大流-Dinic)
Description 有M个机器,有N个任务。每个任务必须在Si或者以后开始做,在Ei或者之前完成,完成任务必须处理Pi个时间单位。其中,每个任务可以在任意(空闲)机器上工作,每个机器的同一时刻只能工作一个任务,每个任务在同一时刻只能被一个机器工作,而且任务做到一半可以打断,拿去其他机器做。问能否在规定时间内把任务做完 Input 第一行为一个整数T表示用例组数,每组用例第一行两个整数N和
477



被折叠的 条评论
为什么被折叠?



