装船问题
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
王小二毕业后从事船运规划工作,吉祥号货轮的最大载重量为M吨,有10种货物可以装船。第i种货物有wi吨,总价值是pi。王小二的任务是从10种货物中挑选若干吨上船,在满足货物总重量小于等于M的前提下,运走的货物的价重比最大。
Input
输入数据的第一行有一个正整数M(0 < M < 10000),表示所有货物最大载重量。在接下来的10行中,每行有若干个数(中间用空格分开),第i行表示的是第i种货物的货物的总价值pi ,总重量wi。(pi是wi的整数倍,0 < pi , wi < 1000)
Output
输出一个整数,表示可以得到的最大价值。
Sample Input
100
10 10
20 10
30 10
40 10
50 10
60 10
70 10
80 10
90 10
100 10
Sample Output
550
Hint
价重比:计算其价值与重量之比
#include<bits/stdc++.h>
using namespace std;
struct node{
int p, w, num;
}a[12];
bool cmp(node x, node y){
return x.num>y.num;
}
int main(){
int m, i, k=0, j=0;
cin>>m;
for(i = 0; i < 10; i++){
cin>>a[i].p>>a[i].w;
a[i].num = a[i].p / a[i].w;
}
sort(a, a+10, cmp);
while(m>0){
if(m>=a[j].w){
k += a[j].p;
m -= a[j].w;
j++;
}
else {
k += m * a[j].num;
break;
}
}
cout<<k<<endl;
}
活动选择
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
学校的大学生艺术中心周日将面向全校各个学院的学生社团开放,但活动中心同时只能供一个社团活动使用,并且每一个社团活动开始后都不能中断。现在各个社团都提交了他们使用该中心的活动计划(即活动的开始时刻和截止时刻)。请设计一个算法来找到一个最佳的分配序列,以能够在大学生艺术中心安排不冲突的尽可能多的社团活动。
比如有5个活动,开始与截止时刻分别为:
最佳安排序列为:1,4,5。
Input
第一行输入活动数目n(0<n<100);
以后输入n行,分别输入序号为1到n的活动使用中心的开始时刻a与截止时刻b(a,b为整数且0<=a,b<24,a,b输入以空格分隔)。
Output
输出最佳安排序列所包含的各个活动(按照活动被安排的次序,两个活动之间用逗号分隔)。
Sample Input
6 8 10 9 16 11 16 14 15 10 14 7 11
Sample Output
1,5,4
Hint
#include<bits/stdc++.h>
using namespace std;
struct node{
int s, e, num;
}a[105];
bool cmp(node x, node y){
return x.e<y.e;
}
int main(){
int n, i, k, b[105], j;
cin>>n;
for(i = 0; i < n; i++){
cin>>a[i].s>>a[i].e;
a[i].num = i+1;
}
sort(a, a+n, cmp);
j = k = 0;
b[j++] = a[0].num;
for(i = 1; i < n; i++){
if(a[i].s>=a[k].e)
{
k = i;
b[j++] = a[i].num;
}
}
cout<<b[0];
for(i = 1; i < j; i++){
cout<<","<<b[i];
}
}
最优合并问题
参考:http://www.cnblogs.com/flyoung2008/articles/2136485.html
https://www.cnblogs.com/Deribs4/p/5657746.html
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
给定k 个排好序的序列s1 , s2,……, sk , 用2 路合并算法将这k 个序列合并成一个序列。假设所采用的2 路合并算法合并2 个长度分别为m和n的序列需要m + n -1次比较。试设计一个算法确定合并这个序列的最优合并顺序,使所需的总比较次数最少。
为了进行比较,还需要确定合并这个序列的最差合并顺序,使所需的总比较次数最多。
对于给定的k个待合并序列,计算最多比较次数和最少比较次数合并方案。
Input
输入数据的第一行有1 个正整数k(k≤1000),表示有k个待合并序列。接下来的1 行中,有k个正整数,表示k个待合并序列的长度。
Output
输出两个整数,中间用空格隔开,表示计算出的最多比较次数和最少比较次数。
Sample Input
4 5 12 11 2
Sample Output
78 52
Hint
#include<bits/stdc++.h>
using namespace std;
int main(){
int n, i, x=0, j, d=0, a, b;
priority_queue<int, vector<int>, greater<int> > p;
priority_queue<int>q;
cin>>n;
for(i = 0; i < n; i++){
cin>>j;
q.push(j);
p.push(j);
}
while(q.size()>1){
a = q.top();
q.pop();
b = q.top();
q.pop();
d += a + b - 1;
q.push(a+b);
}
while(p.size()>1){
a = p.top();
p.pop();
b = p.top();
p.pop();
x += a + b - 1;
p.push(a+b);
}
cout<<d<<" "<<x<<endl;
}
区间覆盖问题
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
设x1 , x2 ,…… , xn 是实直线上的n 个点。用固定长度的闭区间覆盖这n 个点,至少需要多少个这样的固定长度闭区间?
对于给定的实直线上的n个点和闭区间的长度k,设计解此问题的有效算法,计算覆盖点集的最少区间数,并证明算法的正确性。
Input
输入数据的第一行有2 个正整数n和k(n≤10000,k≤100),表示有n个点,且固定长度闭区间的长度为k。接下来的1 行中,有n个整数,表示n个点在实直线上的坐标(可能相同)。
Output
输出一个整数,表示计算出的最少区间数输出。
Sample Input
7 3 1 2 3 4 5 -2 6
Sample Output
3
Hint
#include<bits/stdc++.h>
using namespace std;
int main(){
int n, i, k, j, s = 1, a[10005];
cin>>n>>k;
for(i = 0; i < n; i++){
cin>>a[i];
}
sort(a, a+n);
j = a[0] + k;
for(i = 1; i < n; i++){
if(a[i]>j){
j = a[i]+k;
s++;
}
}
cout<<s<<endl;
}