Example #1
0
void process(void)
{
	int n,m;
	multiset<int> S;
	scanf("%d %d",&n,&m);
	data.resize(n);
	dyna.resize(n,-1);
	for(int i=0;i<n;i++)
	{
		scanf("%d",&data[i]);
	}
	for(int i=0;i<m;i++)
	{
		dyna[i] = data[i];
		S.insert(dyna[i]);
	}
	for(int i=m;i<n;i++)
	{
		dyna[i] = *S.begin() + data[i];
		S.erase(S.find(dyna[i-m]));
		S.insert(dyna[i]);
	}

	int mind = -1;
	for(int i=n-m;i<n;i++)
	{
		if(mind < 0 || mind > dyna[i]) mind = dyna[i];
	}
	cout << mind << endl;
}
Example #2
0
VI MULTI(VI X, VI Y)
{
	int N = X.size(), N2 = N / 2;
	VI R(N * 2, 0);
​
	if (N > 100) {
		VI X2(X.begin() + N2, X.end());
		VI Y2(Y.begin() + N2, Y.end());
		X.resize(N2); Y.resize(N2);
​
		VI A = MULTI(X, Y);
		VI B = MULTI(X2, Y2);
		for (int i = 0; i < N2; i++) X2[i] += X[i], Y2[i] += Y[i];
		VI C = MULTI(X2, Y2);
​
		for (int i = 0; i < A.size(); i++) R[i] += A[i], C[i] -= A[i];
		for (int i = 0; i < B.size(); i++) R[i + N2 * 2] += B[i], C[i] -= B[i];
		for (int i = 0; i < C.size(); i++) R[i + N2] += C[i];
	}
	else {
		for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) R[i + j] += X[i] * Y[j];
	}
​
	return R;
}
int main() {

    cur.resize(N);
    genComb(0, -1);
    getAns();
    cout<<y<<endl;
    return 0;
}
Example #4
0
int main(void) {
    scanf("%d", &n);
    a.resize(n);
    int dif = 0;
    REP(i, n) {
        scanf("%d", &a[i]);
        if (i != 0) dif = max(dif, abs(a[i] - a[i - 1]));
    }
Example #5
0
 BoyerMoore(string pattern, int R = 256):_R(R),_pattern(pattern){
     _right.resize(R);
     for(VI::iterator i = _right.begin(); i < _right.end(); i++){
         *i = -1;
     }
     for(unsigned int j = 0; j < _pattern.size(); j++){
         _right[(int)(pattern[j])] = j;
     }
 }
Example #6
0
void init()
{
    scanf("%d %d",&N,&M);
    perm.resize(N);
    check.resize(N);
    for(int i=0;i<N;i++)
    {
        scanf("%d",&perm[i]);
        check[i]=perm[i];
    }
    for(int i=0;i<M;i++)
    {
        scanf("%d %d",&ind[i][0],&ind[i][1]);
        ind[i][0]--; ind[i][1]--;
    }
    F[0]=1;
    for(int i=1;i<N;i++) F[i]=F[i-1]*i;
}
Example #7
0
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    int counter=0,n,i,j,l;
    VI vis;
    string str,str1,str2;
    vector <string> v;
    cin >>n;
    for(i=0;i<n;i++)
    {
        cin >>str;
        v.PB(str);
        str1=getdomain(str);
        str2=getlogin(str,str1);
        mp[MP(str2,str1)].PB(i);
    }
    VI v1;
    vis.resize(n,0);
    for(i=0;i<n;i++)
    {
        if(!vis[i])
        {
            counter++;
            str=v[i];
            str1=getdomain(str);
            str2=getlogin(str,str1);
            v1=mp[MP(str2,str1)];
            l=v1.size();
            for(j=0;j<l;j++)
                vis[v1[j]]=1;
        }
    }   
    cout <<counter<<"\n";
    vis.assign(n,0);
    for(i=0;i<n;i++)
    {
        if(!vis[i])
        {
            str=v[i];
            str1=getdomain(str);
            str2=getlogin(str,str1);
            v1=mp[MP(str2,str1)];
            l=v1.size();
            cout <<l;
            for(j=0;j<l;j++)
            {
                vis[v1[j]]=1;
                cout <<" "<<v[v1[j]];
            }
            cout <<"\n";
        }
    }   
    return 0;
}
Example #8
0
  int getcost(vector<int> A, vector<int> B, vector<int> C, int K) {
    this->A=A,this->B=B,this->C=C;
    this->N=SZ(A);
    
    int a=f(A,1,K),b=f(B,1,K);
    AB.resize(N);
    REP(i,N) AB[i]+=A[i]+B[i];
    int c=f(AB,0,2*K-1);
    
//    dump3(a,b,c);
    int res=min({a,b,c});
    return res==Inf?-1:res;
  }
