赏月斋源码共享计划 第二期

需求描述:

有m个任务,第i个任务需要xi时间去完成,难度等级为yi

有n台机器,每台机器最长工作时间为zi,机器等级wi

一个任务只能交给一台机器,且如果机器的最长工作时间小于任务所需时间,则不能完成

若完成任务,获得收益为200*xi+3*yi

一台机器一天只能完成一个任务,若机器等级小于任务难度等级,则不能完成

想尽可能多地完成任务,即完成任务数最大,找到收益最大的方案



代码实现:

# include <bits/stdc++.h>
using namespace std;
# define LL long long

const int maxn = 1e5 + 10;
struct node {
	int x, y;
}e[maxn], f[maxn];
int cnt[105];
int cmp(node a, node b) {        //cmp()用于排序,返回a和b按字典排序的大小
	if (a.x == b.x) {
		return a.y > b.y;
	}
	return a.x > b.x;
}

int main(void) {
	int m, n;
	scanf("%d%d", &n, &m);
	for (int i = 0; i < n; i++) scanf("%d%d", &e[i].x, &e[i].y); //e表示机器
	for (int i = 0; i < m; i++) scanf("%d%d", &f[i].x, &f[i].y);//f表示任务
	sort(e, e + n, cmp);
	sort(f, f + m, cmp);	//排序

	int num = 0;
	LL ans = 0;
	memset(cnt, 0, sizeof(cnt));	//赋初值
	int i, j, k;
	for (i = 0, j = 0; i < m; i++) {
		while (j < n && e[j].x >= f[i].x) {
			cnt[e[j].y]++;
			j++;
		}
		for (k = f[i].y; k <= 100; k++) {
			if (cnt[k]) {
				num++;
				cnt[k]--;
				ans = ans + 200 * f[i].x + 3 * f[i].y;
				break;
			}
		}
	}
	/************************************************************************/
	/* while循环先把满足时间条件的机器筛选出来(机器、任务都按从大到小排序)
	选出来满足时间条件的机器
	分别记下他们的难度
	cnt[i]=t 表示选出来t个难度为i的机器

	接下来,while后边的for循环用来选择难度条件
	从任务i的难度k=f[i].y开始依次递增k++
	第一个满足if条件的k就是最小的k(最节约机器难度)
	即:尽量先用满足条件的机器中支持难度小的机器*/
	/************************************************************************/
	printf("%d %ld\n", num, ans);

	return 0;
}

  



解决方案二:

分析:

收益只与完成的任务x、y有关,且x、y越大,获得的收益越大,所以要优先完成x更大的任务,若x相等,则要优先完成y大的任务。

为保证上述完成要求,我们可以将任务的x按从大到小排序,若x相同,根据y从大到小排序,排序完成后,再给任务分配机器。

当有多台机器符合x条件,选择y满足条件的最小的wi。

#include<cstdio>
#include<cstring>
#include<algorithm>
#define Max 100010
#define LL long long

using namespace std;

struct A
{
	int x;
	int y;
}t[Max], m[Max];

bool cmp(A a, A b)
{
	if(a.x != b.x)return a.x > b.x;
	return a.y > b.y;
}

int main()
{
	int N, M;
	scanf("%d %d", &N, &M);
	for(int i = 0; i < N; i++)
		scanf("%d %d", &m[i].x, &m[i].y);
	for(int j = 0; j < M; j++)
		scanf("%d %d", &t[j].x, &t[j].y);
	sort(t, t + M, cmp);
	sort(m, m + N, cmp);

	/*for(int i = 0; i < N; i++)
		printf("%d %d\n", t[i].x, t[i].y);*/

	LL sum = 0;
	int count = 0;
	int mark[Max]={0};
	int j = 0;
	for(int i = 0; i < M; i++)
	{
		//每台机器只能用一次,所以分配下一个任务时,j从上一个任务之后开始考虑,j不用归0
		//由于任务是按从到大小排列的,因此满足上一任务要求的机器一定满足下一任务要求
		while(j < N && m[j].x >= t[i].x)
		{
			mark[m[j].y]++;//机器对应等级加1,表示满足该任务时长下,符合等级要求的可用的机器数
			j++;
		}
		for(int k = t[i].y; k <= 100; k++)//从符合等级要求的等级最低的机器开始分配
		{
			if(mark[k])
			{
				mark[k]--;
				count++;
				sum += (LL)(200 * t[i].x + 3 * t[i].y);
				//printf("sum = %lld\n", sum);
				break;
			}
		}
	}
	printf("%d %lld\n", count, sum);

	return 0;
}  


