#include <iostream> #include <map> using namespace std; static const int MAXN=2e5+10; map<int,int> ghs[MAXN]; int dp[MAXN],nxt[MAXN],n,head; int ans[MAXN]; int main() { scanf("%d",&n); for(int i=1;i<=n;i++) { int v,c,l,r; scanf("%d%d%d%d",&v,&c,&l,&r); if(!l || ghs[l].count(r+c)) { dp[i]=dp[ghs[l][r+c]]+v; nxt[i]=ghs[l][r+c]; if(!r && dp[i]>dp[head]) head=i; if(dp[i]>dp[ghs[l+c][r]]) ghs[l+c][r]=i; } } int idx=0; for(int i=head;i;i=nxt[i]) ans[idx++]=i; printf("%d\n",idx); for(int i=idx-1;i>=0;i--) printf("%d ",ans[i]); return 0; }将它转化成java代码
时间: 2024-04-02 20:31:07 浏览: 90
把C++代码转换成Java代码如下:
import java.util.*;
public class Main {
private static final int MAXN = 2 * 100000 + 10;
private static Map<Integer, Integer>[] ghs = new Map[MAXN];
private static int[] dp = new int[MAXN];
private static int[] nxt = new int[MAXN];
private static int[] ans = new int[MAXN];
private static int n, head;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
n = scanner.nextInt();
for (int i = 1; i <= n; i++) {
int v = scanner.nextInt();
int c = scanner.nextInt();
int l = scanner.nextInt();
int r = scanner.nextInt();
if (ghs[l] == null) {
ghs[l] = new HashMap<Integer, Integer>();
}
if (l == 0 || ghs[l].containsKey(r + c)) {
dp[i] = dp[ghs[l].get(r + c)] + v;
nxt[i] = ghs[l].get(r + c);
if (r == 0 && dp[i] > dp[head]) {
head = i;
}
if (!ghs[l + c].containsKey(r) || dp[i] > dp[ghs[l + c].get(r)]) {
ghs[l + c].put(r, i);
}
}
}
int idx = 0;
for (int i = head; i != 0; i = nxt[i]) {
ans[idx++] = i;
}
System.out.println(idx);
for (int i = idx - 1; i >= 0; i--) {
System.out.print(ans[i] + " ");
}
}
}
阅读全文