Example #9
0
void initFlow(int _T)
{
	T= _T;
	cost.resize(T);
	cap.resize(T);
	for(int i=0;i<T;i++)
	{
		cost[i].resize(T);
		cap[i].resize(T);
	}
	deg.resize(T);
	adjList.resize(T);
}
Example #10
0
int main()
{
    FastIO(); 
    VI v;
    cin >>n;
    v.resize(n+1);
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n;j++)
            cin >>arr[i][j];
    }
    while(counter<n)
    {
        x=computemin();
        for(i=1;i<=n;i++)
        {
            f=1;
            if(check[i]==0)
            {
                for(j=1;j<=n;j++)
                {
                    if(j==i)
                        continue;
                    if(check[j]==1)
                        continue;
                    if(arr[i][j]!=x)
                    {
                        f=0;
                        break;
                    }
                }
                if(f==1)
                {
                    counter++;
                    v[i]=x;
                    check[i]=1;
                    break;
                }
            }
        }
    }
    for(i=1;i<=n;i++)
        cout <<v[i]<<" ";
    cout <<"\n";
    return 0;
}
void DFS(){
    visited.resize(k,0);
    REP(v,k) if (!visited[v]){
        visited[v] = 1;
        printf("%d 0\n",v+1);
        stack.PB(v);
        while (!(stack.empty())){
            int u = stack.back();
            stack.pop_back();
            for(auto ngb : sol[u])
                if (!visited[ngb]){
                    visited[ngb] = 1;
                    printf("%d %d\n",ngb+1,u+1);
                    stack.PB(ngb);
                }
        }    
    }
}
Example #12
0
VI decode(int num)
{
    int used[MAXN];
    memset(used,0,sizeof(used));
    VI ret;
    ret.resize(N);
    int mul;
    for(int i=0;i<N;i++)
    {
        mul=num/F[N-i-1]+1;
        num%=F[N-i-1];
        for(int j=1;j<=N;j++)
            if(!used[j])
            {
                mul--;
                if(!mul) { ret[i]=j; used[j]=1; break; }
            }
    }
    return ret;
}
int main() {    
    int n; 
    cin >> n;
    
    // obavezno resizeamo vektor! Jer smo tako predvidjeli rad procedure "unesi"
    VI v;
    v.resize(n);
    
    // popuni vektor ulazima (ocekujemo n brojeva na ulazu)
    unesi(v);
    
    // napravi novi "duplirani" vektor
    VI out = dupliraj(v);
    
    // ispisi duplirani vektor
    ispisi(out);
    
    // ispisi originalni vektor
    ispisi(v);
    
    // system("pause");
    return 0;
}
VI iterativeMergeSort(VI &v){
    VI t;
    t.resize(v.size());
  
    for(int k=1;k<=v.size();k*=2){ // starting with two element subarray
        for(int i=0;i<v.size();i+=2*k){
          int  p=i, q=i+k-1;
          int  r= i+2*k-1;  
            if(r>=v.size()){
                r=v.size()-1;
            }
            if(q>=v.size()){
               q=v.size()-1;
            }
            // copy to aux
            for(int l=p;l<=r;l++){
                t[l]=0+v[l];
            }
            int t1 = p, t2=q+1, f=p;
            while(t1<=q&&t2<=r){
                if(t[t1]<t[t2]){
                    v[f++]=t[t1++];
                } else {
                    v[f++]=t[t2++];
                }
            }
            while(t1<=q){
                v[f++]=t[t1++];
            }
            while(t2<=r){
                v[f++]=t[t2++];
            }
        }
    }
    return v;
}
Example #15
0
int main ()
{
#ifndef ONLINE_JUDGE
  freopen ("150E.in" , "r", stdin);
  freopen ("150E.out", "w", stdout);
#endif
  ios_base::sync_with_stdio (false);

  int n; VI ws;
  
  cin >> n >> L >> R; G.resize (n);

  for (int i = 1, s, t, w; i < n; ++i)
    {
      cin >> s >> t >> w;
      G.add (s, (edge){t, w});
      G.add (t, (edge){s, w});
      ws.push_back (w);
    }

  sort (iter (ws));
  ws.resize (unique (iter (ws)) - ws.begin ());
  int l = 0, r = ws.size () - 1, mid;
  for (; mid = (l + r + 1) >> 1, l < r; )
Example #16
0
int main()
{
	#ifndef ONLINE_JUDGE
		freopen ("in.txt", "r", stdin);
		// freopen ("out.txt", "w", stdout);
	#endif
	int n,m;
	VI P;
	VVI rooms;
	numTest(t)
	{
		cin>>n>>m;
		rooms.resize(n);
		P.resize(m);
		scana(P, m);
		REP(j,n)
		{
			int num;
			cin>>num;
			rooms[j].resize(num);
			scana(rooms[j], num);
			sort(rooms[j]);
		}
		// PR0(P,m);
		// REP(i,n)
		// 	PR0(rooms[i],sz(rooms[i]));
		ULL vol = 0;
		REP(i,m)
		{
			int s = sz(rooms[P[i]]);
			if(s != 0)
			{
				vol += rooms[P[i]][s-1];
				rooms[P[i]].pop_back();
			}
		}
	SEGTREE(const int & size)
	{
		n = size;
		tree.resize(4 * n, 0);
		lazy.resize(4 * n, false);
	}
void run()
{
	scanf("%d", &n);
	a.resize(n);
	for (int i = 0; i < n; i++)
		scanf("%d", &a[i]);
	for (int i = 0; i < n; i++)
		a[i]--;
	
	vertex *v = new vertex(0, n - 1);
	
	for (int i = 0; i < n; i++)
	{
		int amn = v->get(0, i - 1, a[i]);
		int ost = (a[i] - amn);
		d[a[i]] = i + ost - a[i];
	}
	
	for (int i = 0; i < n; i++)
		if (debug) printf("%d ", d[i]);
	if (debug) printf("\n");
	
	for (int i = 0; i < n; i++)
	{
		l[i][0] = a[0] < i;
		for (int j = 1; j < n; j++)
			l[i][j] = l[i][j - 1] + (a[j] < i);
	}
	
	for (int i = 0; i < n; i++)
		if (debug) printf("%d ", l[i][i]);
	if (debug) printf("\n");
	
	for (int i = 0; i <= n; i++)
		if (i == n)
		{
			printf("%d %d\n", -1, -1);
			return;
		}
		else if (a[i] != i)
			break;
	
	int mind = INF;
	int i1 = -1, i2 = -1;
	for (int i = 0; i < n; i++)
		for (int j = 0; j < n; j++)
			if (a[i] > a[j])
			{
				int amni = j == 0 ? 0 : l[a[i]][j - 1];
				int osti = (a[i] - amni);
				int di = j + osti - a[i];
				
				int amnj = i == 0 ? 0 : l[a[j]][i - 1];
				int ostj = (a[j] - amnj);
				int dj = i + ostj - a[j];
				
				int cd = di + dj - d[a[i]] - d[a[j]];
				//dbg(di - d[a[i]]);
				//dbg(dj - d[a[j]]);
				if (cd < mind)
				{
					mind = cd;
					i1 = i;
					i2 = j;
				}
			}
	
	printf("%d %d\n", i1 + 1, i2 + 1);
}
Example #19
0
void solve()
{
    int ret=0;

    for(int i=1;i<=n;++i)
    {
        for(int j=i;j<=n;++j)
        {

            nums.clear();
            nums.PB(DUMMY);

            summ[0]=0;
            reqa[0]=summ[0]-a;
            reqb[0]=summ[0]-b-1;

            nums.PB(summ[0]);
            nums.PB(reqa[0]);
            nums.PB(reqb[0]);


            for(int k=1;k<=m;++k)
            {

                summ[k]=summ[k-1]+arr[j][k]-arr[i-1][k];
                reqa[k]=summ[k]-a;
                reqb[k]=summ[k]-b-1;

                nums.PB(summ[k]);
                nums.PB(reqa[k]);
                nums.PB(reqb[k]);
            }



            sort(nums.begin()+1,nums.end());
            it=unique(nums.begin()+1,nums.end());
            nums.resize(distance(nums.begin(),it));



            nums_len=nums.size();
            int ti;

            MSET(bitt,0);
            it=lower_bound(nums.begin()+1,nums.end(),summ[0]);
            ti=it-nums.begin();

            //printf("Nums Len\t%d\n",nums_len);

            update(ti);

            for(int k=1;k<=m;++k)
            {
                //printf("DD\n");
                int en=lower_bound(nums.begin()+1,nums.end(),reqa[k])-nums.begin();
                int st=lower_bound(nums.begin()+1,nums.end(),reqb[k])-nums.begin();

                ret+=query(en)-query(st);

                it=lower_bound(nums.begin()+1,nums.end(),summ[k]);
                ti=it-nums.begin();

                update(ti);
            }
        }
    }

    printf("%d\n",ret);
}
Example #20
0
int main( void )
{
  freopen("republic.in", "rt", stdin);
  freopen("republic.out", "wt", stdout);

//  fprintf(stderr, "reading...");
  int N, M;
  scanf("%d%d", &N, &M);
  for (int i = 0; i < M; i++)
  {
    int a, b;
    scanf("%d%d", &a, &b);
    V1[a - 1].PB(b - 1);
  }
//  fprintf(stderr, "%c%c%c [OK]\n", 8, 8, 8);

//  fprintf(stderr, "dfs1...");
  memset(C, 0, sizeof(C));
  for (int i = 0; i < N; i++)
    if (!C[i])
      dfs1(i);
//  fprintf(stderr, "%c%c%c [OK]\n", 8, 8, 8);

//  fprintf(stderr, "dfs2...");
  memset(C, 0, sizeof(C));
  int Color = 0;
  for (int i = 0; i < N; i++)
    if (!C[P[i]])
      dfs2(P[i], Color++);
  int N2 = Color;
//  fprintf(stderr, "%c%c%c [OK]\n", 8, 8, 8);

//  fprintf(stderr, "condensate...");
  for (int i = 0; i < N; i++)
  {
    Index[Co[i]] = i + 1;
    for (int j = 0; j < (int)V1[i].size(); j++)
      V2[Co[i]].PB(Co[V1[i][j]]), R2[Co[V1[i][j]]].PB(Co[i]);
  }
//  fprintf(stderr, "%c%c%c [OK]\n", 8, 8, 8);

/*
  fprintf(stderr, "\nG = %d %d\n", N2, M);
  for (int i = 0; i < N2; i++)
    for (int j = 0; j < (int)V2[i].size(); j++)
      fprintf(stderr, "%d %d\n", i + 1, V2[i][j] + 1);
  fprintf(stderr, "\n");
*/

//  fprintf(stderr, "dfs3...");
  memset(C, -1, sizeof(C));
  int Color2 = 0;
  for (int i = 0; i < N2; i++)
    if (C[i] == -1)
      dfs3(i, Color2++);
//  fprintf(stderr, "%c%c%c [OK]\n", 8, 8, 8);

/*
  fprintf(stderr, "C[] = {");
  for (int i = 0; i < N2; i++)
    fprintf(stderr, "%d%s", C[i], i < N2 - 1 ? ", " : "}\n");
*/

//  fprintf(stderr, "searching...");
  for (int i = 0; i < N2; i++)
  {
    if (V2[i].size() == 0)
      Col2[C[i]].PB(i);
    if (R2[i].size() == 0)
      Col1[C[i]].PB(i);
  }
//  fprintf(stderr, "%c%c%c [OK]\n", 8, 8, 8);

/*
  for (int i = 0; i < Color2; i++)
  {
    fprintf(stderr, "Col1[i = %d] = {", i);
    for (int j = 0; j < (int)Col1[i].size(); j++)
      fprintf(stderr, "%d%s", Col1[i][j], j < (int)Col1[i].size() - 1 ? ", " : "");
    fprintf(stderr, "}\n");
    fprintf(stderr, "Col2[i = %d] = {", i);
    for (int j = 0; j < (int)Col2[i].size(); j++)
      fprintf(stderr, "%d%s", Col2[i][j], j < (int)Col2[i].size() - 1 ? ", " : "");
    fprintf(stderr, "}\n");
  }
*/

  vector <PII> Answer(0);
  int UpV = -1, DoV = -1;

//  fprintf(stderr, "connect...");
  if (Color2 > 1)
    for (int i = 0; i < Color2; i++)
    {
      int j = (i + 1) % Color2;
      Answer.PB(MP(Index[Col2[i][(int)Col2[i].size() - 1]], Index[Col1[j][(int)Col1[j].size() - 1]]));
      UpV = Index[Col1[j][(int)Col1[j].size() - 1]];
      DoV = Index[Col2[i][(int)Col2[i].size() - 1]];
      Col2[i].resize(Col2[i].size() - 1);
      Col1[j].resize(Col1[j].size() - 1);
    }
//  fprintf(stderr, "%c%c%c [OK]\n", 8, 8, 8);

//  fprintf(stderr, "remake...");
  for (int i = 0; i < Color2; i++)
  {
    for (int j = 0; j < (int)Col1[i].size(); j++)
      Res1.PB(Col1[i][j]);
    for (int j = 0; j < (int)Col2[i].size(); j++)
      Res2.PB(Col2[i][j]);
  }
//  fprintf(stderr, "%c%c%c [OK]\n", 8, 8, 8);

/*
  fprintf(stderr, "Res1 = {");
  for (int j = 0; j < (int)Res1.size(); j++)
    fprintf(stderr, "%d%s", Res1[j], j < (int)Res1.size() - 1 ? ", " : "");
  fprintf(stderr, "}\n");
  fprintf(stderr, "Res2 = {");
  for (int j = 0; j < (int)Res2.size(); j++)
    fprintf(stderr, "%d%s", Res2[j], j < (int)Res2.size() - 1 ? ", " : "");
  fprintf(stderr, "}\n");
*/

//  fprintf(stderr, "generate...");
  int N3 = (int)max(Res1.size(), Res2.size());
  for (int i = 0; i < N3; i++)
  {
//    fprintf(stderr, "\ni = %d of %d", i, (int)max(Res1.size(), Res2.size()));
    int T1, T2;
    if (Res1.size())
      T1 = Index[Res1[Res1.size() - 1]], Res1.resize(Res1.size() - 1);
    else
      T1 = UpV;
    if (Res2.size())
      T2 = Index[Res2[Res2.size() - 1]], Res2.resize(Res2.size() - 1);
    else
      T2 = DoV;
    UpV = T1, DoV = T2;
//    fprintf(stderr, "\ni = %d, T1 = %d, T2 = %d\n", i, T1, T2);
    if (T1 != T2)
      Answer.PB(MP(T2, T1));
  }
//  fprintf(stderr, "%c%c%c [OK]\n", 8, 8, 8);

//  fprintf(stderr, "writing...");
  printf("%d\n", (int)Answer.size());
  for (int i = 0; i < (int)Answer.size(); i++)
    printf("%d %d\n", Answer[i].FI, Answer[i].SE);
//  fprintf(stderr, "%c%c%c [OK]\n", 8, 8, 8);

  return 0;
}