我正在努力解决一个编程问题,为明天的比赛练习,我想也许这将是一个讨论如何接近它的好地方。问题是此网站上的第一个问题:http://www.cs.rit.edu/~icpc/questions/2010/Oswego_2010.pdf
本网站上的常见问题解答提到了算法和数据结构的概念,以及设计模式,所以我想问一下如何解决这个问题并非偏离主题。这是我到目前为止(不多)。我不明白如何解决这个问题。
public class Ape
{
public void computeOutput(int weight, int[] capacities, int[] snackLosses)
{
//not sure what to do
}
public static void main(String [] args) throws FileNotFoundException
{
Ape ape = new Ape();
File file = new File(args[0]);
Scanner in = new Scanner(file);
int totalWeight = in.nextInt();
int n = in.nextInt();
int[] capacities = new int[n];
int[] snackLosses = new int[n];
for (int i = 0; i < n; i++)
{
capacities[i] = in.nextInt();
snackLosses[i] = in.nextInt();
}
ape.computeOutput(totalWeight, capacities, snackLosses);
}
}
答案 0 :(得分:4)
乍一看,这似乎是一个动态编程问题。
基本上,我们有一个函数f(N,K)=给予K的bannas和前N只猴子带来的bannas数量。
显然f(0,K)= 0且f(N,0)= 0
然后你所要做的就是找出f(n,k)的值。你应该通过最多两个案例来做到这一点:
使用此逻辑填写表格,然后确定f(N,K),你就得到了答案。
答案 1 :(得分:0)
这个问题可以简化为0/1背包,这本身就是众所周知的DP算法。但是这里没有价值,你有能力 - 小吃。
答案 2 :(得分:0)
#include <iostream>
using namespace std;
main(){
int totalwight,numberapes;
//cout<<"Enter The total weight of bananas available to lug home : ";
cin>>totalwight;
//cout<<"Enter The number of apes : ";
cin>>numberapes;
int *capacity = new int [numberapes];
int *tcapacity = new int [numberapes];
int *snackloss = new int [numberapes];
int *pos = new int [numberapes];
int *tpos = new int [numberapes];
for (int i=0 ; i<numberapes ; i++){
pos[i]=i+1;
tpos[i]=0;
}
for (int i=0 ; i<numberapes ; i++){
// cout<<"Enter How many monkey # "<<i+1<<" carrying capacity : ";
cin>>capacity[i];
tcapacity[i]=capacity[i];
//cout<<"Enter How many snack loss of monkey # "<<i+1<<" : ";
cin>>snackloss[i];
}
int *arr = new int [numberapes];
for (int i=0 ; i<numberapes ; i++)
arr[i] = capacity[i] - snackloss[i];
int temp;
for (int i=0 ; i<numberapes ; i++)
for (int j=0 ; j<i ; j++)
if (arr[i] > arr[j]){
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
temp = tcapacity[i];
tcapacity[i] = tcapacity[j];
tcapacity[j] = temp;
temp = pos[i];
pos[i] = pos[j];
pos[j] = temp;
}
int *tarr = new int [numberapes];
int j=0;
for (int i=0 ; i<numberapes ; i++)
tarr[i]=0;
for (int i=0 ; i<numberapes ; i++){
if (arr[i] <= totalwight && tcapacity[i] <= totalwight){
totalwight -= tcapacity[i];
tpos[j] = pos[i];
j++;
}
}
for (int i=0 ; i<numberapes ; i++)
for (int j=0 ; j<numberapes ; j++)
if (tpos[j] > tpos[i] && tpos[i]!=0 && tpos[j]!=0){
temp = tpos[i];
tpos[i] = tpos[j];
tpos[j] = temp;
}
int t1=1,t2=0;
while (t1<=numberapes){
if (t1 == tpos[t2]){
cout<<"1 ";
t2++;
}
else
cout<<"0 ";
t1++;
}
}
答案 3 :(得分:0)
请查看我的答案:
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
namespace monkey
{
class Program
{
static int getTotalCapacity(int[] ki, int[] indexes)
{
int result = 0;
foreach (int i in indexes)
{
result += ki[i];
}
return result;
}
static int[] computeOutput(int k, int n, int[] ki, int[] wi)
{
// #1: Sort the input by ki - wi
Dictionary<int, int> vi = new Dictionary<int, int>();
for (int i = 0; i < n; i++)
{
vi.Add(i, ki[i] - wi[i]);
}
Dictionary<int, int> sorted_vi = (from entry in vi orderby entry.Value descending select entry).ToDictionary(x => x.Key, x => x.Value);
// #2: Take elements in sequence in order to fulfill the minimum total k
Dictionary<int, int> result = new Dictionary<int, int>();
foreach (KeyValuePair<int, int> kv in sorted_vi)
{
if (ki[kv.Key] + getTotalCapacity(ki, result.Select(x=>x.Key).ToArray()) <= k)
{
result.Add(kv.Key,kv.Value);
}
}
// #3 Transform to output format
int[] output = new int[n];
foreach(KeyValuePair<int,int> kv in result){
output[kv.Key] = 1;
}
return output;
}
static void Main(string[] args)
{
if (args.Length != 1)
{
Console.WriteLine("Usage: <prog>.exe <filepath>");
return;
}
try
{
int k;
int n;
int[] ki;
int[] wi;
// Reading file input
using (StreamReader fs = new StreamReader(args[0]))
{
k = Convert.ToInt32(fs.ReadLine());
n = Convert.ToInt32(fs.ReadLine());
ki = new int[n];
wi = new int[n];
for (int i = 0; i < n; i++)
{
string[] line = fs.ReadLine().Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
ki[i] = Convert.ToInt32(line[0]);
wi[i] = Convert.ToInt32(line[1]);
}
}
int[] output = computeOutput(k, n, ki, wi);
Console.WriteLine(string.Join(" ", output));
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return;
}
}
}
}