I forgot to sort the vectors before doing a binary search lol
Read more… (12 words)
What's wrong with this?
#include <vector>#include <cstdio>#include <algorithm>using namespace std;vector<int> V;vector<int> W;int Vi = -1, Wi = -1, S, E;int t;void _Vi(int low, int high){if (low > high)return;else{int mid = (low + high) / 2;if (V[mid] <= S){Vi = mid;_Vi(mid + 1, high);}else _Vi(low, mid - 1);}}void _Wi(int low, int high){if (low > high)return;else{int mid = (low + high) / 2;if (W[mid] >= E){Wi = mid;_Wi(low, mid - 1);}else _Wi(mid + 1, high);}}void time(){if (Vi != -1 && Wi != -1)t = W[Wi] - V[Vi] + 1;else t = -1;}void solve(){_Vi(0, V.size() - 1);_Wi(0, W.size() - 1);time();}int main(){int N, X, Y, _S, _E, temp, n;scanf("%d %d %d", &N, &X, &Y);n = N;vector<int> time_spent;time_spent.reserve(N);V.reserve(X);W.reserve(Y);vector<int> S_E;S_E.reserve(N * 2);while (N--){scanf("%d %d", &_S, &_E);S_E.push_back(_S);S_E.push_back(_E);}while (X--){scanf("%d", &temp);V.push_back(temp);}while (Y--){scanf("%d", &temp);W.push_back(temp);}for (int i = 0; i < n * 2; i += 2){S = S_E.at(i);E = S_E.at(i + 1);solve();if (t != -1)time_spent.push_back(t);}sort(time_spent.begin(), time_spent.end());printf("%d\n", time_spent.at(0));}
Why doesn't my code get accepted? Any help is appreciated :)
import java.io.BufferedReader;import java.io.InputStreamReader;import java.util.TreeSet;class Main {public static void main (String[] args) throws Exception {BufferedReader br = new BufferedReader(new InputStreamReader(System.in));int N = Integer.parseInt(br.readLine());String[] words;TreeSet<String> lwords = new TreeSet<String>();for (int i = 0; i < N; i++) {words = br.readLine().split("\\s");for (String word : words) {if (word.length() <= 0) {}else {if (word.contains(",") || word.contains("'") || word.contains(";") || word.contains(":") || word.contains(".")){for (int j = 0; j < word.length(); j++){if (word.charAt(j) == ',' || word.charAt(j) == '\'' || word.charAt(j) == ';' || word.charAt(j) == ':' || word.charAt(j) == '.'){word = word.substring(0, j) + word.substring(j + 1, word.length());j--;}}}word = word.toLowerCase();lwords.add(word);}}}System.out.println(lwords.size());for (String word : lwords)System.out.println(word);}}
Here's my solution:
#include <cstdio>#include <iostream>using namespace std;int area(int x1, int y1, int x2, int y2){return (x2 - x1) * (y2 - y1);}int main(){freopen("bendin.txt", "r", stdin);freopen("bendout.txt", "w", stdout);int Ax1, Ax2, Ay1, Ay2, Bx1, Bx2, By1, By2, Ix1, Ix2, Iy1, Iy2, areaA, areaB, areaI, temp;cin >> Ax1 >> Ay1 >> Ax2 >> Ay2 >> Bx1 >> By1 >> Bx2 >> By2;Ix1 = (Bx1 > Ax1) ? Bx1 : Ax1;Ix2 = (Bx2 < Ax2) ? Bx2 : Ax2;Iy1 = (By1 > Ay1) ? By1 : Ay1;Iy2 = (By2 < Ay2) ? By2 : Ay2;areaA = area(Ax1, Ay1, Ax2, Ay2);