解决方案三:

import java.util.Arrays;
import java.util.Scanner;
class A implements Comparable<A>{
	int x;
	int y;
	public int compareTo(A o) {
        // TODO Auto-generated method stub
        if(this.x>o.x)
            return -1;//返回-1表示当前对象在前,即由高到底排序  ;return 1则为由低到高排
        else if(this.x<o.x)
            return 1;
        else{ //如果x的值一样,则比较y的大小
            if(this.y>o.y)
                return -1;//由高到低排序
            else if(this.y<o.y)
                return 1;
            else
                return 0;  //如果x,y都相等,返回0
        }
    }
}
public class Main {
	public static int Max = 100000;
    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);   

        int N = in.nextInt();//机器的数量
        int M = in.nextInt();//任务的数量

        int i,j,k;
         //引用类型数组初始化!!!!动态初始化由系统为数组元素分配默认的初始值:null,即每个数组元素的值都是null。
        A[] machine = new A[Max];//机器的工作时间和机器等级
        //m数组的Max个数组元素都是引用,而且这个引用并未指向任何有效的内存,因此每个数组元素的值都是null
        //因为每个数组元素都是null,这相当于定义了Max个连续的A变量,但这个变量还未指向任何有效的内存区,所以这Max个连续的A变量(m数组的数组元素)还不能使用。

        for(i=0;i<N;i++){
        	machine[i] = new A();//!!!!如果没有这一句,对象都没有初始化,因为只创建了数组对象,而数组的每个元素都为null,所以运行的时候会抛出空指针异常。
        	machine[i].x = in.nextInt();
        	machine[i].y = in.nextInt();
        }

        A[] task = new A[Max];//任务需要完成的时间和任务难度
        for(i=0;i<M;i++){
        	task[i] = new A();
        	task[i].x = in.nextInt();
        	task[i].y = in.nextInt();
        }
        //Arrays.sort(machine);//空指针错误,用Arrays.sort 的排序,必须保证数组里面不存在空值,

        A m[] = Arrays.copyOfRange(machine,0,N);
        Arrays.sort(m);//自动调用compareTo
        //Collections.sort(m);//出现错误!!类型 Collections 中的方法 sort(List<T>)对于参数(A[])不适用
//        for(i=0;i<N;i++){
//        	System.out.println("###"+m[i].x+"###"+m[i].y);
//        }

        A t[] = Arrays.copyOfRange(task,0,M);
        Arrays.sort(t);

        int mark[] = new int[100];//定义数组,并初始化,默认值就是0

        long sum = 0;int count = 0;
        for(i=0,j=0;i<M;i++){
        	while(j<N&&t[i].x<=m[j].x){
        		mark[m[j].y]++;
        		j++;
        	}
        	for(k=t[i].y;k<100;k++){
        		if(mark[k]!=0){
        			mark[k]--;
        			count++;
        			sum = sum + 200*t[i].x+3*t[i].y;
        			break;
        		}
        	}
        }
        System.out.println(count+" "+sum);
    }
}  


解决方案四:

package com.study;

import java.util.Scanner;

/**
* @author ZhaoFang
* @date 2018年4月5日 下午9:18:17
* @describe 安排机器:小Q的公司最近接到m个任务,第i个任务需要Xi的时间去完成,难度等级为yi。
*                 小Q拥有n台机器,每台机器最长工作时间zi,机器等级wi。
*                 对于一个任务,它只能交由一台机器来完成,如果安排给它的机器的最长工作时间小于任务需要的时间,则不能完成,
*                 如果完成这个任务将获得200*xi + 3*yi收益。
*                 对于一台机器,它一天只能完成一个任务,如果它的机器等级小于安排给它的任务难度等级,则不能完成。
*                 小Q想在今天尽可能的去完成任务,即完成的任务数量最大。如果有多种安排方案,小Q还想找到收益最大的那个方案。小Q需要你来帮助他计算一下。
*           输入描述:输入包括 N + M + 1行
*                 输入的第一行为两个正整数n和m(1 <= n, m <= 100000),表示机器的数量和任务的数量。
*                 接下来n行,每行两个整数zi和wi(0 < zi < 1000, 0 <= wi <= 100),表示每台机器的最大工作时间和机器等级。
*                 接下来的m行,每行两个整数xi和yi(0 < xi < 1000, 0 <= yi <= 100),表示每个任务需要的完成时间和任务的难度等级。
*           输出描述:输出两个整数,分别表示最大能完成的任务数量和获取的利益。
*           输入: 1 2
*               100 3
*               100 2
*               100 1
*           输出:1 20006
*/
public class Tencent3 {

