"covers segment [0, M] completely" means: 0 1 2 3 does not cover [0,3] completely 0 1 1 3 covers [0,3] completely Thank you for your tests! Tested. Still WA#2 Maybe you printed “No solution[space]” instead of “No solution” like me :)) This is so dumb. But thanks for clarification. I don't understand why they did not mention it in problem statement. Or I might have not noticed it. Could anybody help me? I have WA at 3th test .I tried many test and answers were correct .I also tried this test and answer was correct: 5 -5 0 0 1 1 2 2 3 4 5 3 4 10 49000 0 0 Hey, this is the test 16 - it was a long path to get there. What can possibly go wrong? I don't understand why my prog don't work Can you give me some data or advice Edited by author 20.03.2007 14:04 Edited by author 20.03.2007 14:04 10 0 9 0 0 I had "Output limit exceeded" on this test, but it actually was WA12. It works. Thanks man, appreciate it! <3 i have tried all cases and it seems correct but cant pass test 13. Can someone help me what is test 13??? Английский я знаю плохо, поэтому sorry:) У меня тоже был WA 12. Попробуй тест: 1 49999 50000 0 0 TY so much <3 :(( Английский я знаю плохо, поэтому sorry:) У меня тоже был WA 12. Попробуй тест: 1 49999 50000 0 0 Edited by author 25.03.2022 08:26I have solved this problem by using an easy greedy algorithm. MAIN: you need to use an idea of 2 pointers(of course you need to sort array) and take the rightest segment. Left part of segment need to be <= x(another pointer) The solution below: void solve() { int m; cin >> m; vector< pair<int, int> > pi; int a, b; cin >> a >> b; while (a || b) { pi.pb({a, b}); cin >> a >> b; } sort(all(pi)); int cnt = 0, j = 0; vector< pair<int, int> > ans; j = 0; int x = 0; for (; j < sz(pi) && x < m; j++) { int k = j; int mx = x; pair<int, int> par = {-INF, -INF}; while (k < sz(pi) && pi[k].ff <= x) { if (mx < pi[k].ss) { mx = pi[k].ss; par = pi[k]; } k++; } if (par.ff == -INF) { cout << "No solution\n"; return; } x = mx; ans.pb(par); if (k > j) j = k - 1; } if (x < m) { cout << "No solution\n"; return; } cout << sz(ans) << '\n'; for (auto &i : ans) { cout << i.ff << ' ' << i.ss << '\n'; } } always choose segments with rightest right points dp[i] - минимальное количество отрезков, если последний отрезок заканчивался в позиции i. ответ dp[m]. What can you tell me about this test? I'm having WA all the time Same question. I tried all tests of this problem's forum and still got WA3. Edited by author 25.02.2013 21:48 just try 5 -5 0 0 1 1 2 2 3 4 5 3 4 10 49000 0 0 There is full solution!!! Try thinking yourself or using hints before watching it and use visual c++ 2017 . . . . . . . . . . . . . . . . . . . //#include <bits/stdc++.h> #include <iostream> #include <vector> #include <algorithm> #define int long long #define pb push_back using namespace std; const int N = 50000; pair<int, int> dp[N], right_end[N]; vector<pair<int, int> > v; vector<int> ans; unsigned main() { for(int i = 0; i < N; i++) { right_end[i].first = -50001; right_end[i].second = -50001; } int m; cin >> m; int l, r; int k = 0; while(true) { cin >> l >> r; if(l > m || r < 0) continue; if(l == 0 && r == 0) break;
v.pb({l, r});
if(r > right_end[l].first) { right_end[l].first = r; right_end[l].second = k; } k++; }
int i = 0; for(auto it: v) { if(it.first <= 0 && it.second > 0) { if(it.second > dp[0].first) { dp[0].first = it.second; dp[0].second = i; } } i++; } for(int i = 1; i <= m; i++) { if(dp[i - 1].first >= right_end[i].first) { dp[i].first = dp[i - 1].first; dp[i].second = dp[i - 1].second; } else { dp[i].first = right_end[i].first; dp[i].second = right_end[i].second; } } for(int i = 0; i <= m; i++) { if(dp[i].first < i) { cout << "No solution" << endl; return 0; } } int pos = 0; while(pos < m) { ans.pb(dp[pos].second); if(dp[pos].first == pos) { cout << "No solution" << endl; return 0; } pos = dp[pos].first; } cout << ans.size() << endl; for(auto it: ans) { cout << v[(int)it].first << " " << v[(int)it].second << endl; } } Edited by author 09.02.2020 01:50 Could anybody help me? I have WA at 4th test! u may try this test: 2 1 2 0 1 0 0 so output should be: 2 0 1 1 2 when WA is 2 1 2 0 1 This test helped me: 4 0 4 -5 0 3 4 -4 4 0 0 Answer is: 1 -4 4 Is the answer : 1 0 4 right? i have wa 4 too,and i am confused about the output principles. This test helped me: 10 -5 1 1 14 -5 2 2 3 3 19 0 0 The answer is: 2 -5 1 1 14 Now, let's fight with WA5 :D why not this: 2 -5 2 1 14 ? Actually, what if there are several possibilites to cover? I think that it does not matter which one you choose, as long as it has a minimum number of intervals. I guess the judge just tests whether the intervals you give cover the large interval. Best regards, Erik Edited by author 25.02.2013 04:56 This test helped me too. At last got AC. My solution is similar to task 1987. Edited by author 05.09.2019 16:47 1) greedy works here, although it's not that much obvious how *correct* greedy should look like 2) 5th test case, that didn't pass for me: 12 -3 10 -2 8 -1 16 0 0 Edited by author 17.02.2018 04:30 The problem is quite evil. The problem is actually very easy. It is hard to understand that the problem is that easy. Possible solution. First , eliminate all such segments I=[l;r], that l>M or r<0, because of such I's can't appear in the answer. Second, build your dp[]. Your dp[x] contains rightmost right end of all such segments [l;r], that l<=x. Finally, just start from segment whose right end is dp[0], jump to a segment whose right end is dp[dp[0]] and so on. How to build dp[x]? I have used an additional array right_end[y]. Where right_end[y] is the right end of a segment with maximal length from all segments whose left end is y. dp[x] =max(dp[x-1], right_end[x]). Hi! I use greedy algorithms and can't get past first test. Could someone please provide me with some tests? I tried these tests with following results: 1 -1 0 -5 -3 2 5 0 0 ----------- No solution 1 -1 0 0 1 0 0 ----------- 0 1 10 -5 1 1 14 -5 2 2 3 3 19 0 0 --------- -5 2 1 14 5 -5 0 0 1 1 2 2 3 4 5 3 4 10 49000 0 0 --------- 0 1 1 2 2 3 3 4 4 5 4 0 4 -5 0 3 4 -4 4 0 0 ------- -4 4 6 0 5 7 8 0 0 ------- No solution 10 0 9 0 0 ------- No solution you don't print segment's count first line #include <bits/stdc++.h> #define f first #define s second #define pb push_back #define ppb pop_back #define mp make_pair #define pii pair <int, int> #define pll pair <ll, ll> #define ld long double #define ll long long #define ull unsigned ll #define bit(x) __builtin_popcountll(x) #define all(x) x.begin(), x.end() #define sqr(x) ((x) * 1ll * (x)) #define sz(x) (int)x.size() #define purple ios_base :: sync_with_stdio(0), cin.tie(0), cout.tie(0); #define rep(_i, _from, _to) for (int _i = _from; _i <= _to; _i++) #define per(_i, _from, _to) for (int _i = _from; _i >= _to; _i--) #define nl '\n' #define ioi exit(0); #define _225day "" using namespace std; const int N = 1e6 + 7, mod = 1e9 + 9, inf = 1e9 + 7; const ll linf = (ll)1e18 + 7; const ld eps = 1e-15, pi = 3.141592; const int dx[] = {-1, 0, 1, 0, 1, -1, -1, 1}, dy[] = {0, 1, 0, -1, 1, -1, 1, -1}; int n, m, base = 50000; int s[N]; pii a[N]; struct Tree{ int ans, add; } t[N << 2]; inline bool cmp(pii x, pii y){ if (x.f != y.f) return x.f < y.f; } inline bool Check(int pref){ memset(s, 0, sizeof(s)); rep(i, 1, pref) s[a[i].f]++, s[a[i].s + 1]--; rep(i, 0, m + base){ s[i] += s[i - 1]; if (i > base && !s[i]) return 0; } return 1; } inline void Push(int v, int tl, int tr){ if (t[v].add && tl != tr){ t[v + v].add += t[v].add, t[v + v + 1].add += t[v].add; t[v + v].ans += t[v].add, t[v + v + 1].ans += t[v].add; t[v].add = 0; } } inline void Update(int l, int r, int add, int v = 1, int tl = 0, int tr = N){ Push(v, tl, tr); if (l <= tl && tr <= r){ t[v].ans += add; t[v].add += add; return; } if (tl > r || tr < l) return; int tm = tl + tr >> 1; Update(l, r, add, v + v, tl, tm); Update(l, r, add, v + v + 1, tm + 1, tr); t[v].ans = min(t[v + v].ans, t[v + v + 1].ans); } inline int Get(int l, int r, int v = 1, int tl = 0, int tr = N){ Push(v, tl, tr); if (l <= tl && tr <= r) return t[v].ans; if (tl > r || tr < l) return inf; int tm = tl + tr >> 1; return min(Get(l, r, v + v, tl, tm), Get(l, r, v + v + 1, tm + 1, tr)); } int main(){ #ifndef _225day freopen (_225day".in", "r", stdin); freopen (_225day".out", "w", stdout); #endif cin >> m; while (1){ int x, y; cin >> x >> y; if (x == 0 && y == 0) break; a[++n] = {x + base + 1, y + base}; } sort (a + 1, a + 1 + n, cmp); int l = 1, r = n, ans = -1, mid; while (l <= r){ mid = l + r >> 1; if (Check(mid)) ans = mid, r = mid - 1; else l = mid + 1; } if (ans == -1) cout << "No solution", ioi ans = n; rep(i, 1, ans) Update(a[i].f, a[i].s, 1); vector <pii> res; rep(i, 1, ans){ Update(a[i].f, a[i].s, -1); if (!Get(base + 1, m + base)) Update(a[i].f, a[i].s, 1), res.pb({a[i].f, a[i].s}); } sort (all(res), cmp); cout << sz(res) << nl; for (auto i : res) cout << i.f - base - 1 << ' ' << i.s - base << nl; ioi } Same problem :( WA5, WA5, WA5... What causes that? When my sort compare function is: bool cmp(pair<int, int> p1, pair<int, int> p2) { if(p1.first != p2.first) return p1.first < p2.first; else return p1.second >= p2.second; } I get WA#14. And then delete the "=" like : bool cmp(pair<int, int> p1, pair<int, int> p2) { if(p1.first != p2.first) return p1.first < p2.first; else return p1.second > p2.second; } I get AC; Try this test: 5 0 3 3 5 5 7 0 0 AC answer is: 2 0 3 3 5 not: 3 0 3 3 5 5 7 Исправьте слово "соледует" в последнем предложении Результата: "соледует вывести единственную фразу «No solution»". #include <cstdio> #include <climits> #include <cstdlib> #include <algorithm> #include <list> #include <vector> using namespace std; #define PB push_back #define BEG begin() #define MP make_pair #define F first #define S second bool compare(const pair<int,int> &left, const pair<int,int> &right){ return left.F <= right.F; } int main(){ int M; int readInt1, readInt2; vector<pair<int,int> > segments; vector<int> solutionInd; scanf("%d", &M); while(scanf("%d %d", &readInt1, &readInt2) && !(readInt1 == 0 && readInt2 == 0)){ segments.PB(MP(readInt1, readInt2)); } sort(segments.begin(), segments.end(), compare); if(segments.empty() || segments[0].F > 0){ printf("No solution\n"); return 0; }
int currEnd = 0; int segInd = 0; int ansInd = -1; int maxEnd = 0;
while(segInd < segments.size()){ while(segInd < segments.size() && segments[segInd].F <= currEnd){ if(segments[segInd].S > maxEnd){ maxEnd = segments[segInd].S; ansInd = segInd; }
segInd++; } solutionInd.PB(ansInd); currEnd = maxEnd;
if(currEnd >= M || segments[segInd].F > currEnd) break; } if(solutionInd.empty() || currEnd < M){ printf("No solution\n"); } else{ printf("%d \n", solutionInd.size()); for(int i = 0; i<solutionInd.size(); i++){ printf("%d %d\n", segments[solutionInd[i]].F, segments[solutionInd[i]].S); } }
}
Keep getting runtime error(Access violation) even though I can't find anywhere it is possible to go outside the vectors memory space.
Update: The problem was accepted when using visual studio compiler instead of g++. Same thing happened to me. But why is that? Hi, I get WA6 with g++ but get runtime error access violation on test case 14 with Visual Studio 2010 C++. Code : /* * File: main.cpp * Author: Parnami * Created on April 14, 2014, 10:18 PM * Description : TIMUS Online Judge Problem ID : 1303 (DP) */ #include <stdio.h> #include <iostream> #include <algorithm> #include <vector> #include <map> #include <string> #include <queue> #include <cmath> #include <utility> #include <limits.h> using namespace std; inline int fastRead() { int input; char c=0; while (c<33) c=getchar(); input=0; while (c>33) { input=input*10+c-'0'; c=getchar(); } return input; } vector < pair <int,int> > myVec,answer; bool inRange(pair <int,int> cur, int starter) { if(starter>=cur.first && starter<cur.second) return true; return false; } int main(int argc, char** argv) { int m,starter,ender,iter,i,flag; pair <int,int> maxxer,current; m = fastRead(); while(1) { cin>>starter>>ender; if(starter==0&&ender==0) break; if(ender<=0||(starter>=m&&ender>m)) { //Do Nothing } else { //cout<<"Pushing "<<starter<<"-"<<ender<<endl; myVec.push_back(make_pair(starter,ender)); } } if(!myVec.empty()) { sort(myVec.begin(),myVec.end()); iter = 0; starter=0; while(starter<m) { //cout<<starter<<endl; current = myVec[iter]; //cout<<"Yahaan Phuncha"<<endl; flag = 0; while(!inRange(current,starter) && iter!=myVec.size()) { //cout<<"Not in range : "<<current.first<<"-"<<current.second<<endl; iter++; current = myVec[iter]; } if(iter==myVec.size()) { //cout<<"End of vector nowhere to search"<<endl; flag = 1; break; } maxxer = current; iter++; if(iter==myVec.size()) {
} else { while(1) { if(myVec[iter].first>starter) { break; } else { if(myVec[iter].second>maxxer.second) { maxxer = myVec[iter]; } iter++; } } } //cout<<"Pushing into answer "<<maxxer.first<<"-"<<maxxer.second<<endl; answer.push_back(maxxer); starter = maxxer.second; } } else flag=1; if(flag) { cout<<"No solution"<<endl; } else { cout<<answer.size()<<endl; for(i=0;i<answer.size();i++) { cout<<answer[i].first<<" "<<answer[i].second<<endl; } } return 0; } Edited by author 15.04.2014 04:19 Try this test: 6 0 4 4 4 4 6 0 0 Edited by author 02.06.2008 18:49 or this 6 0 1 1 2 2 3 3 4 4 5 5 6 0 0 I have wa5,and i have ra on these two test . Edited by author 19.08.2011 14:18 Edited by author 19.08.2011 14:18 You may also try this 10 49999 50000 0 0 |
|