Sibin_Thomas's blog

By Sibin_Thomas, history, 4 years ago, In English

I wanted to take input from a file and print output to another file. I am able to take the input successfully(Tested using System.out) but there is no output on the output file.

import java.util.*;
import java.io.*;

class a{

	BufferedReader br;
	PrintWriter out;
	StringTokenizer st;

	public a() throws Exception{
		if (System.getProperty("ONLINE_JUDGE") == null) {
			br = new BufferedReader(new FileReader("input.txt"));
			out = new PrintWriter(new FileWriter("output.txt"));
		}
		else {
			br = new BufferedReader(new InputStreamReader(System.in));
			out = new PrintWriter(System.out);
		}
	}

	public void solve() throws IOException{
		int n = Integer.parseInt(br.readLine());
		out.println(n);
		out.flush();
	}

	public static void main(String[] args) throws Exception{
		new a().solve();
	}
}

Full text and comments »

  • Vote: I like it
  • 0
  • Vote: I do not like it

By Sibin_Thomas, history, 4 years ago, In English

I don't understand why my code is giving TLE. Not able to catch the bug. Any help would be appreciated !!! Link to Question

#include<bits/stdc++.h>
# define    all(a)              a.begin(), a.end()
#define MOD (long long int )1e9+7
using namespace std;

typedef long long int ll;

ll dp[110][100010];
ll solve(int index, ll currweight, ll weight, vector<vector<int>> nums, int n) {
    if (index == n)
        return 0;
    if (dp[index][currweight] != -1)
        return dp[index][currweight];
    ll res = solve(index+1, currweight, weight, nums, n);
    if (currweight+nums[index][0] <= weight) {
        res = max(res, nums[index][1] + solve(index+1, currweight+nums[index][0], weight, nums, n));
    }
    dp[index][currweight] = res;
    return res;
}


int main() {
    #ifndef ONLINE_JUDGE
    // for getting input from input.txt
    freopen("input.txt", "r", stdin);
    // for writing output to output.txt
    freopen("output.txt", "w", stdout);
    #endif
    int n, k;
    cin >> n >> k;
    vector<vector<int>> nums(n, vector<int>(2));
    for (int i=0; i<n; i++) cin >> nums[i][0] >> nums[i][1];
    memset(dp, -1, sizeof(dp));
    ll res = solve(0, 0, k, nums, n); 
    cout << res << endl;
    return 0;
}

Full text and comments »

  • Vote: I like it
  • -9
  • Vote: I do not like it