	public static void main(String[] args) {

	        Scanner in = new Scanner(System.in);

	        int n = in.nextInt();//机器的数量
	        int m = in.nextInt();//任务的数量

	        if(n < 1 || n > 100000 || m < 1 || m > 100000)
	        	return;

	        int[] z = new int[n];//机器的最大工作时间数组
	        int[] w = new int[n];//机器等级数组

	        int[] x = new int[m];//任务需要的完成时间数组
	        int[] y = new int[m];//任务的难度等级数组

	        int temp1 = 0;
	        int temp2 = 0;

	        //输入每台机器的最大工作时间和机器等级
	        for(int i = 0; i < n; i++) {
	        	temp1 = in.nextInt();
	        	temp2 = in.nextInt();

	        	if(temp1 > 0 && temp1 < 1000 && temp2 >= 0 && temp2 <= 100) {
	        		z[i] = temp1;
	        		w[i] = temp2;
	        	}
	        }

	        //输入每个任务需要的完成时间和任务的难度等级
	        for(int j = 0; j < m; j++) {
	        	temp1 = in.nextInt();
	        	temp2 = in.nextInt();

	        	if(temp1 > 0 && temp1 < 1000 && temp2 >= 0 && temp2 <= 100) {
	        		x[j] = temp1;
	        		y[j] = temp2;
	        	}
	        }

	        int cost = 0;//获取的收益
	        int count = 0;//最大能完成的任务数量
	        int p = -1;//设置标志,如果机器与任务都相匹配,选出收益最大,并保证该任务只被一个机器完成,且一台机器每天只能完成一个任务
	        int q = -1;

	        //解题难点:存在一个机器适用于多种任务的情况,此时需选出收益最大的那个任务,并将完成的任务数量+1

	        for(int i = 0; i < n; i++) {
	        	for(int j = 0; j < m; j++) {
	        		if(z[i] >= x[j] && w[i] >= y[j]) {
	        			temp1 = 200*x[j] + 3*y[j];
	        			if(temp1 > cost) {
	        				cost = temp1;
	        			}
	        			if(p != i && q != j) {
	        				p = i;
	        				q = j;
	        				count++;
	        			}
	        		}
	        	}
	        }

	        System.out.println("最大能完成的任务数量为:" + count);
	        System.out.println("获取的利益:" + cost);
	}
}

  

源码解析:

1. 如何在VS中添加<bits/stdc++.h>

https://blog.csdn.net/dragon60066/article/details/56529077

VS 的include文件夹路径:

C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.10.25017\include

在vs的include文件夹里面新建一个bits文件夹,里面新建一个名叫stdc++.h的头文件

stdc++.h源文件内容如下:

// C++ includes used for precompiling -*- C++ -*-

// Copyright (C) 2003-2015 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.

// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.

// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
// <http://www.gnu.org/licenses/>.

/** @file stdc++.h
 *  This is an implementation file for a precompiled header.
 */

// 17.4.1.2 Headers

// C
#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdalign>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif

// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif

  



2. 在国内oj中,poj,hdu 不支持这个函数,这几个oj的编译器问题,其他国外的oj,还有台湾的oj都支持,CF,Topcoder也都支持。



3.

C语言memset()函数:将内存的前n个字节设置为特定的值

头文件:#include <string.h>

memset() 函数用来将指定内存的前n个字节设置为特定的值,其原型为:
    void * memset( void * ptr, int value, size_t num );

参数说明:

  • ptr 为要操作的内存的指针。
  • value 为要设置的值。你既可以向 value 传递 int 类型的值,也可以传递 char 类型的值,int 和 char 可以根据 ASCII 码相互转换。
  • num 为 ptr 的前 num 个字节,size_t 就是unsigned int。

【函数说明】memset() 会将 ptr 所指的内存区域的前 num 个字节的值都设置为 value,然后返回指向 ptr 的指针。

memset() 可以将一段内存空间全部设置为特定的值,所以经常用来初始化字符数组。例如:

  1. char str[20];
  2. memset(str, ‘\0‘, sizeof(str)-1);

【返回值】返回指向 ptr 的指针。

