Seeking Help with Reading Complex Inputs in Rust

Revision en7, by rizzgod_tourist, 2024-07-25 19:06:02

Hello Codeforces community,

I'm reaching out for help with a Rust programming issue that's been frustrating me for a while. As a beginner in Rust, I'm struggling to understand how to read complex inputs, especially when it comes to competitive programming.

I've been trying to learn Rust, but I'm finding it challenging to grasp the concept of input/output handling. I've watched YouTube tutorials, but they only cover basic examples like "Hello, World!" or reading simple integers , also i have tried exploring some of the udemy courses but they are teaching all the complex stuff but they are not telling how to read input . However, when it comes to more complex inputs, such as reading multiple test cases with varying input formats, I'm lost.

I've tried to convert my C++ code to Rust using AI-powered tools like Blackbox AI and ChatGPT, but the generated code doesn't work as expected. I've also used C++ to Rust converters, but the resulting code doesn't produce any output.

Here's my C++ code that I'm trying to convert to Rust:

#include<bits/stdc++.h>

using namespace std;


void solve(){

    int n, p;

    cin >> n >> p;

    vector<pair<int,int>>vec(n);

    for(int i=0;i<n;i++){

        cin>>vec[i].second;

    }

    for(int i=0;i<n;i++){

        cin>>vec[i].first;

    }

    sort(vec.begin(),vec.end());

    int need=n-1;

    long long  ans=p;

    for(int i=0;i<n;i++){

        if(vec[i].first<=p){

            int t=min(need,vec[i].second);

            need-=t;

            ans+=(long long)t*vec[i].first;

        }

    }

    cout<<ans+(long long)need*p<<endl;

}


int main(){

    int t;

    cin >> t;

    while(t--){

        solve();

    }

} ```


The AI-generated Rust code is:


use std::io::{self, BufRead};

use std::cmp::min;

fn solve() {

let stdin = io::stdin();

let mut input = stdin.lock().lines();



let first_line = input.next().unwrap().unwrap();

let mut iter = first_line.split_whitespace();

let n: usize = iter.next().unwrap().parse().unwrap();

let p: i64 = iter.next().unwrap().parse().unwrap();



let mut vec: Vec<(i64, i64)> = Vec::with_capacity(n);



for _ in 0..n {

    let value: i64 = input.next().unwrap().unwrap().parse().unwrap();

    vec.push((0, value));

}



for i in 0..n {

    let value: i64 = input.next().unwrap().unwrap().parse().unwrap();

    vec[i].0 = value;

}



vec.sort();



let mut need = n as i64 - 1;

let mut ans = p;



for i in 0..n {

    if vec[i].0 <= p {

        let t = min(need, vec[i].1);

        need -= t;

        ans += t * vec[i].0;

    }

}



println!("{}", ans + need * p);

}

fn main() {

let stdin = io::stdin();

let input = stdin.lock().lines();

let mut iter = input.map(|line| line.unwrap()).peekable();



let t: usize = iter.next().unwrap().parse().unwrap();



for _ in 0..t {

    solve();

}

} ```

However, this code is giving me an "idleness limit" error, which I've never seen before in C++ or Java.

If someone could provide guidance on how to read complex inputs in Rust or share a working code for this problem without using templates, I would greatly appreciate it. I'm eager to learn and understand the underlying concepts, rather than just copying and pasting code.I don't want to use templates , first i want to write the raw code i don't care about writing some more line of code but i want to know everything from scratch also when i will become somewhat decent then i will write my own template i don't want to blindly follow some template and simply say that reading input and output is complicated in rust.

I've searched for Rust submissions for this problem (https://codeforces.net/contest/1876/problem/A), but out of nearly 7,000 submissions, I only found 6-7 in Rust, and they were all using templates for reading inputs.

Please help me overcome this hurdle, and I'll be grateful for any resources or explanations that can aid my learning journey in Rust.

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en9 English rizzgod_tourist 2024-07-25 19:09:33 5 (published)
en8 English rizzgod_tourist 2024-07-25 19:09:03 88
en7 English rizzgod_tourist 2024-07-25 19:06:02 95
en6 English rizzgod_tourist 2024-07-25 19:04:21 94
en5 English rizzgod_tourist 2024-07-25 19:02:34 5 Tiny change: 'n\n\n\n```\nuse std:' -> 'n\n\n\n``` Rust\nuse std:'
en4 English rizzgod_tourist 2024-07-25 19:01:47 14
en3 English rizzgod_tourist 2024-07-25 18:59:17 127 (saved to drafts)
en2 English rizzgod_tourist 2024-07-25 18:57:29 146
en1 English rizzgod_tourist 2024-07-25 18:45:40 4323 Initial revision (published)