Exemple #1
0
int main()
{
	ios_base::sync_with_stdio(false);
	FastInput in;
	int T=in.ReadNext();
	for(int t=0;t<T;t++)
	{	
		int x=abs(in.ReadNext());
		int y=abs(in.ReadNext());
		
		if(x==y)
		{	
			cout<<(long long)x+y<<"\n";
		}		
		else if(x>y)
		{
			int d=(x-y);	
			cout<<2LL*y+4LL*(d/2)+3LL*(d%2)<<"\n";	
		}
		else if(x<y)
		{
			int d=(y-x);	
			cout<<2LL*x+4LL*(d/2)+1LL*(d%2)<<"\n";	
		}
	}
	return 0;
}
int main() {
		uint32_t testcases = g_fi.ReadNext();
/*				max2 = (float **)calloc(2000,  sizeof(float*));
				used = (int **)calloc(2000  ,  sizeof(int *));
				for (int r = 0; r < 2000;r++) {
					max2[r] = (float *)calloc(2000  ,  sizeof(float));
					used[r] = (int *)calloc(2000  ,  sizeof(int));
				}*/
//			memset(max2, 0, sizeof(float)*2000*2000);
			memset(used, 0, sizeof(int)*2000);

		for (tc = 0; tc < testcases; ++tc) {
				 //cout<<"Test Cases  "<<i<<endl;
				 NumberofDiamonds =  g_fi.ReadNext();
				diamonds = (int *)malloc(NumberofDiamonds*sizeof(int *));
				for (int r = 0; r < NumberofDiamonds;r++) {
						uint32_t S = g_fi.ReadNext();
						diamonds[r] = S;
					}
				
		
				//This variable will be used to maintain the index of the  lookup table which will help in DP
				int index = 1;

				//Building the lookup table for the dynamic program max2 thing.

				float answertc;
				steps = 0;
				int lev = ceil(log(NumberofDiamonds)/log(2));
				//Sorting the Elements.
				//Iteration over the activity store available with us .
					answertc = expecteddiamondvalue(0,NumberofDiamonds-1,lev);	
				/*for ( int i = 0;i < activitystore.size()+1;i++) {
					cout<<"Max t"<<i<<"   "<<max2[i];
				}*/
				cout<<"Steps :" <<steps;
				printf("%f\n",answertc);

//				memset(max2, 0, sizeof(float)*2000*2000);
				for(int i=0;i<NumberofDiamonds;i++) {
					cout<<"   i"<<used[i]<<endl;
				}
				memset(used, 0, sizeof(int)*2000);
				free(diamonds);
}

		//				g_fo.PrintUint(answertc, '\n');
				////cout<<"final answer  : "<<a<<"After division by :"<<divis<<endl;
/*
				for (int r = 0; r < NumberofDiamonds;r++) {
					free(max2[r]);
					free(used[r]);
				}
				free(max2);
				free(used);*/
		g_fo.Flush();
		return 0;
}
Exemple #3
0
int main() {
	//ios_base::sync_with_stdio(false);

	FastInput input;
	//FastOutput output;

	int nCases = input.ReadNext();
	//int nCases; scanf("%d\n",&nCases);
	
	for(int caseNum=1;caseNum<=nCases;caseNum++) {
		nRows = input.ReadNext();
		nCols = input.ReadNext();
		int nNodes = 1;
		int w_min = INF; //max 10
		int nEdges = 1;
		for(int i=0;i<nRows;i++) {
			for(int j=0;j<nCols;j++) {
				//grid[i][j] = input.ReadNext();
				deg[nNodes] = 0;
				if (j-1>=0) // add it
					myGraph[nNodes][deg[nNodes]++] = make_pair(nNodes-1,nEdges++);
				if (j+1<nCols) 
					myGraph[nNodes][deg[nNodes]++] = make_pair(nNodes+1,nEdges++);
				if (i-1>=0)
					myGraph[nNodes][deg[nNodes]++] = make_pair(nNodes-nCols, nEdges++);
				if (i+1<nRows)
					myGraph[nNodes][deg[nNodes]++] = make_pair(nNodes+nCols, nEdges++);

				myGrid[nNodes] = input.ReadNext()+ADD_FACTOR;
				w_min = min (w_min,myGrid[nNodes]);
				nNodes++;
			}
		}
		
		//build graph
		buildMyGraph(nNodes); //takes 0.482s?

		cerr << "Done building" << endl;
		//output.PrintUint(dijkstraCrazyQueue(0, nNodes, nRows*nCols),'\n');
		printf("%d\n", ASP(0, nNodes, nRows*nCols,w_min));
		//printf("%d\n",SPFA_SLF(nNodes,0, nRows*nCols));
	}
	//cout <<"fart" << endl;
	//output.Flush();
	return 0;
}
Exemple #4
0
int main(){
	while(1){
		n=in.ReadNext();
		if(!n)
			break;
		for(i=1;i<=n;++i)
			a[i]=in.ReadNext();
		st=stack<int>();
		//Calculating left indices
		for(i=1;i<=n;++i){
			while(!st.empty()){
				if(a[i]<=a[st.top()])
					st.pop();
				else
					break;
			}
			if(st.empty())
				sum[i]=i-1;
			else
				sum[i]=i-st.top()-1;
			st.push(i);
		}
		//Calculating right indices
		st=stack<int>();
		for(i=n;i>=1;--i){
			while(!st.empty()){
				if(a[i]<=a[st.top()])
					st.pop();
				else
					break;
			}
			if(!st.empty())
				sum[i]+=st.top()-i-1;
			else
				sum[i]+=n-i;
			st.push(i);
		}
		ans=0;
		for(i=1;i<=n;++i)
			ans=max(ans, (ll)a[i]*(ll)(sum[i]+1));
		printf("%lld\n", ans);
	}
	return 0;
}
int main() {
	FastInput input;
	int nCases = input.readNextUnsigned();
	for(int caseNum = 1; caseNum<=nCases;caseNum++) {
		n = input.readNextUnsigned();
		k = input.readNextUnsigned();
		maxFactor = min (n,k);
		for(int i=0;i<n;i++) {
			for(int j=0;j<n;j++) {
				grid[i][j] = input.readNextUnsigned();
				memo[i][j] = -1;
			}
		}

		printf("%d\n",unrolledHop(0,0,grid[0][0])+grid[0][0]);
		if (caseNum == nCases) break;
		printf("\n");
	}
	return 0;
}
Exemple #6
0
int main() {
	int nCities, nRoads; 
	ios_base::sync_with_stdio(false);
	FastInput input;
	while (true) {
		nCities = input.readNextUnsigned();
		nRoads = input.readNextUnsigned();
		if (nCities == 0 && nRoads == 0) break;
		
		vector<Edge> edges;
		for(int i=0;i<nRoads;i++) {
			int u = input.readNextUnsigned();
			int v = input.readNextUnsigned();
			int w = input.readNextUnsigned();
			edges.push_back(Edge(u,v,w));
			//assert(w < 100000);
		}
		ini(nCities);

		sort(begin(edges), end(edges));

		int maxW = 0;
		int nConnected = 0;
		for(int i=0;i<edges.size();i++) {
			int xroot = set_find(edges[i].u);
			int yroot = set_find(edges[i].v);
			if (xroot != yroot) {
                link(xroot,yroot);
                int w = edges[i].w;
                nConnected++;
                if (w > maxW) maxW = w; 
            }
		}
		if (nConnected + 1 == nCities)
			cout << maxW << "\n";
		else cout << "IMPOSSIBLE\n";
	}

	return 0;
}
Exemple #7
0
int main() {
		// Taking input of number of testcases to be solved.
		uint32_t testcases =  g_fi.ReadNext();


		//Loop to go through process number of testcases time.
		for (uint32_t i = 0; i < testcases; ++i) {
			
				// This map will hold the level wise total score of soint.
				map<int,int> sointlevelscore;
				// This map will hold the level wise total score of sofloat.
				map<int,int> sofloatlevelscore;


				// Inputing number of Warriers in each team Soint and soFloat
				uint32_t sointN = g_fi.ReadNext();
				uint32_t sofloatM = g_fi.ReadNext();
				
				for ( uint32_t j  = 0;j < sointN ; j++ ) {
					uint32_t chakra = g_fi.ReadNext();
					uint32_t level = g_fi.ReadNext();
					if (sointlevelscore.find(level) == sointlevelscore.end()) {
						sointlevelscore[level] = chakra;
					} else {
						sointlevelscore[level] = sointlevelscore.find(level)->second + chakra;
					}
				}

				for ( uint32_t j  = 0;j < sofloatM ; j++ ) {
					uint32_t chakra = g_fi.ReadNext();
					uint32_t level = g_fi.ReadNext();
					if (sofloatlevelscore.find(level) == sofloatlevelscore.end()) {
						sofloatlevelscore[level] = chakra;
					} else {
						sofloatlevelscore[level] = sofloatlevelscore.find(level)->second + chakra;
					}
				}
				int minimumchakra = 0;
	//To check how much time actual logic takes
/*
				map<int,int>::iterator chakrainterator;
				for ( chakrainterator = sointlevelscore.begin();chakrainterator != sointlevelscore.end();chakrainterator++) {
					int score = ( sofloatlevelscore[(*chakrainterator).first] - (*chakrainterator).second ); 
					if ( score > 0 ) {
						minimumchakra += score; 						
					}
				}
*/

				g_fo.PrintUint(minimumchakra, '\n');
		}
		g_fo.Flush();
		return 0;
}
Exemple #8
0
int main()
{
    ios_base::sync_with_stdio(0);
    int n, m, currL, currR;
    node q[N];
    //cin>>n;
    n=g_fi.ReadNext();
    fo(i, n)
    {
        //cin>>dat[i];
        dat[i]=g_fi.ReadNext();
    }
    //cin>>m;
    m=g_fi.ReadNext();
    fo(i, m)
    {
        //cin>>q[i].l>>q[i].r;
        q[i].l=g_fi.ReadNext();
        q[i].r=g_fi.ReadNext();
        q[i].l--, q[i].r--;
        q[i].i=i;
    }
int main() {
  FastInput obj;
	while(true) {
		hand[0] = obj.ReadNext(); 
    hand[1] = obj.ReadNext(); 
    hand[2] = obj.ReadNext();
		if((hand[0]|hand[1]|hand[2]) == 0) return 0;
		sort(hand, hand+3);
		if(hand[0] == hand[1] && hand[1] == hand[2]) { // set
			if(hand[0] == 13) puts("*");
			else printf("%d %d %d\n", (hand[2]+1), (hand[2]+1), (hand[2]+1));
		}
		else if(hand[0] == hand[1]) { // pair 1
			hand[2]++;
			if(hand[2] > 13) {
				hand[0]++, hand[1]++, hand[2] = 1;
			}
			sort(hand, hand+3);
			printf("%d %d %d\n", hand[0], hand[1], hand[2]);
		}
		else if(hand[1] == hand[2]) { // pair 2
			if(hand[0] == 12 && hand[1] == 13) {
				puts("1 1 1");
				continue;
			}
			do { 
				hand[0]++; 
				if(hand[0] > 13) {
					hand[0] = 1, hand[1]++, hand[2]++;
					break;
				}
			} while(hand[0] == hand[1]);
			sort(hand, hand+3);
			printf("%d %d %d\n", hand[0], hand[1], hand[2]);
		}
		else puts("1 1 2");
	}
}
Exemple #10
0
int main() {
	FastInput input;
	//FastOutput output;
	int nCases = input.ReadNext();
	int n,m,source,destination;
	int u,v,w;
	int i;
	for(int numCase=1;numCase<=nCases;numCase++) {

		n=input.ReadNext();
		m=input.ReadNext();
		source=input.ReadNext();
		destination=input.ReadNext();

		for(i=0;i<=n;i++) adj[i].clear();
		//for(i=0;i<=n;i++) deg[i] = 0;

		for(i=0;i<m;i++) {
			u = input.ReadNext();
			v = input.ReadNext();
			w = input.ReadNext();

			//edges[u][deg[u]++] = {w,v};

			//edges[v][deg[v]++] = {w,u};

			adj[u].push_back(make_pair(w,v));
			adj[v].push_back(make_pair(w,u));
		}
		SPFA_SLF(n,source,destination);
		/*
		output.PrintStr("Case #");
		output.PrintUint(numCase,':');
		output.PrintChar(' ');
		*/
		//cout << "Case #" << numCase <<": ";
		if(dist[destination]==INF) {
			//output.PrintStr("unreachable\n");
			printf("Case #%d: unreachable\n",numCase);
			//cout << "unreachable" << "\n";
		} else { 
			//output.PrintUint(dist[destination],'\n');
			printf("Case #%d: %d\n", numCase, dist[destination]);
			//cout << dist[destination] << "\n";
		}
	}
	//output.Flush();
	return 0;
}
Exemple #11
0
int main() {
    int tst,t,i,j,f,k,c,ansi,cnt,cs;
    //scanf("%d",&tst);
    FastInput input;
    tst = input.readNextUnsigned();
    for(cs=1; cs<=tst; cs++) {
        n = input.readNextUnsigned();
        e = input.readNextUnsigned();
        ac = input.readNextUnsigned();
        ini();
        int myMaxWeight = 0;
        for(j=0; j<e; j++) {
            f = input.readNextUnsigned();
            t = input.readNextUnsigned();
            c = input.readNextUnsigned();

            if (c < ac) {
                //linear sort
                edgesCounting[c].push_back(make_pair(f,t));
                myMaxWeight = max(myMaxWeight,c);
            }
        }
        //linear sort extraction
        int weightTotal = 0;
        for(int weight = 1; weight<=myMaxWeight;++weight) {
            for(int j=0;j<edgesCounting[weight].size();++j) {
                int xroot = set_find(edgesCounting[weight][j].first);
                int yroot = set_find(edgesCounting[weight][j].second);
                if (xroot != yroot) {
                    link(xroot,yroot);
                    weightTotal += weight;
                }
            }
            edgesCounting[weight].clear();
        }

        ansi=weightTotal;
        cnt=0;
        for(i=1; i<=n; i++)
            if(par[i]==i)cnt++;

        printf("Case #%d: %d %d\n",cs,ansi+(cnt*ac),cnt);
    }
    return 0;
}
Exemple #12
0
int main() {
  cin.sync_with_stdio(0);
 
  vector<int> primes;
  for (int i=2; i<=100; ++i) {
    bool ok = true;
    for (int j=2; j<i; ++j) {
      if (i % j == 0) {
        ok = false;
      }
    }
    if (ok) {
      primes.push_back(i);
    }
  }
  int nprimes = primes.size();
 
  static int cnt[100001][25];
  ZERO(cnt[0]);
 
  FastInput fin;
  FastOutput fout;
  int n = fin.readuint();
  for (int i=1; i<=n; ++i) {
    memcpy(cnt[i], cnt[i-1], sizeof cnt[i]);
    int x = fin.readuint();
    for (int j=0; j<nprimes; ++j) {
      while (x % primes[j] == 0) {
        x /= primes[j];
        ++cnt[i][j];
      }
    }
  }
 
  int q = fin.readuint();
  repeat (q) {
    int l = fin.readuint(), r = fin.readuint(), mod = fin.readuint();
    unsigned result = mod > 1 ? 1 : 0;
 
    static vector<pair<int, int> > entries; entries.clear();
    for (int a=0; a<nprimes; ++a) {
      int n = cnt[r][a] - cnt[l-1][a];
      if (n > 0) {
        entries.push_back(make_pair(n, primes[a]));
      }
    }
 
    sort(ALL(entries), greater<pair<int, int> >());
    entries.push_back(make_pair(0, 1));
 
    unsigned a = 1;
    for (int i=0; i<(int)entries.size()-1; ++i) {
      a = maybe_mod(mult(a, entries[i].second), mod);
      unsigned now = entries[i].first - entries[i+1].first;
      if (now > 0) {
        result = maybe_mod(mult(result, modpow(a, now, mod)), mod);
      }
    }
 
    fout.writeuint(result);
    fout.putchar('\n');
  }
 
  return 0;
}
int main() {
	FastInput input;

	int nComputers;
	nComputers = input.readNextUnsigned();
	while (true) {
		if (nComputers == 0x80000000) break;

		long long sum = 0;
		for(int i=0;i<nComputers-1;++i) {
			int u, v, w;
			u = input.readNextUnsigned();
			v = input.readNextUnsigned();
			w = input.readNextUnsigned();

			sum +=w;
		}
		cout << sum << '\n';

		int nAdditionalComputers = input.readNextUnsigned();

		nEdges = 0;
		for(int i=0;i<nAdditionalComputers;i++) {
			int u, v, w;
			u = input.readNextUnsigned();
			v = input.readNextUnsigned();
			w = input.readNextUnsigned();
			edges[nEdges++] = Edge(u,v,w);
		}

		int nAdditionalEdges = input.readNextUnsigned();

		for(int i=0;i<nAdditionalEdges;++i) {
			int u, v, w;
			u = input.readNextUnsigned();
			v = input.readNextUnsigned();
			w = input.readNextUnsigned();
			edges[nEdges++] = Edge(u,v,w);
		}

		sort(edges, edges+nEdges);

		UnionFind uf(nComputers+1);
		long long newSum = 0;
		for(int i=0;i<nEdges;++i) {
			//take off edge
			int w = edges[i].w;
			int u = edges[i].u;
			int v = edges[i].v;

			int xroot = uf.find(u);
			int yroot = uf.find(v);

			if (xroot != yroot) {
				uf.Union(xroot,yroot);
				newSum += w;
			}
		}

		cout << newSum << '\n';
		nComputers = input.readNextUnsigned();
		if (nComputers == 0x80000000) break;
		cout << '\n';

	}
	return 0;
}
Exemple #14
0
int main() {
	//ios_base::sync_with_stdio(false);
	FastInput input;
	int nCases = input.readNextUnsigned();
	for(int caseNum=1;caseNum<=nCases;++caseNum) {
		int N = input.readNextUnsigned();
		int M = input.readNextUnsigned();
		int A = input.readNextUnsigned();

		int nEdges = 0;
		int myMaxWeight = 0;
		int myMinWeight = 1000000;
		for(int i=0;i<M;++i) {
			int u = input.readNextUnsigned();
			int v = input.readNextUnsigned();
			int w = input.readNextUnsigned();
			--u;--v;
			if (w >= A) continue;
			edges.insert(make_pair(w,make_pair(u,v)));
			//edgesCounting[w].push_back(make_pair(u,v));
			//myMaxWeight = max(myMaxWeight, w);
			//myMinWeight = min(myMinWeight, w);
			//adj[v].push_back({u,w});
		}
		UnionFind uf(N);
		//sort in linear time
		/*
		weightTotal = 0;
		for(int weight = myMinWeight; weight<=myMaxWeight;++weight) {
			for(int j=0;j<edgesCounting[weight].size();++j) {
				int xroot = uf.find(edgesCounting[weight][j].first);
				int yroot = uf.find(edgesCounting[weight][j].second);
				if (xroot != yroot) {
					uf.Union(xroot,yroot);
					weightTotal += weight;
				}
			}
			edgesCounting[weight].clear();
		}
		*/
		
		weightTotal = 0;
		//sort(edges,edges + nEdges); //stable sort
		while (edges.size()) {
			auto temp = edges.begin();
			int w = temp->first;
			int xroot = uf.find(temp->second.first);
			int yroot = uf.find(temp->second.second);

			edges.erase(temp);

			if (xroot != yroot) {
				uf.Union(xroot,yroot);
				weightTotal += w;
			}
		}
		/*
		for(int i=0;i<nEdges;++i) {
			int xroot = uf.find(edges[i].second.first);
			int yroot = uf.find(edges[i].second.second);
			if (xroot != yroot) {
				uf.Union(xroot,yroot);
				weightTotal += edges[i].first;
			}
		}
		*/
		nComponents = 0;
		for(int i=1;i<=N;++i) {
			int place = uf.find(i);
			if (!mySet[place]) {
				++nComponents;
				mySet[place] = 1;
			}
		}
		mySet = 0;
		int ans = weightTotal + A * nComponents;
		cout << "Case #" << caseNum << ": " << ans << " " << nComponents << '\n';

	}
	return 0;
}