注意:参数 value 虽声明为 int,但必须是 unsigned char,所以范围在0 到255 之间。

范例:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. int main()
  5. {
  6. // 不可以声明为 char *str = "http://c.biancheng.net";
  7. char str[] = "http://c.biancheng.net";
  8. memset(str, ‘-‘, 7);
  9. puts(str);
  10. system("pause");
  11. return EXIT_SUCCESS;
  12. }

执行结果:
-------c.biancheng.net

注意:字符数组是可以被修改的,字符串是只读的,不能被修改,而 memset() 又必须修改 str,所以不能将 char str[] = "http://c.biancheng.net"; 声明为 char *str = "http://c.biancheng.net";,否则运行时会报错。



4. C++的sort函数

最近在刷ACM经常用到排序,以前老是写冒泡,可把冒泡带到OJ里后发现经常超时,所以本想用快排,可是很多学长推荐用sort函数,因为自己写的快排写不好真的没有sort快,所以毅然决然选择sort函数

用法

1、sort函数可以三个参数也可以两个参数,必须的头文件#include < algorithm>和using namespace std; 
2、它使用的排序方法是类似于快排的方法,时间复杂度为n*log2(n)

3、Sort函数有三个参数:(第三个参数可不写)

(1)第一个是要排序的数组的起始地址。

(2)第二个是结束的地址(最后一位要排序的地址)

(3)第三个参数是排序的方法,可以是从大到小也可是从小到大,还可以不写第三个参数,此时默认的排序方法是从小到大排序。

两个参数用法

#include <iostream>
#include <algorithm>
int main()
{
 int a[20]={2,4,1,23,5,76,0,43,24,65},i;
 for(i=0;i<20;i++)
  cout<<a[i]<<endl;
 sort(a,a+20);
 for(i=0;i<20;i++)
 cout<<a[i]<<endl;
 return 0;
}

三个参数的用法

// sort algorithm example
#include <iostream>     // std::cout
#include <algorithm>    // std::sort
#include <vector>       // std::vector

bool myfunction (int i,int j) { return (i<j); }//升序排列
bool myfunction2 (int i,int j) { return (i>j); }//降序排列

struct myclass {
  bool operator() (int i,int j) { return (i<j);}
} myobject;

int main () {
    int myints[8] = {32,71,12,45,26,80,53,33};
  std::vector<int> myvector (myints, myints+8);               // 32 71 12 45 26 80 53 33

  // using default comparison (operator <):
  std::sort (myvector.begin(), myvector.begin()+4);           //(12 32 45 71)26 80 53 33

  // using function as comp
  std::sort (myvector.begin()+4, myvector.end(), myfunction); // 12 32 45 71(26 33 53 80)
    //std::sort (myints,myints+8,myfunction);不用vector的用法

  // using object as comp
  std::sort (myvector.begin(), myvector.end(), myobject);     //(12 26 32 33 45 53 71 80)

  // print out content:
  std::cout << "myvector contains:";
  for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)//输出
    std::cout << ‘ ‘ << *it;
  std::cout << ‘\n‘;

  return 0;
}

  

string 使用反向迭代器来完成逆序排列

#include <iostream>
using namespace std;
int main()
{
     string str("cvicses");
     string s(str.rbegin(),str.rend());
     cout << s <<endl;
     return 0;
}
//输出:sescivc

原文地址:https://www.cnblogs.com/sddai/p/9568785.html

时间: 2024-07-30 23:41:00

赏月斋源码共享计划 第二期的相关文章

AE+SceneControl源码共享

最近,有博友发私信或留言交流,先把之前用SceneControl代做的一个小工程共享,做的很粗糙,希望可以帮到大家,欢迎留言交流哈. 除了基本的打开.保存.数据加载.查询等,主要实现以下几个功能: 1.根据高程点(excel文件)生成TIN. 2.TIN与DOM叠加构建三维场景. 3.根据TIN提取等高线. 4.模型导入 下载地址见:http://download.csdn.net/detail/liushuo_whu/7793889 AE+SceneControl源码共享,布布扣,bubuko

