Algorithm 安排任务的错误答案#1704在线判断
你的老板告诉你,你只有一台电脑来完成某些任务。他们每个人都为公司赚了一笔钱,从现在起必须在t小时内完工。之后,它就不能再执行了,也没有更多的价值了。计算机每小时只完成一项任务 你想给老板留下好印象,这样他可能会给你升职。要做到这一点,您需要使用编程技能来选择执行哪些任务,以使损失的资金量最小化 输入 输入由几个测试用例组成,并以文件结尾结束。每一个都描述了一个任务列表,并以两个整数N(1)开始≤N) 和H(H≤ 1000),分别是任务数和计算机可用的小时数。然后跟随N行,每行有两个整数v(1)≤ v≤ 1000)和t(1≤ T≤ H) 如上所述 输出 对于每个测试用例,输出一行,其中一个整数表示最小损失金额Algorithm 安排任务的错误答案#1704在线判断,algorithm,data-structures,Algorithm,Data Structures,你的老板告诉你,你只有一台电脑来完成某些任务。他们每个人都为公司赚了一笔钱,从现在起必须在t小时内完工。之后,它就不能再执行了,也没有更多的价值了。计算机每小时只完成一项任务 你想给老板留下好印象,这样他可能会给你升职。要做到这一点,您需要使用编程技能来选择执行哪些任务,以使损失的资金量最小化 输入 输入由几个测试用例组成,并以文件结尾结束。每一个都描述了一个任务列表,并以两个整数N(1)开始≤N) 和H(H≤ 1000),分别是任务数和计算机可用的小时数。然后跟随N行,每行有两个整数v(1)≤
Sample Input Sample Output
3 3 0
5 1 3
10 2
20 3
4 2
1 2
2 1
4 1
2 2
我正在解决许多复杂的测试用例,如下面给出的,得到了预期的结果,但在提交时仍然得到100%的错误答案。我的方法是根据货币价值按降序排列数组,然后按逻辑排列。我的算法有什么错误
Input Output
5 3 30
10 1 30
20 2 1
50 2 0
40 3
30 3
4 5 test case 1
10 1
20 1
50 2
40 2
6 5 test case 2
10 1
12 4
15 5
20 5
1 5
30 5
3 1000
1000 1000
2 1
3 3
在下面的代码中-测试用例1(4 5)和测试用例2(6 5)工作正常,因为测试用例1的tlarge较小,并且测试用例2中的大多数任务可以在5小时内完成,因此equal标志没有设置为true
while(hours < H && z < N)
{
if(equal[Arr[z].getT()]!=true && hours<tlarge) {
loss -= Arr[z].getV();
equal[Arr[z].getT()]=true;
hours++;
}
else
{
int time=Arr[z].getT()-1;
while(time>0)
{
if(equal[time]==false)
{
loss -= Arr[z].getV();
hours++;
if(Arr[z].getT()<=hours)
{
int time1 = Arr[z].getT()-1;
while(time1>0)
{
equal[time1]=true;
time1--;
}
}
break;
}
time--;
}
}
z++;
}
while(小时
这是我的密码-
类条目
{
私人INTV;
私人int t;
条目(整数v,整数t){
这个,v=v;
t=t;
}
公共int getV(){
返回v;
}
公共int getT(){
返回t;
}
}
公共班机{
公共最终静态条目[]Arr=新条目[1003];
公共静态void main(字符串[]args)引发java.lang.Exception
{
BufferedReader in=新的BufferedReader(新的InputStreamReader(System.in));
while(true){
字符串T=in.readLine();
如果(T==null){
打破
}
字符串[]spl=T.split(“”);
intn=Integer.parseInt(spl[0]);
inth=Integer.parseInt(spl[1]);
int Num=N;int tlarge=0;
整小时=0;整损失=0;整z=0,y=0;
布尔等于[]=新布尔值[1003];
while(Num>0)
{
字符串oper=in.readLine();
字符串[]op=oper.split(“”);
intv=Integer.parseInt(op[0]);
int t=Integer.parseInt(op[1]);
Arr[Num-1]=新条目(v,t);
Num--;
}
而(ytlarge)tlarge=Arr[y].getT();
等于[Arr[y].getT()]=false;
y++;
}
快速排序(0,N-1);//从零到无任务
而(小时数[0,N-1]
的数组equal
设置为false
。但它实际上用于范围[0,H]
,没有任何说明H
应该小于N
。这意味着可能存在影响交叉测试用例
(二)
3) 对于所使用的时间段,您没有将其设置为true
if((equal[Arr[z].getT()]!=true) || hours<Arr[z].getT()) {
if(Arr[z].getT()<=tlarge) {
if(z<tlarge) {
loss -= Arr[z].getV();
equal[Arr[z].getT()]=true;
hours++;
}
}
}
这些可能只是您应该解决的主要问题。但是正如您所看到的,这个算法需要重新思考
建议修复(扰流板)
- 按您的方式处理所有排序的任务,但对于可能的最高点,将其设置为true,低于其结束时间
- 如果找不到更低的值,则任务无法完成,其值应添加到损失中
import java.util.Arrays;
import java.util.Scanner;
public class Main {
static class Entry implements Comparable<Entry> {
private final int v, t;
Entry(int v, int t) {
this.v = v;
this.t = t;
}
public int getV() {
return v;
}
public int getT() {
return t;
}
@Override
public int compareTo(Entry oth) {
if(this.v == oth.v){
Integer.signum(this.t - oth.t);
}
return Integer.signum(oth.v - this.v);
}
}
public static void main(String[] args) throws java.lang.Exception {
final Scanner in = new Scanner(System.in);
while (in.hasNext()) {
int N = in.nextInt();
int H = in.nextInt();
Entry[] arr = new Entry[N];
for(int i=0;i<N;i++) {
int v = in.nextInt();
int t = in.nextInt();
arr[i] = new Entry(v, t);
}
Arrays.sort(arr);
Entry[] entryByHour = new Entry[H+1];
int loss = 0;
for(int i=0;i<arr.length;i++){
int j=arr[i].t-1;
while(j>=0 && entryByHour[j] != null){
j--;
}
if(j < 0){
loss += arr[i].v;
}else{
entryByHour[j] = arr[i];
}
}
System.out.printf("%d\n", loss);
}
in.close();
}
}
导入java.util.array;
导入java.util.Scanner;
公共班机{
静态类条目实现了可比较的{
私人终场积分v,t;
条目(整数v,整数t){
这个,v=v;
t=t;
}
公共int getV(){
返回v;
}
公共int getT(){
返回t;
}
@凌驾
公共内部比较(输入/输出){
if(this.v==oth.v){
整数.signum(this.t-oth.t);
}
返回整数.signum(oth.v-this.v);
}
}
公共静态void main(字符串[]args)引发java.lang.Exception{
最终扫描仪输入=新扫描仪(系统输入);
while(在.hasNext()中){
int N=in.nextInt();
int H=in.nextInt();
条目[]arr=新条目[N];
对于(int i=0;我建议你解释一下你的逻辑是什么,以及为什么你认为它是有效的。这样我们就可以给你一些你可能认为错误的线索。我已经相应地编辑了我的代码。对于第二种情况-总时间(4 1)不能小于任何任务的单个时间。对于第一种情况,我对单个任务小时数进行了更改,将equal flag设置为false。哎呀,我添加了错误的情况。我想知道为什么要将z(排序数组中的索引)检查得更小
if((equal[Arr[z].getT()]!=true) || hours<Arr[z].getT()) {
if(Arr[z].getT()<=tlarge) {
if(z<tlarge) {
loss -= Arr[z].getV();
equal[Arr[z].getT()]=true;
hours++;
}
}
}
4 4
1 1
5 2
5 2
1 4
expected: 1
result: 0
import java.util.Arrays;
import java.util.Scanner;
public class Main {
static class Entry implements Comparable<Entry> {
private final int v, t;
Entry(int v, int t) {
this.v = v;
this.t = t;
}
public int getV() {
return v;
}
public int getT() {
return t;
}
@Override
public int compareTo(Entry oth) {
if(this.v == oth.v){
Integer.signum(this.t - oth.t);
}
return Integer.signum(oth.v - this.v);
}
}
public static void main(String[] args) throws java.lang.Exception {
final Scanner in = new Scanner(System.in);
while (in.hasNext()) {
int N = in.nextInt();
int H = in.nextInt();
Entry[] arr = new Entry[N];
for(int i=0;i<N;i++) {
int v = in.nextInt();
int t = in.nextInt();
arr[i] = new Entry(v, t);
}
Arrays.sort(arr);
Entry[] entryByHour = new Entry[H+1];
int loss = 0;
for(int i=0;i<arr.length;i++){
int j=arr[i].t-1;
while(j>=0 && entryByHour[j] != null){
j--;
}
if(j < 0){
loss += arr[i].v;
}else{
entryByHour[j] = arr[i];
}
}
System.out.printf("%d\n", loss);
}
in.close();
}
}