#include <bits/stdc++.h> using namespace std; #def...
创建于:2025年3月23日
使用 O3 Mini High 回答 Chat01
创建于:2025年3月23日
使用 O3 Mini High 回答 Chat01
#include <bits/stdc++.h>
using namespace std;
#define int long long
set<vector<int>> st,st2;
void pre(int lm){
for(int i=0;i<=lm;i++){
for(int j=0;j<=lm;j++){
st.insert({3i+3j+2,3i+1,3j+1});
st.insert({3i+3j+3,3i+1,3j+2});
st.insert({3i+3j+3,3i+2,3j+1});
st.insert({3i+3j+6,3i+2,3j+2});
}
}
textfor(int i=0;i<=lm;i++){ for(int j=0;j<=lm;j++){ st2.insert({3*i+3*j+2,3*i+1,3*j+1}); } }
}
void solve(){
int n; cin >> n;
textint lm = sqrt(2*n); pre(lm); vector<int> t(n); for(auto &p:t){ cin >> p; if(p == 1){ auto it = *st.begin(); st.erase(it); st2.erase(it); cout << it[1] << " " << it[2] << endl; } else{ auto it = *st2.begin(); st.erase(it); st2.erase(it); cout << it[1] << " " << it[2] << endl; } }
}
int32_t main() {
textint t; cin >> t; while(t--){ solve(); }
}
This is the correct c++ code
But my java code is giving wrong answer
import java.io.;
import java.util.;
public class Mainn {
static class FastReader {
BufferedReader br;
StringTokenizer st;
textpublic FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } } public static List<Integer> primefactor(int n){ List<Integer> arr=new ArrayList<>(); for(int i=2;i*i<=n;i++){ while(n%i==0){ arr.add(i); n/=i; } } if(n>1) arr.add(n); return arr; } public static void main(String[] args) { FastReader sc = new FastReader(); int tc = sc.nextInt(); StringBuilder sb = new StringBuilder(); ArrayList<int[]> arr=new ArrayList<>(); ArrayList<int[]> brr=new ArrayList<>(); for(int i=0;i<=350;i++){ for(int j=0;j<=350;j++){ arr.add(new int[]{3*i+3*j+2,3*i+1,3*j+1}); arr.add(new int[]{3*i+3*j+3,3*i+1,3*j+2}); arr.add(new int[]{3*i+3*j+3,3*i+2,3*j+1}); arr.add(new int[]{3*i+3*j+6,3*i+2,3*j+2}); } } for(int i=0;i<=350;i++){ for(int j=0;j<=350;j++){ brr.add(new int[]{3*i+3*j+2,3*i+1,3*j+1}); } } Collections.sort(arr,(x,y)->(x[0]==y[0])?(x[1]-y[1]):(x[0]-y[0])); Collections.sort(brr,(x,y)->(x[0]==y[0])?(x[1]-y[1]):(x[0]-y[0])); //for(int it[]:brr) System.out.println(Arrays.toString(it)); label: while (tc-- > 0) { int n=sc.nextInt(); int a[]=new int[n]; for(int i=0;i<n;i++) a[i]=sc.nextInt(); int x=0,y=0; for(int it:a){ if(it==1){ System.out.println(arr.get(x)[1]+" "+arr.get(x)[2]); if(brr.get(y)[1]==arr.get(x)[1] && brr.get(y)[2]==arr.get(x)[2]) y++; x++; } else{ System.out.println(brr.get(y)[1]+" "+brr.get(y)[2]); if(brr.get(y)[1]==arr.get(x)[1] && brr.get(y)[2]==arr.get(x)[2]) x++; y++; } } } System.out.print(sb); }
}
It seems like your Java code is performing similar logic to the C++ code but with some differences in how data is handled. Let me go over a few potential issues and suggestions for improvement.
arr
and brr
:
st
and st2
are sets that maintain the order of insertion and also guarantee uniqueness. In Java, you are using ArrayList<int[]>
and sorting them based on the first element (x[0]
). However, the logic in C++ doesn't involve sorting st
and st2
; they are just sets, and the order of traversal depends on the set's internal structure.x
and y
):
x
and y
are intended to track the position in arr
and brr
, respectively. However, the logic for determining whether to increment x
or y
based on matching values is flawed in your Java code.if(brr.get(y)[1]==arr.get(x)[1] && brr.get(y)[2]==arr.get(x)[2]) y++
might not reflect the exact logic intended by the C++ code.arr
and brr
, you should treat them as LinkedHashSet
in Java to preserve the order of insertion, just like C++ sets do.x
and y
should be adjusted to match the behavior from C++. Essentially, you should only increment x
or y
when you are sure that the right set has been processed.Here is an updated version of your Java code with the necessary fixes:
javaimport java.io.*; import java.util.*; public class Mainn { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } } public static void main(String[] args) { FastReader sc = new FastReader(); int tc = sc.nextInt(); StringBuilder sb = new StringBuilder(); // Initialize the sets directly instead of sorting. Set<List<Integer>> st = new LinkedHashSet<>(); Set<List<Integer>> st2 = new LinkedHashSet<>(); for (int i = 0; i <= 350; i++) { for (int j = 0; j <= 350; j++) { st.add(Arrays.asList(3 * i + 3 * j + 2, 3 * i + 1, 3 * j + 1)); st.add(Arrays.asList(3 * i + 3 * j + 3, 3 * i + 1, 3 * j + 2)); st.add(Arrays.asList(3 * i + 3 * j + 3, 3 * i + 2, 3 * j + 1)); st.add(Arrays.asList(3 * i + 3 * j + 6, 3 * i + 2, 3 * j + 2)); } } for (int i = 0; i <= 350; i++) { for (int j = 0; j <= 350; j++) { st2.add(Arrays.asList(3 * i + 3 * j + 2, 3 * i + 1, 3 * j + 1)); } } label: while (tc-- > 0) { int n = sc.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = sc.nextInt(); // Process each query for (int it : a) { if (it == 1) { // Take from st List<Integer> curr = st.iterator().next(); // Get the first element st.remove(curr); st2.remove(curr); sb.append(curr.get(1)).append(" ").append(curr.get(2)).append("\n"); } else { // Take from st2 List<Integer> curr = st2.iterator().next(); // Get the first element st.remove(curr); st2.remove(curr); sb.append(curr.get(1)).append(" ").append(curr.get(2)).append("\n"); } } } System.out.print(sb.toString()); } }
Replaced Sorting with LinkedHashSet:
LinkedHashSet
instead of sorting the arrays to maintain insertion order.Simplified Logic for Element Removal:
st
and st2
, I directly get the first element using iterator().next()
, and then remove it from both sets.Output Handling:
StringBuilder
to append the output for efficiency, and at the end, the result is printed.The LinkedHashSet
is used to maintain the insertion order, and the set operations are generally O(1), so the performance should be in line with the original C++ code.
This approach should now match the behavior of the C++ code more closely and give the correct answer.