最近建了一个.net源码共享群,群共享有大量网友分享的.net(C#)商业源码

.net源码共享群 324087998. 本群创建于2013/6/21: 群里都是.net(C#)程序开发人员,群共享有大量网友分享的.net(C#)商业源码.比如:DTCMS旗舰版,hishop微分销,shopnum微分销,多用户微信公众平台开发,力软信息化快速开发框架.源码研究,源码共享,.net反编译破解,.net破解反编译,技术研究交流! 之前在CSDN上发了一下,现在群里有六七百人了.马上就满员了,第二个群又要开始了.群友都是很活跃的,现在群等级满级了. 这是之前文章内容: 由于工作需

微信游戏圈小游戏源码共享下载

源码数量不少于300套!持续更新中! 皇家VIP群入会费请咨询QQ   截止于2014-10-18,更新的一些游戏截图   截止于2014-10-13,微信小游戏VIP交流群已经共享的源码截图 列举一下部分游戏的名称: 随意门(屌丝版) 植物大战僵尸 逼死强迫症2 胸口碎大石 粉绿男女 打击者 黄金矿工 反腐小王子 转你妹 纸飞机 狂射iphone6 小兔子快快跑 跳跳狗 扫雷 奇葩连连看 看你有多花 开心消消乐 进化 拯救精子 打老虎 色域迷惑人 厕纸挑战 气球砰砰砰 横冲直闯 疯狂飙车 粑粑

WangSql 3.0源码共享(WangSql 1.0重大升级到3.0)

WangSql 1.0博文阅读: http://www.cnblogs.com/deeround/p/6204610.html 基于1.0做了以下重大改动: 1.多数据实现方式调整 2.使用EmitMapper代替反射实现DbDataReader转实体 3.源码在文章最后提供下载地址 核心功能介绍 多数据支持,采用配置文件形式实现多种数据实现,只需要指定数据库驱动即可实现所支持所有数据库任意切换. SQL自行管理,大大提高SQL可控性以及SQL性能优化. 高效实体转换,集成EmitMapper来

WangSql 1.0源码共享

一,项目背景 现在基本上大大小小的项目都需要和数据库打交道,自然而然数据库操作会有很多地方,而使用传统ADO.NET整个流程有点麻烦,出参都需要手动转换为对象.基于以上,我们需要一个SQL执行工具,能简化上诉步骤,而不失对SQL控制权. 二,核心技术 最底层是ADO.NET,基于ADO.NET开发的工具. 1.支持多数据库 2.支持增删改查事务过程等常用SQL操作 3.支持参数统一化 4.支持入参多样化,且自动转换成SQL参数 5.支持出参多样化,且自动转换成对象 6.缓存方案(未完成) 7.并

(1) Android-5.0源码分析计划

从事 Android 工作也有一段时间了,一直想把整个系统过一遍.在学习过程中一直在看老罗的博客,分析地很到位,可以看出功力相当深厚啊!记得以前听人说,读书要先读厚,再读薄,再读厚--如此反复,才可吃透.面对像 Android 这样的大型软件,感觉无论用什么方法感觉都无法让人直观地去认识,更别提过几遍了,能完整地理解整个系统已经很不容易了.但还是要乐观地去面对这件事,能做一些就做一些了.         想来想去,如果能做到如下几点,用比较符合人脑认知事物的方式,还是可以的: 尽量以图形方式来表

Android 开源项目源码解析(第二期)

Android 开源项目源码解析(第二期) 阅读目录 android-Ultra-Pull-To-Refresh 源码解析 DynamicLoadApk 源码解析 NineOldAnimations 源码解析 SlidingMenu 源码解析 Cling 源码解析 BaseAdapterHelper 源码分析 Side Menu.Android 源码解析 DiscreteSeekBar 源码解析 CalendarListView 源码解析 PagerSlidingTabStrip 源码解析 公共

asp.net,java,jsp,安卓Android,苹果ios,php,vb.net,c#免费毕业课程设计源码共享网盘下载

百度网盘下载地址1:  http://pan.baidu.com/s/1o67fybC 百度网盘下载地址2: http://pan.baidu.com/s/1kTxckmF163网盘下载地址:http://home.163disk.com/shuangyulin file://E:\计算机设计参考!!!!!!!!!!!\资料 (4 folders, 0 files, 0 bytes, 641.25 MB in total.) ├─QQ254540457 (0 folders, 49 files,

VS2015 多项目源码共享链接

Eclipse有这个功能,在一个项目中加入另一个项目文件夹的引用,源码包含过来,这样不必copy一份代码,只需要维护一份源代码.一直想在VS中找到这个功能,目前项目需要,终于google到了. http://stackoverflow.com/questions/3610845/add-as-link-for-folders-in-visual-studio-projects In VS2012 and later, you can drag a folder to another projec