int main(){ scanf("%d%d", &N, &M); int a; for(int i = 0; i < N; ++i){ scanf("%d", &a); cities.push_back(a); } for(int i = 0; i < M; ++i){ scanf("%d", &a); towers.push_back(a); } s = 0; e = INF; while(s < e){ r = (s+e)/2; bool covered = true; for(int i = 0; i < N; i++){ int low = lower_bound(towers.begin(), towers.end(), cities[i]) - towers.begin(); int high = upper_bound(towers.begin(), towers.end(), cities[i]) - towers.begin(); if(towers[low] == cities[i]) continue; if(low != 0) low--; // the one before cities[i] if(high == M) high--; if(abs(towers[low] - cities[i]) <= r || abs(towers[high] - (ll) cities[i]) <= r) continue; covered = false; break; } if(covered){ e = r; }else{ s = r+1; } } cout << s << endl; }
bool checkConstraints(vi remStrings, int n, vbs A, vi f){ sort(remStrings.begin(), remStrings.end()); vector<vi> strings; for(int i=0; i<itv.size(); i++){ vi cur = itv[i]; if(binary_search(remStrings.begin(), remStrings.end(), i)){ continue; } strings.push_back(cur); } for(int i=0; i<A.size(); i++){ if(A[i].size()==1) continue; set<vi> s; for(int j=0; j<strings.size(); j++){ vi curV; for(int k=0; k<n; k++){ if(!A[i][k]) continue; curV.push_back(strings[j][k]); } s.insert(curV); if(s.size()>f[i]) return false; } } return true; }
void compute_hull() { int pivot = 0, n = p.size(); vector<int> ps(n + 1, 0); for (int i = 1; i < n; ++i) { ps[i] = i; if (comp_lexo(p[i], p[pivot])) pivot = i; } ps[0] = ps[n] = pivot; ps[pivot] = 0; sort(ps.begin()+1, ps.end()-1, [this, &pivot](int l, int r) { return comp_angl(p[l], p[r], p[pivot]); }); h.push_back(ps[0]); size_t i = 1; ll d; while (i < ps.size()) { if (p[ps[i]] == p[h.back()]) { i++; continue; } if (h.size() < 2 || ((d = det(p[h.end()[-2]], p[h.back()], p[ps[i]])) > 0)) { // >= for col. h.push_back(ps[i]); i++; continue; } if (p[h.end()[-2]] == p[ps[i]]) { i++; continue; } h.pop_back(); if (d == 0) h.push_back(ps[i]); } if (h.size() > 1 && h.back() == pivot) h.pop_back(); }
void printGroups(vi & visited) { std::sort(visited.begin(), visited.end()); out("visited: "); for(vi::const_iterator cit = visited.begin(); cit!=visited.end();++cit){ out("%d ",*cit); } out("\n"); vi groups; vi::const_iterator pit = visited.begin(); vi::const_iterator cit = pit+1; int currcnt = 1; while(cit != visited.end()) { if(*cit == *pit) { currcnt++; } else { groups.push_back(currcnt); currcnt = 1; pit = cit; } cit++; } groups.push_back(currcnt); std::sort(groups.begin(), groups.end(), std::greater<int>()); int i = 0; for(vi::const_iterator cit = groups.begin(); cit!=groups.end() && i < 10;++cit, i++){ printf("%d ",*cit); } printf("\n"); }
int main() { FILE *in = fopen (PROBLEM_NAME".in","r"); FILE *out = fopen (PROBLEM_NAME".out","w"); int k; fscanf(in, "%d %d ", &n, &k); for (int i = 0; i < n; ++i) { used.push_back(false); tin.push_back(0); fup.push_back(0); vi l; g.push_back(l); } for (int i = 0; i < k; ++i) { int x, y; fscanf(in, "%d %d ", &x, &y); g[x-1].push_back(y-1); g[y-1].push_back(x-1); } timer = 0; dfs(0); sort(ans.begin(), ans.end()); ans.erase( unique( ans.begin(), ans.end() ), ans.end() ); fprintf(out, "%d\n", (int)ans.size()); for (size_t i = 0; i < ans.size(); ++i) { fprintf(out, "%d\n", ans[i]+1); } fclose(in); fclose(out); return 0; }
void hapusRadius(vi & Lingkaran, int inp){ vi::const_iterator a = Lingkaran.begin(); for (vi::const_iterator i = Lingkaran.begin(); i<Lingkaran.end();++i){ if (*i == inp){ Lingkaran.erase(Lingkaran.begin()+(i-a)); i=Lingkaran.end(); } } }
int main() { int temp,i; char c; cin>>n; perm.pb(0); done.pb(0); for( i=1;i<=n;i++) { cin>>temp; perm.pb(temp); done.pb(0); } for( i=1;i<=n;i++) { for(int j=1;j<=n;j++) { cin>>c; if(c=='1') mat[i][j]=1; else mat[i][j]=0; } } for(i=1;i<n;i++) { if(done[i]==0) { bfs(i); for(int j=0;j<help.size();j++) sorted.pb(perm[help[j]]); sort(help.begin(),help.end()); sort(sorted.begin(),sorted.end()); for(int j=0;j<help.size();j++) { perm[help[j]]=sorted[j]; } help.clear(); sorted.clear(); } } for(i=1;i<n;i++) cout<<perm[i]<<" "; cout<<perm[i]; return 0; }
int main() { // freopen("fence8.in", "r", stdin); // freopen("fence8.out", "w", stdout); int i,j,k; scanf("%d", &n); boards = vi(n); for(i=0; i<n; i++) scanf("%d", &boards[i]); sort(boards.begin(), boards.end()); scanf("%d", &m); rails = vi(m); for(i=0; i<m; i++) scanf("%d", &rails[i]); sort(rails.begin(), rails.end()); cummSum = vi(m); cummSum[rails.size()-1]=rails[rails.size()-1]; for(int i=rails.size()-2; i>=0; i--){ cummSum[i]=rails[i]+cummSum[i+1]; } options = vector<vvi>(n); for(int i=0; i<n; i++){ find_options(i, boards[i], vi(), 0); //sort options with more rails first vii p; for(int j=0; j<options[i].size(); j++){ p.push_back(ii(options[i][j].size(), j)); } sort(p.rbegin(), p.rend()); vvi newOptions(options[i].size()); for(int j=0; j<p.size(); j++){ newOptions[j]=options[i][p[j].second]; } options[i]=newOptions; } // sort boards with less number of options first vii p; for(int i=0; i<n; i++){ p.push_back(ii(options[i].size(), i)); } sort(p.begin(), p.end()); vector<vvi> newOptions(n); for(int i=0; i<n; i++){ newOptions[i]=options[p[i].second]; } options=newOptions; printf("%d\n", recur(0, bitset<BSM>())); return 0; }
void CariRadius(vi & Lingkaran, int inp){ while(binary_search(Lingkaran.begin(),Lingkaran.end(),inp)){ hapusRadius(Lingkaran,inp); } if (!binary_search(Lingkaran.begin(),Lingkaran.end(),inp)){ printf("Tidak ada lingkaran dengan radius %d\n",inp); } }
bool IsStonglyConnected() { V.assign(N,0); dfs(0, G1); if(find(ALL(V), 0) != V.end()) return false; // if all nodes are not reachable from a given node(say v), then return false. V.assign(N,0); dfs(0, G2); if(find(ALL(V), 0) != V.end()) return false; // if any other node cannot reach to v, then return false. return true; }
int main(){ while(scanf("%d %d %d %d", &n, &a, &b, &c) != EOF){ AdjList.assign(n + 1, vi()); stop = false; distBC = 0; for(int i = 0; i < n - 1; i++){ int u, v; scanf("%d %d", &u, &v); AdjList[u].push_back(v); AdjList[v].push_back(u); } bfs(b); sort(pathBC.begin(), pathBC.end()); int w = bfs2(a); queue<int> q; q.push(w); vi dist(n + 1, INF); dist[w] = 0; while(!q.empty()){ int u = q.front(); q.pop(); for(int j = 0; j < (int) AdjList[u].size(); j++){ int v = AdjList[u][j]; if(dist[v] == INF){ dist[v] = dist[u] + 1; q.push(v); } } } printf("%.6lf\n", 1.0 - (dist[b]*1.0)/(distBC*1.0)); } return 0; }
int findSolution() { p = vi(n, -1); int ans = 0; for (;;) { bool g = false; for (int i = 0; i < n; i++) if (ty[i] == 0 && p[i] < 0) { was = vb(n, false); chain.clear(); if (dfs(i)) { reverse(chain.begin(), chain.end()); assert(chain.size() % 2 == 0); for (int i2 = 0; i2 < chain.size(); i2 += 2) { p[chain[i2]] = chain[i2 + 1]; p[chain[i2 + 1]] = chain[i2]; } g = true; ans++; break; } } if (!g) break; } return ans; }
void doGreedy() { int cek; LL total = 0; // SOrt dlu sort(dragons.begin(), dragons.end()); sort(knights.begin(), knights.end()); REP(a,n) { cek = 0; REP(b,m) { //printf("dragon : %d \n",dragons[a]); // Ga bisa dipake lagi if(knights[b].second == 1) continue; if(knights[b].first >= dragons[a] ) { //printf("KILL by : %d\n",knights[b].first); cek = 1; total += knights[b].first; knights[b].second = 1; break; } } if(!cek) { break; } }
int main() { v.assign(101, vector<ii>()); visited.assign(101, 0); while(true) { topo.clear(); for(i=0;i<101;++i) visited[i]=0; scanf("%d %d", &n, &m); if(!n && !m) break; for(i=1;i<=n;++i) v[i].clear(); while(m--) { scanf("%d %d", &x, &y); v[x].push_back(make_pair(y, 1)); } for(i=1;i<=n;++i) { if(!visited[i]) dfs(i); } reverse(topo.begin(), topo.end()); for(i=0;i<n;++i) { if(i>0) putchar(' '); printf("%d", topo[i]); } putchar('\n'); } return 0; }
void convex_hull(vector<Point> & a) { if (a.size() == 1) return; sort(a.begin(), a.end(), &cmp); Point p1 = a[0], p2 = a.back(); vector<Point> up, down; up1.clear(); down1.clear(); up.push_back(p1); up1.pb(0); down.push_back(p1); down1.pb(0); for (size_t i = 1; i < a.size(); ++i) { if (i == a.size() - 1 || ccw(p1, a[i], p2)) { while (up.size() >= 2 && !ccw(up[up.size() - 2], up[up.size() - 1], a[i])) up.pop_back(),up1.pop_back(); up.push_back(a[i]),up1.push_back(i); } if (i == a.size() - 1 || cw(p1, a[i], p2)) { while (down.size() >= 2 && !cw(down[down.size() - 2], down[down.size() - 1], a[i])) down.pop_back(),down1.pop_back(); down.push_back(a[i]),down1.push_back(i); } } if (up[0].x == up.back().x && up[0].y == up.back().y) up.erase(up.end() - 1),up1.erase(up1.end()-1); a.clear();a1.clear(); for (size_t i = 0; i < up.size(); ++i) a.push_back(up[i]),a1.push_back(up1[i]); for (size_t i = down.size() - 2; i > 0; --i) a.push_back(down[i]),a1.push_back(down1[i]); }
void sol(){ if(s2<s1){ vi t=a;a=b;b=t; ll x=s1;s1=s2;s2=x; t=ida;ida=idb;idb=t; fliped=!fliped; } if(a.empty() || b.empty())return ; if(abs(s1-s2)==0){return ;} ll diff = abs(s2 - s1); ll Y = diff; int x,y; x=y=0; // printf("diff = %lld\n",diff); for (int i = 0; i < a.size(); ++i) { vi::iterator it = lower_bound(b.begin(), b.end(), diff/2 + a[i]); int j = it-b.begin(); if(j && j == b.size())j--; // printf("i,j=%d,%d\n",i,j); ll ns1 = s1 - a[i] + b[j]; ll ns2 = s2 - b[j] + a[i]; // printf("debug a[%d] = %d b[%d] = %d\n",i,a[i],j,b[j]); // printf("debug ns1 = %lld ns2 = %lld\n",ns1,ns2); if(abs(ns1 - ns2) < Y){ x = i; y = j; // printf("debug %d %d\n",x,y); Y = abs(ns1 - ns2); } } if(Y<diff){ // printf("swaping a[%d] b[%d] diff = %lld\n", x,y, Y); if(fliped) ans.push_back(ii(idb[y],ida[x])); else ans.push_back(ii(ida[x],idb[y])); s1 = s1 - a[x] + b[y]; s2 = s2 + a[x] - b[y]; } // printf("x,y = %d %d\n",x,y); a.erase(move(a,x)); ida.erase(move(ida,x)); b.erase(move(b,y)); idb.erase(move(idb,y)); }
vi prime_factorise( int a ){ vector< int >::iterator it; vector< int > result; for(it = primes.begin();it!=primes.end() && a>1 && (*it) * (*it) <= a ; it++ ){ int count = 0; while( a%(*it) == 0 ) (count++, a /= *it); result.push_back(count); } if(a > 1){ int index = lower_bound(primes.begin(),primes.end(),a) - primes.begin(); result.resize(index+1); result[index] = 1; } return result; }
forn(i, n) { int pos = upper_bound(values.begin(), values.end(), param1[i]) - values.begin(); if (typ[i] == 0) { add(1, 0, list.size(), 0, pos, -1); } else add(1, 0, list.size(), 0, pos, +1); int ans = get(1, 0, (int)list.size()); printf("%d\n", ans == -1 ? -1 : list[ans].se); }
void assertsorted(vi sorted ){ int i = 0; for(vi::iterator it=sorted.begin(); it!=sorted.end(); ++it){ assert(i <= *it); i = *it; } }
inline void addIfPrime(vi & primes, int n) { for(vi::iterator it = primes.begin(); (*it)*(*it) <= n && it != primes.end(); it++) { int fract = n / (*it); if(fract * (*it) == n ) return; } primes.push_back(n); }
vector<double> expectedFinishTimes( vi t, vs usr ) { int n = t.size(); vector<double> ans(t.begin(),t.end()); rep(i,n) utot[usr[i]] += t[i]; rep(i,n) rep(j,n) if(utot[usr[i]]>utot[usr[j]]) ans[i] += t[j]; rep(i,n) rep(j,n) if(i!=j&&utot[usr[i]]==utot[usr[j]]) ans[i] += 0.5*t[j]; return ans; }
vector<vector<int> > combinationSum(vector<int> &_candidates, int _target) { candidate = _candidates; target = _target; n = candidate.size(); sort(candidate.begin(), candidate.end()); vi path; dfs(0, path, target); return ans; }
int main(){ ios_base::sync_with_stdio(false); int n, m, dummy; cin >> n >> m; while( n!= 0 || m != 0){ int l = -1; long long sum = 0; dragons.clear(); knights.clear(); for(int i = 0; i < n; i++){ cin >> dummy; dragons.push_back(dummy); } for(int i = 0; i < m; i++){ cin >> dummy; knights.push_back(dummy); } if(n > m){ cout << "Loowater is doomed!\n"; goto fim; } sort(dragons.begin(), dragons.end()); sort(knights.begin(), knights.end()); for(int i = 0; i < n; i++){ bool hasIt = false; for(int j = l+1; j < m; j++){ if(knights[j] >= dragons[i]){ hasIt = true; sum += knights[j]; l = j; break; } } if(!hasIt){ cout << "Loowater is doomed!\n"; goto fim; } } cout << sum << '\n'; fim: cin >> n >> m; } }
//---------------------------------------------------------------------------- // Return strue if a given vector<long> contains given long //---------------------------------------------------------------------------- bool containsElem(vi list, long value) { for (vi::iterator it = list.begin(); it != list.end(); it++) { if (*it == value) { return true; } } return false; }
int main() { s(N) ; s(K) ; pos.resize(N); rep(0,N-1,i) s(pos[i]) ; sort(pos.begin() , pos.end()); p(solve(0,100000000)); return 0; }
void KelilingCircle(Circle circle, vi &Lingkaran){ printf("Radius\t\tKeliling\n"); for (vi::const_iterator i = Lingkaran.begin(); i!=Lingkaran.end();++i){ int radius = *i; circle.setPi(); circle.setRadius(radius); double hasil = circle.keliling_circle(); printf("%d\t\t%.3f\n",radius,hasil); } }
int counter(vi arr, int l) { int cont = 0; sort(arr.begin(), arr.end()); for(int i=arr.size() - 1; i>=0; i--){ if(arr[i] < l) break; cont++; } return cont; }
int main(){ int n, m, d; ll a, b, c; cin >> n >> m >> d; v.clear(); while (m--) { cin >> a >> b; if (!adj.count(a)) { adj[a] = vi(); v[a] = 0; } if (!adj.count(b)) { adj[b] = vi(); v[b] = 0; } adj[a].push_back(b); } v.clear(); for (map<long, vi>::iterator it = adj.begin(); it != adj.end(); it++) { if (!v[it->first]) dfs(it->first); } reverse(rank1.begin(), rank1.end()); map<long, long> db; vi visited(n, 0); for (int i = 0; i < rank1.size(); i++) db[rank1[i]] = i; vi wrong; int cnt = 0; int dup = 0; while (n--) { cin >> c; if (!db.count(c)) { cnt += 2; } else { if (visited[db[c]]) dup++; wrong.push_back(c); visited[db[c]] = 1; } } vi wrank; for (int i = 0; i < wrong.size(); i++) { wrank.push_back(db[wrong[i]]); } int l = lis(wrank); cnt += (wrank.size()-l) * 2; cout << cnt << endl; return 0; }
vi merge(vi &a, vi &b){ vi c; merge(a.begin(),a.end(),b.begin(),b.end(),back_inserter(c), cmp); vi res; clear(c); for(int i = c.size() - 1 ; i >= 0 ; i--){ int v = V[c[i]]; int u = U[c[i]]; int x = join(c[i]); if(x) res.push_back(c[i]); if(x == 2){ // has odd cycles hasOddCycle = true; break; } } reverse(res.begin(), res.end()); return res; }
int solver(int n, int m, int ll, vi &v) { // A largura da mesa (em centímetros) tem que ser um múltiplo da largura da // tábua, caso contrário não consigo botar as tábuas uma do lado da outra. if ((n * 100) % ll != 0) return IMPOSSIBLE; // n representava uma largura absoluta. Agora n vai ser a largura relativa // à largura de uma tábua. É uma mudança de variável que torna a largura da // tábua igual a 1, de modo que eu vou querer n tábuas. n = (n * 100 / ll); // Primeiro passo: Contar a quantidade de tábuas que, sozinhas, já preenchem // uma unidade de largura do salão. int woods = count(v.begin(), v.end(), m); if (woods >= n) return n; // Segundo passo: Encontrar n - woods pares que, somados, dão m. sort(v.begin(), v.end()); n = n - woods; int l = 0; int r = lower_bound(v.begin(), v.end(), m) - v.begin() - 1; while (n > 0 and l < r) { if (v[l] + v[r] < m) { l++; } else if (v[l] + v[r] > m) { r--; } else { l++; r--; n--; woods += 2; } } if (n == 0) return woods; else return IMPOSSIBLE; }