【贪心算法】格子与DP
1
#include <bits/stdc++.h>
using namespace std;
int main() {
int T;
scanf("%d", &T);
for(int t = 1; t <= T; t++) {
int a, b, c, res = 0;
scanf("%d%d%d", &a, &b, &c);
while(true) {
if(a >= 1 && b >= 2 && c >= 1) {
a--;
b -= 2;
c--;
res++;
} else if(a >= 1 && b >= 4) {
a--;
b -= 4;
res++;
} else if(a >= 3 && b >= 1 && c >= 1) {
a -= 3;
b--;
c--;
res++;
} else if(a >= 3 && b >= 3) {
a -= 3;
b -= 3;
res++;
} else if(a >= 5 && c >= 1) {
a -= 5;
c--;
res++;
} else if(a >= 5 && b >= 2) {
a -= 5;
b -= 2;
res++;
} else if(a >= 7 && b >= 1) {
a -= 7;
b--;
res++;
} else if(a >= 9) {
a -= 9;
res++;
} else break;
}
printf("Case #%d: %d\n", t, res);
}
return 0;
}
2三人成百问题
package competition;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
/**
* @author OliverWu
*
*/
public class test04{
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
int a = input.nextInt() ;
if (a <=0 || a>=100 ){
System.out.println("NO");
return ;
}
List<Integer> inputList = new ArrayList<>();
while(a >0 ){
inputList.add(a);
a = input.nextInt() ;
}
//int[] nums = {10,2,48,2,48,3,47,1,60,2,80,48,53,90,91,30,59,90,60,99,50,60};
//10 2 48 2 48 3 47 1 60 2 80 48 53 90 91 30 59 90 60 99 50 60 0
int[] nums = inputList.stream().mapToInt(Integer::valueOf).toArray(); //List<Integer> 转 int[]
List<List<Integer>> result = threeSum(nums,100) ;
if(result.size() ==0 ){
System.out.println("NO");
}
for( List<Integer> l : result ){
for (int e : l){
System.out.print(e+" ");
}
System.out.println();
}
//System.out.println("*****************");
}
//三人成百
//https://www.programcreek.com/2012/12/leetcode-3sum/
public static List<List<Integer>> threeSum(int[] nums,int target) {//jik就是那三人,num是输入的年龄数组
Arrays.sort(nums);
ArrayList<List<Integer>> result = new ArrayList<>();
for (int i = 0; i < nums.length; i++) {
int j = i + 1;
int k = nums.length - 1;
if (i > 0 && nums[i] == nums[i - 1]) {
continue;
}
while (j < k) {
if (k < nums.length - 1 && nums[k] == nums[k + 1]) {
k--;
continue;
}
if (nums[i] + nums[j] + nums[k] > target) {
k--;
} else if (nums[i] + nums[j] + nums[k] < target) {
j++;
} else {
ArrayList<Integer> l = new ArrayList<>();
l.add(nums[i]);
l.add(nums[j]);
l.add(nums[k]);
result.add(l);
j++;
k--;
}
}
}
return result;
}
}
3
package competition;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
public class test04{
public static void main(String[] args) {
//创建活动并添加到集合中
Active act1 = new Active(1, 4);
Active act2 = new Active(3, 5);
Active act3 = new Active(0, 6);
Active act4 = new Active(5, 7);
Active act5 = new Active(3, 8);
Active act6 = new Active(5, 9);
Active act7 = new Active(6, 10);
Active act8 = new Active(8, 11);
Active act9 = new Active(8, 12);
Active act10 = new Active(2, 13);
Active act11 = new Active(12, 14);
List<Active> actives = new ArrayList<Active>();
actives.add(act1);
actives.add(act2);
actives.add(act3);
actives.add(act4);
actives.add(act5);
actives.add(act6);
actives.add(act7);
actives.add(act8);
actives.add(act9);
actives.add(act10);
actives.add(act11);
List<Active> bestActives = getBestActives(actives, 0, 16);
for (int i = 0; i < bestActives.size(); i++) {
System.out.println(bestActives.get(i));
}
}
/**
*
* @param actives
* 活动集合
* @param startTime
* 教室的开始使用时间
* @param endTime
* 教室的结束使用时间
* @return
*/
public static List<Active> getBestActives(List<Active> actives, int startTime, int endTime) {
//最佳活动选择集合
List<Active> bestActives = new ArrayList<Active>();
//将活动按照最早结束时间排序
actives.sort(null);
//nowTime 用来记录上次活动结束时间
int nowTime = startTime;
/**
* 因为我们已经按照最早结束时间排序,那么只要活动在时间范围内
* actives.get(1)就应当是第一个活动的结束时间.
* 则我们记录第一次活动结束的时间,在结合剩下的活动中,
* 知道选出所有活动
*/
for (int i = 0; i < actives.size(); i++) {
Active act = actives.get(i);
if(act.getStartTime()>=nowTime&&act.getEndTime()<=endTime){
bestActives.add(act);
nowTime = act.getEndTime();
}
}
return bestActives;
}
}
/**
* 活动类
* @CreatTime 下午9:45:37
*
*/
class Active implements Comparable<Active>{
private int startTime;//活动开始时间
private int endTime;//活动结束时间
public Active(int startTime, int endTime) {
super();
this.startTime = startTime;
this.endTime = endTime;
}
public int getStartTime() {
return startTime;
}
public void setStartTime(int startTime) {
this.startTime = startTime;
}
public int getEndTime() {
return endTime;
}
public void setEndTime(int endTime) {
this.endTime = endTime;
}
@Override
public String toString() {
return "Active [startTime=" + startTime + ", endTime=" + endTime + "]";
}
//活动排序时按照结束时间升序
@Override
public int compareTo(Active o) {
if(this.endTime>o.getEndTime()){
return 1;
}else if(this.endTime == o.endTime){
return 0;
}else{
return -1;
}
}
}