void expand(int x,vi & V,ll & ans){
	ll p = 1; int idx = 0;
	while(idx < sz(V)){
		int s = V[idx],nxt = 1 << 20;
		if(M.find(x - 1) != M.end()) {
			auto p = lower_bound(all(M[x - 1]),s);
			if(p != M[x - 1].end()) nxt = *p;
		}
		if(M.find(x + 1) != M.end()) {
			auto p = lower_bound(all(M[x + 1]),s);
			if(p != M[x + 1].end()) nxt = min(nxt,*p);
		}
		if(nxt == (1 << 20)){
			ans += p * (sz(V) - 1);
			return;
		}
		int nidx = lower_bound(all(V),nxt) - V.begin();
		if(nidx == sz(V)) break;
		ll y = 0;
		if(M.find(x - 1) != M.end()) {
			y += lower_bound(all(M[x - 1]),V[nidx]) - V.begin();
			y -= lower_bound(all(M[x - 1]),V[idx]) - V.begin();
		}
		if(M.find(x + 1) != M.end()) {
			y += lower_bound(all(M[x + 1]),V[nidx]) - V.begin();
			y -= lower_bound(all(M[x + 1]),V[idx]) - V.begin();
		}		
		p *= (nidx -0LL- idx) * p;
		idx = nidx;
	}
	ans += p;
}
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;
}
Exemple #3
0
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;
}
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");

}
vi id ( vi state ) {

  //--- Phase 1: Edge orientations.
  if( phase < 2 )
    return vi( state.begin() + 20, state.begin() + 32 );

  //-- Phase 2: Corner orientations, E slice edges.
  if( phase < 3 ){
    vi result( state.begin() + 31, state.begin() + 40 );
    for( int e=0; e<12; e++ )
      result[0] |= (state[e] / 8) << e;
    return result;
  }

  //--- Phase 3: Edge slices M and S, corner tetrads, overall parity.
  if( phase < 4 ){
    vi result( 3 );
    for( int e=0; e<12; e++ )
      result[0] |= ((state[e] > 7) ? 2 : (state[e] & 1)) << (2*e);
    for( int c=0; c<8; c++ )
      result[1] |= ((state[c+12]-12) & 5) << (3*c);
    for( int i=12; i<20; i++ )
      for( int j=i+1; j<20; j++ )
    result[2] ^= state[i] > state[j];
    return result;
  }

  //--- Phase 4: The rest.
  return state;
}
void merge(vi &v,int l, int p, int h)
{
  int i,r,j,k=l,m=p-l+1,n=h-p;
  vi L(v.begin()+l,v.begin()+p+1),R(v.begin()+p+1,v.begin()+h+1);

  for(l=0,r=0;l<m&&r<n;k++){
    if(L[l]<R[r]||L[l]==R[r]){
      v[k]=L[l];
      ++l;
    }
    else{
      invcnt+=m-l;
      v[k]=R[r];
      ++r;
    }
  }

  
  if(l==m&&r!=n){
    for(i=r;i<n&&k<h+1;i++,k++)
      v[k]=R[i];
  }
  if(l!=m&&r==n){
    for(i=l;i<m&&k<h+1;i++,k++){
      v[k]=L[i];
    }
  }
}
Exemple #7
0
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;
}
Exemple #8
0
	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));
	}
Exemple #9
0
 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);
 }
Exemple #10
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();
		}
	}
}
Exemple #11
0
 REP(i, n)
 {
     st ^= a[i];
     ans += i+1;
     if (st == 0) ans--;
     p = lower_bound(b.begin(), en, st) - b.begin();
     ans -= t[p];
     t[p]++;
 }
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;
}
Exemple #13
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;
}
Exemple #14
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);
	}

}
Exemple #15
0
int main() {
    int q;
    cin >> n >> m >> q;
    scanf("%d%d%d", &n, &m, &q);
    scanf("%s%s", T, P);
    kmpPreprocess(); kmpSearch();
    forn(i, q) {
        int l, r; cin >> l >> r;
        int ind1 = upper_bound(all.begin(), all.end(), l - 2) - all.begin();
        int ans = 0;
        for(int k = ind1; k < all.size(); k++)
            if(all[k] + m <= r) ans++;
        printf("%d\n", ans);
    }
int getPairs(vi & primes, int n)
{
    int count = 0;
    vi::iterator jt = std::upper_bound(primes.begin(), primes.end(), n);
    //printf("lower bound find %d is %d\n", n, *jt);
    for(vi::iterator it = primes.begin(); (*it) <= n && it != jt; it++)
    {
        int d = n - (*it);
        if(std::binary_search(it, jt, d)) {
            count++;
            //printf("%d + %d = %d\n", *it, n - (*it), n);
        }
    }
    return count;
}
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;
}
Exemple #18
0
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 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;
 		}
 	}
pair<int,int> complete_search(int nb){
    int first=0,last=primes.size()-1;
    last=lower_bound(primes.begin(),primes.end(),nb)-primes.begin();
    for(int i=last;i>0;i--){
        if(primes[i]<nb){
            first=i;
            for(int j=1;j<=first;j++){
                if(primes[j]+primes[i]==nb)
                    return {primes[j],primes[i]};
                else if(primes[j]+primes[i]>nb)
                    break;
            }
        }
    }
    return {-1,-1};
}
vi mergeSort(const vi &data) {
	if (data.size() <= 1)
		return data;

	int middle = data.size() / 2;
	vi left(data.begin(), data.begin() + middle);
	vi right(data.begin() + middle, data.end());

	left = mergeSort(left);
	right = mergeSort(right);

	vi result(data.size());
	merge(ALL(left), ALL(right), result.begin());

	return result;
}
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;
}
Exemple #23
0
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;
}
Exemple #24
0
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;
	}
Exemple #27
0
 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;
 }
Exemple #28
0
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;
	}
}
Exemple #29
0
//----------------------------------------------------------------------------
// 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;
}