bool coin_change(int k, int left_pane, int right_pane, int taken, bool flag, vi panes)
{
    int i;

    if(taken==m)
    {
        outs=panes;
        return 1;
    }

    for(i=1; i<=10; i++)
    {
        if(i==k) continue;
        if(!flag && coins[i] && left_pane+i>right_pane)
        {
            panes.pb(i);
            if(coin_change(i,left_pane+i,right_pane,taken+1,!flag,panes)) return 1;
            panes.pop_back();
        }
        if(flag && coins[i] && right_pane+i>left_pane)
        {
            panes.pb(i);
            if(coin_change(i,left_pane,right_pane+i,taken+1,!flag,panes)) return 1;
            panes.pop_back();
        }
    }
    return ret;
}
Example #2
0
int main() 
{
    int T;
    sf("%d", &T);
    while (T--)
    {
        sf("%d%d%d", &n, &s, &w);
        int g = s;
        REP(i, n)
        {
            a[i] = g;
            if (a[i] == 0) a[i] = g = w;
            if (g%2 == 0) g /= 2;
            else g = (g/2) ^ w;
        }
        //REP(i, n) cout <<a[i] <<" ";
        //cout <<endl;
        st = 0;
        //REP(i, n) st ^= a[i];
        b.clear();
        b.pb(0);
        REP(i, n)
        {
            st ^= a[i];
            b.pb(st);
        }
int main () {
    #ifndef ONLINE_JUDGE
    freopen(INP, "r", stdin); freopen(OUT, "w", stdout);
    #endif
    
    scanf("%d", &n);
    order.clear();
    arr1.assign(n+1,0);
    a1.clear();
    FOR(i,1,n) scanf("%d", &a), a1.pb(a);
    REP(i,n) arr1[a1[i]-1] = i+1;
    REP(i,n) order[arr1[i]] = i;
    while(scanf("%d", &a) != EOF){
        arr.assign(n+1,0);
        a2.clear();
        a2.pb(a);
        ans.resize(0);
        len = 0;
        FOR(i,1,n-1) scanf("%d", &a), a2.pb(a);
        REP(i,n) arr[a2[i]-1] = i+1;
        REP(i,n) updateLis(arr[i]);   
        cout << len << endl; 
    }

    return 0;
}
Example #4
0
int TheArray::find (int _n, int _d, int _first, int _last) 
{
	arr.clear();
	d = abs(_d);
	n = _n;
	first = _first;
	last = _last;
	if(first > last)
		swap(first, last);

	arr.pb(first);

	while(arr.size() < n){
		lft = n - arr.size();
		if(arr.back() + 0 - d*(lft-1) <= last)
			arr.pb(arr.back() + find1(0, d));
		else
			arr.pb(max(arr.back()-d, last));
	}
/*
	tr(it, arr)
		write(*it);
	cout << endl;
*/

	return *max_element(all(arr));
}
// elfekra kolha en el diff yeb2a minimum 3la ad ma2dar f ba7wel ene a generate every possible diff
// w homa 2 cases ema abd2a b 0 aw b 1 fagrab kolo w a5tar ela7san 
// GET betgeb law habda2 b 0 EX :- 011010010 while ene ma7afz 3 eltarteb
// GET1 betgeb law habda2 b 1 
int GET(){
 
  int it1 = 1, it2 = 1 , i = 1;
  int st = 0 ; 
  while(it1 <= n && it2 <=m){
    while(A[it1]==st && it1<=n){
      seq.pb(it1);
      H[0][i++]=A[it1++];
    }
    while(B[it2]==st && it2<=m){
      seq.pb(it2+n);
      H[0][i++]=B[it2++];
    }
    st = 1 - st ;
 
  }
  while(it1<=n){ seq.pb(it1) ; H[0][i++]=A[it1++]; } 
  while(it2<=m){ seq.pb(it2+n) ; H[0][i++]=B[it2++]; }
  int dif = 0;
 
  forn(i , 2 , n+m+1)
  if(H[0][i]!=H[0][i-1]){
       dif++;
       // saved me from many TLE 
       ans[0].pb(i-1);
  }
  last[0]=(H[0][n+m]==0 ); 
  if(H[0][n+m] ){ dif++;ans[0].pb(n+m);}
return dif;
 
}
void sieve()
{
    for(int i = 0 ; i <= maxN ; i++)
        check[i] = true;
    
    for(ll i = 2 ; i <= maxN ; i++)
    {
        if(check[i])
        {
            primes.pb(i);
            for(ll j = i ; i*j <= maxN ; j++)
                check[i*j] = false;
        }
    }
    a.pb(2);
    rep(i,sz(primes))
    {
        int k = primes[i];
        bool ok = true;
        while(k)
        {
            if(k%10 == 0 || (k%10)%2 == 0)
            {
                ok = false;
                break;
            }
            k /= 10;
        }
        
        if(ok) a.pb(primes[i]);
    }
Example #7
0
int main(){
    int t, n, i, c;
    scanf("%d", &t);
    kase=1;
    while(t--){
        scanf("%s", str);
        n = strlen(str);
        grps.clear();
        type.clear();
        for(i=0; i<n; ){
            if(str[i]=='a'){
                c=0;
                while(i<n && str[i]=='a') c++, i++;
                grps.pb(c);
                type.pb(0);
            }else{
                c=0;
                while(i<n && str[i]=='b') c++, i++;
                grps.pb(c);
                type.pb(1);
            }
        }

        done = (1<<grps.size())-1;
        printf("%d\n", recur(0));
        kase++;
    }
    return 0;
}
Example #8
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]);
}
int GET2(){
 
  int it1 = 1, it2 = 1 , i = 1;
  int st = 1 ; 
  while(it1 <= n && it2 <=m){
    while(A[it1]==st && it1<=n){
      seq1.pb(it1);
      H[1][i++]=A[it1++];
    }
    while(B[it2]==st && it2<=m){
      seq1.pb(it2+n);
      H[1][i++]=B[it2++];
    }
    st = 1 - st ;
 
  }
  while(it1<=n){ seq1.pb(it1) ; H[1][i++]=A[it1++]; } 
  while(it2<=m){ seq1.pb(it2+n) ; H[1][i++]=B[it2++]; }
  int dif = 0;
 
  forn(i , 2 , n+m+1)
  if(H[1][i]!=H[1][i-1]){
       dif++;
       ans[1].pb(i-1);
  }
  if(H[1][n+m] ){dif++;ans[1].pb(n+m);}
  last[1]=(H[1][n+m]==0); 
return dif;
 
}
// Prima pertama terdapat di primes[1] yah :D
void sieve(LL upperbound){
	bs.set();
	bs[0]=bs[1]=0;
	primes.pb(0);
	for(LL i=2; i<=upperbound+1; i++)if(bs[i]){
	for(LL j=i*i; j<= upperbound+1; j+=i)bs[j] = 0;
			primes.pb((int) i);
	}
}
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;
}
void extract()
{
	for(i,0,sz){
		if(isv(word[i])) {
			vogais.pb(i);
			onde[i] = vogais.size()-1; 
		}else{
			consoantes.pb(i);
			onde[i] = consoantes.size()-1;
		}
	}
}
Example #13
0
void solve() {
	arr.clear();
	p.clear();
/*
	cin >> K >> N;
	arr = vi(K);
	forn(i, K)
		cin >> arr[i];
*/
	cin >> N >> K;
	int64 b, c, r;
	arr = vi(K);
	cin >> arr[0] >> b >> c >> r;

	forab(i, 1, K-1)
		arr[i] = (arr[i-1]*b + c) % r;
	forn(i, K)
		write(arr[i]);
	cout << endl;

	sort(all(arr));
	uni.pb(arr.front());
	p.pb(mp(arr[0], arr[0]));
	
	forn(i, K) {
		if(arr[i] != uni.back())
			uni.pb(arr[i]);
		if(arr[i] == p.back().second)
			;
		else if(arr[i] == p.back().second+1)
			p.back().second++;
		else
			p.pb(mp(arr[i], arr[i]));

	}

	forn(i, K)
		write(arr[i]);
	cout << endl;
	forn(i, uni.size())
		write(uni[i]);
	cout << endl;

	
	tr(it, p)
		Pf("(%d, %d) ", it->first, it->second);
	cout << endl;

	int M = N;
	forab(i, K, N) {
		N = i;
		Pf("arr[%d] = %d\n", N, binarySearch());
	}
void sieve(int64 n){
	int i, j;
	bs.set();
	bs[0] = bs[1] = 0;
	primes.pb(1);
	for(i=2; i<=n; i++){
		if(bs[i]){
			primes.pb(i);
			if(i*1ll*i<=n)
				for(j=i*i; j<=n; j+=i){bs[j] = 0;}
		}
	}
}
Example #15
0
void primeFill(int upBound){
  vector <bool> row(upBound,1);
  int del;                              //del += 2 
  for( del= 2; del*del <= upBound; ++del){
    if(row[del]){
      prime.pb(del);
      for(int i = del*del; i <= upBound; i+=del)
        row[i] = false;
    }
  }
  for(;del < upBound; ++del)
    if(row[del]) 
      prime.pb(del);
}
int main(){
	pre();
	cin >> u >> v;
	loop(i,len(v)){
		if(v[i] == '0') zero.pb(i);
		else one.pb(i);
	}
	ll ans = 0;
	loop(i,len(u)){
		vi *V;
		if(u[i] == '0') V = & one;
		else V = & zero;
		ans += upper_bound(V->begin(),V->end(),len(v) - len(u) + i) - lower_bound(V->begin(),V->end(),i);
	}
void sive()
{
    int i,j,k,r,c;
    flag[0]=flag[1]=1;
    prime.pb(2);
    for(i=3;i<MAX;i+=2)
    {
        if(!flag[i])
        {
            prime.pb(i);
            r=i*2;
            if(i<MAX/i)for(j=i*i;j<MAX;j+=r) flag[j]=1;
        }
    }
}
void buildSet(int nd, int i, int value, vi &s) {
	s.pb(value);
	if (i >= nd) return;
	buildSet(nd, i + 1, value + 0, s);
	buildSet(nd, i + 1, value + a[i], s);
	buildSet(nd, i + 1, value + 2 * a[i], s);
}
Example #19
0
File: E.cpp Project: Igorjan94/CF
void run()
{
    ints(n);
    int x, y;
    map<int, int> m, index;
    fori(n)
    {
        readln(x, y);
        if (m.find(x) == m.end())
            m[x] = x;
        if (m.find(y) == m.end())
            m[y] = y;
        swap(m[x], m[y]);
    }
    int counter = 0;
    for (auto i : m)
        temp.pb(i.second),
        index[i.second] = counter++;
    int count = temp.size();
    a.resize(count);
    b.resize(count);
    inverses(0, count);
    ll ans = 0;
    int i = 0;
    for (auto t : b)
        ans += t.second + abs(t.first - m[t.first]) - abs(i++ - index[t.first]);
    writeln(ans);
}
void dfs( int u ){
    seen[u] = 1;
    for( int i = 0; i < adjList[u].size(); ++i )
        if( !seen[adjList[u][i]] )
            dfs( adjList[u][i] );
    topoSort.pb( u );
}
vector<ll> bfs(int start) {
	vector<ll> d(n+1, INF);
	RI(i, n) vis[i] = false;
	d[start] = 0;
	set<pair<ll,int> > s;
	RI(i, n) s.insert(mp(d[i], i));
	while(!s.empty()) {
		int me = (*s.begin()).nd;
		order.pb(me);
		vis[me] = true;
		s.erase(s.begin());
		for(int ed : w[me]) {
			int he = e[ed].b;
			if(vis[he]) continue;
			s.erase(mp(d[he], he));
			ll maybe = d[me] + e[ed].d;
			if(maybe < d[he]) {
				pre[he].clear();
				pre[he].pb(ed);
			}
			else if(maybe == d[he]) pre[he].pb(ed);
			mini(d[he], maybe);
			s.insert(mp(d[he], he));
		}
	}
	return d;
}
void dekho(int a,int b)
{
    if(b==0)
        return;
    num.pb(a/b);
    dekho(b,a%b);
}
Example #23
0
void put(int p) {
    for (int pi : pos) {
        int d = abs(pi-p);
        cnt[d]--;
    }
    pos.pb(p);
}
Example #24
0
int main()
{
    int pos,i,j,k,tot,sum;
    string x,y;
    essay dhur,bal;
    fast;
    cin>>m;
    pos=0;
    for(i=1; i<=m; i++)
    {
        cin>>x;
        dhur=reform(x);
        if(mp[dhur.str]==0)
        {
            pos++;
            mp[dhur.str]=pos;
            all_nodes.pb(dhur);
        }
        j=mp[dhur.str];
        nodes.pb(j);
    }
    cin>>n;
    for(i=0; i<n; i++)
    {
        cin>>x>>y;
        dhur=reform(x);
        bal=reform(y);
        if(mp[dhur.str]==0)
        {
            pos++;
            mp[dhur.str]=pos;
            all_nodes.pb(dhur);
        }
        if(mp[bal.str]==0)
        {
            pos++;
            mp[bal.str]=pos;
            all_nodes.pb(bal);
        }
        j=mp[dhur.str];
        k=mp[bal.str];
        edges[j].pb(k);
    }
    for(i=0; i<m; i++)
    {
        j=nodes[i];
        if(vis[j]==0) nodes[i]=dfs(j);
        else nodes[i]=vis[j];
    }
    tot=sum=0;
    for(i=0; i<m; i++)
    {
        j=nodes[i];
        tot+=all_nodes[j-1].cntr;
        sum+=all_nodes[j-1].str_len;
    }
    pr2(tot,sum);
    return 0;
}
Example #25
0
		void pre(vi a,int pos) {
			for(int i=0;i<a.size();i++) {
				if(!val[a[i]])
					arr.pb(a[i]);
				val[a[i]]++;
				adj[a[i]].pb(pos);
			}
		}
Example #26
0
int main(){
    std::ios::sync_with_stdio(false);
    cin >> n >> k;
    f (n, i) {
        ll aux;
        cin >> aux;
        needs.pb (aux);
    }
Example #27
0
int main() {
	var(int);
	sc("%d",&T);
	vec.pb(0);
	while(T--) {
		sc("%d",&tmp);
		vec.pb(tmp);
	}
	init_seg(vec.size());
	build_seg(1,1,vec.size());
	sc("%d",&N);
	while(N--) {
		sc("%d %d",&tmp1,&tmp2);
		pf("%d\n",query(1,1,vec.size(),tmp1,tmp2).bs);
	}
	return 0;
}
int main()
{
    int i,a,b;
    cin>>n>>a>>b;
    store[0].idx=0;
    store[0].width=a;
    store[0].height=b;
    k=0;
    for(i=1; i<=n; i++)
    {
        iin(a);
        iin(b);
        if(a>store[0].width && b>store[0].height)
        {
            k++;
            store[k].idx=i;
            store[k].width=a;
            store[k].height=b;
        }
    }
    sort(store+1,store+k+1);
    setneg(dp);
    setneg(dir);
    a=0;
    b=0;
    for(i=1; i<=k; i++)
    {
        if(lis(i)>a)
        {
            a=lis(i);
            b=i;
        }
    }
    i=b;
    while(dir[i]!=-1)
    {
        outs.pb(store[i].idx);
        i=dir[i];
    }
    outs.pb(store[i].idx);
    pr1(a);
    for(i=0; i<a; i++) printf("%d ",outs[i]);
    nl;
    return 0;
}
void sieve(int64 n){
	bs.set();
	bs[0] = bs [1] = 0;
	for(int64 i=2; i<=n; i++)
		if(bs[i]){
			for(int64 j=i*i; j<=n; j+=i) bs[j]=0;
			primes.pb((int)i);
		}
}
Example #30
0
bool dfs(int u) {
  if(vis[u]) return false;
  vis[u] = 1;

  if(u == 1) {
    ans.pb(u);
    return true;
  }

  for(int v:adj[u]) {
    if(nex[u] == v) continue;
    if(dfs(v)) {
      ans.pb(u);
      return true;
    }
  }
  return false;
}