Example #1
0
void reduction(double ** matrix, int * rows, int * cols)
{
	double max = 0;
	int i, j;
	if (rank == 0) { //master
		max = getmax(matrix, rows, cols);
		double temp;
		for (i = 1; i < size; i++) { //get the max from each other process
			MPI_Recv(&temp, 1, MPI_DOUBLE, i, SLAVE_TO_MASTER_TAG, MPI_COMM_WORLD, &status);
			if (temp > max) {max = temp;}
		}
	} else { //slave
		max = getmax(matrix, rows, cols);
		MPI_Send(&max, 1, MPI_DOUBLE, 0, SLAVE_TO_MASTER_TAG, MPI_COMM_WORLD);
	}
	MPI_Barrier(MPI_COMM_WORLD);
	if (rank == 0) { //master
		for (i = 1; i < size; i++) {
			MPI_Isend(&max, 1, MPI_DOUBLE, i, MASTER_TO_SLAVE_TAG, MPI_COMM_WORLD, &request);
		}
	} else { //slave
		MPI_Recv(&max, 1, MPI_DOUBLE, 0, MASTER_TO_SLAVE_TAG, MPI_COMM_WORLD, &status);
	}
	for (i = 0; i < *rows; i++) {
		for (j = 0; j < *cols; j++) {
			matrix[i][j] /= max;
		}
	}
}
Example #2
0
int main()
{
	scanf("%d%d", &n, &kmax);
	for(int i = 1; i <= n; i++)
	{
		scanf("%d", &a[i]);
		b[i] = a[i];
	}
	for(int ti = 1; ti <= kmax; ti++)
	{
		int maxx = getmax(a);
		int minx = getmin(a);
		if(maxx == minx) break;
		a[maxx]--; a[minx]++;
		ans[Cnt].i = maxx;
		ans[Cnt++].j = minx;
	}
	int ANS = a[getmax(a)] - a[getmin(a)];
	printf("%d %d\n", ANS, Cnt);
	for(int i = 0; i < Cnt; i++)
	{
		printf("%d %d\n", ans[i].i, ans[i].j);
	}
	return 0;
}
Example #3
0
int main()
{
    int t = 0;
    int i = 0;
    int j = 0;
    double p = 0.0;
    double t1 = 0.0;
    double t2 = 0.0;
    double max = -1.0;
    scanf("%d", &t);

    for(i = 0; i < t; i++)
    {
        scanf("%f", &p);
        max = -1.0;
        for(j = 0; j <= 10000; j++)
        {
            t1 = (3 - 2 * p) * j * 1.0 - 10000;
            t2 = (3 - 2 * (1 - p)) * j * 1.0 - 10000;
            max = getmax(max, t1 * p + t2 * (1 - p));
            printf("%.6f %.6f\n", t1 ,t2);
        }
        printf("%.6f\n", max);
    }

    return 0;
}
Example #4
0
static PropPtr
getmax(PropPtr avl)
{
	if (avl && AVL_RT(avl))
		return getmax(AVL_RT(avl));
	return avl;
}
Example #5
0
int main()
{
    freopen("FREQUENT.in", "r", stdin);
    freopen("FREQUENT.out", "w", stdout);
    while (scanf("%d", &n), n)
    {
	M = 1, index = 0;
	scanf("%d", &q);
	for (int i = 1, last = -nmax - 1, a; i <= n; ++i)
	    if (scanf("%d", &a), a == last)
		++c[p[i] = index];
	    else
		r[index] = i - 1, c[p[i] = ++index] = 1, l[index] = i, last = a;
	r[index] = n;
	while (M <= index) M <<= 1;
	for (int i = 1; i <= index; ++i) seg[M + i] = c[i];
	for (int i = M - 1; i; --i) seg[i] = max(seg[i << 1], seg[i << 1 | 1]);
	for (int nl, nr; q--;)
	{
	    scanf("%d%d", &nl, &nr);
	    printf("%d\n", max(getmax(p[nl] + 1, p[nr] - 1), max((r[p[nl]] >= nr ? nr : r[p[nl]]) - nl + 1, nr - (l[p[nr]] <= nl ? nl : l[p[nr]]) + 1)));
	}
    }
    return 0;
}
 int maximalRectangle(vector<vector<char> > &matrix) {
     
     if(matrix.size() == 0)
         return 0;
     if(matrix[0].size() == 0)
         return 0;
         
     int iret = 0;
     vector<int> currentline(matrix[0].size()+2,0);
     
     for(int i = 0;i < matrix.size();i++)
     {
         for(int j = 0; j < matrix[i].size();j++)
         {
             if(matrix[i][j] == '0')
             {
                 currentline[j+1] = 0;
             }
             else if(matrix[i][j] == '1')
             {
                 currentline[j+1] += 1;
             }
         }
         
         int tmp = getmax(currentline);
         if(tmp > iret)
             iret = tmp;
     }
     
     return iret;
     
 }
Example #7
0
static void
draw(
        SDL_Renderer * renderer,
        int width,
        int height,
        float (* state)[NHOR][NVER]
        ) {
    SDL_Rect rect;
    int r, g, b, a = 0xff,
        blockw = width / NHOR,
        blockh = height / NVER;
    float scale = getmax(state);
    SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
    SDL_RenderClear(renderer);
    float value;
    for (int idy = 0; idy < NVER; idy++) {
        for (int idx = 0; idx < NVER; idx++) {
            value = GET(0, 0);
            r = g = b = 0;
            if (value > 0) {
                r = (int)(0xff * value);
            } else {
                b = (int)(0xff * -value);
            }
            r = g = b = bound((int)(0xff * (0.5+value/2)), 0, 0xff);
            SDL_SetRenderDrawColor(renderer, r, g, b, a);
            rect = (SDL_Rect){ idx*blockw, idy*blockh, blockw, blockh };
            SDL_RenderFillRect(renderer, &rect);
        }
    }
}
Example #8
0
int solve(int rows) {
    // let sum[i][j] be the maximum sum when path ends at ith row and jth coloumn
    int  max = 0;
    int i,j;
    int sum[100][101];
    for(i = 0; i < 100; i++) {
        for(j = 0; j < 101; j++) {
            sum[i][j] = 0;
        }
    }


    sum[0][1] = arr[0][1];

    for(i = 1;i < rows; i++) {
        for(j = 1; j <= i+1; j++) {
            sum[i][j] = getmax(sum[i-1][j], sum[i-1][j-1]) + arr[i][j];
        }

    }

    for(i=1;i<rows+1;i++) {
        if(sum[rows-1][i] > max )
            max = sum[rows-1][i];
    }
    return max;
}
Example #9
0
/*__________________________________________________________________________________________*/
CPoint wzq::AiGo(bool color)
{
	CPoint turnback(-1,-1);
	clear(false);					//pc全部置为255
	fillin(color,false,true);
	fillin(!color,false,false);
	wzq::aitype2 = getAItype();
	unsigned char found = 255;
	for (int ix = 0; ix < 15; ix++)
	{
		for (int iy = 0; iy < 15; iy++)
		{
			for (int im = 0; im <= 3; im++)
			{
				if (pc[ix][iy][im] <= found)
				{
					found = pc[ix][iy][im];
					turnback.x = ix,turnback.y = iy;
				}
			}
		}
	}
	if (found <= 23)
		return turnback;
	else
		return getmax(color);
}
int main(int argc, char *argv[])
{
   long size = 0;  // The size of the array
   long i;  // loop index
   long * numbers; //pointer to the array
    
    if(argc !=2)
    {
       printf("usage: maxseq num\n");
       printf("num = size of the array\n");
       exit(1);
    }
   
    size = atol(argv[1]);

    numbers = (long *)malloc(size * sizeof(long));
    if( !numbers )
    {
       printf("Unable to allocate mem for an array of size %ld\n", size);
       exit(1);
    }    

    srand(time(NULL)); // setting a seed for the random number generator
    // Fill-up the array with random numbers from 0 to size-1 
    for( i = 0; i < size; i++)
       numbers[i] = rand() % size;    
   
    printf(" The maximum number in the array is: %ld\n", 
           getmax(numbers, size));

    free(numbers);
    exit(0);
}
Example #11
0
static PropPtr
remove_propnode(char *key, PropPtr * root)
{
	PropPtr save;
	PropPtr tmp;
	PropPtr avl = *root;
	int cmpval;

	save = avl;
	if (avl) {
		cmpval = Comparator(key, PropName(avl));
		if (cmpval < 0) {
			save = remove_propnode(key, &AVL_LF(avl));
		} else if (cmpval > 0) {
			save = remove_propnode(key, &AVL_RT(avl));
		} else if (!(AVL_LF(avl))) {
			avl = AVL_RT(avl);
		} else if (!(AVL_RT(avl))) {
			avl = AVL_LF(avl);
		} else {
			tmp = remove_propnode(PropName(getmax(AVL_LF(avl))), &AVL_LF(avl));
			if (!tmp)
				abort();		/* this shouldn't be possible. */
			AVL_LF(tmp) = AVL_LF(avl);
			AVL_RT(tmp) = AVL_RT(avl);
			avl = tmp;
		}
		if (save) {
			AVL_LF(save) = NULL;
			AVL_RT(save) = NULL;
		}
		*root = balance_node(avl);
	}
	return save;
}
/* Main program: Using the various functions */
int main (void) {
  int x, y, z;

  x = getint();
  y = getint();
  z = getint();
  printf("The largest of %d, %d, and %d is %d\n", x, y, z, getmax(x,y,z));
}
Example #13
0
/**
 * Get the 'maximum' AVL node, with the search starting at 'avl'.  This
 * basically finds the right-mode node.
 *
 * @private
 * @param avl the node to search
 * @return the rightmode ("max") node.
 */
static PropPtr
getmax(PropPtr avl)
{
    if (avl && avl->right)
        return getmax(avl->right);

    return avl;
}
Example #14
0
void autoget(int *x, int *y)
{
	think();
	print_score();
	getmax();
	*x = maxpoint.row;
	*y = maxpoint.col;
	printf("Player 2 : %d %d\n", *x, *y);
}
Example #15
0
void Param::ChangeVal(double newVal)
{
	if (newVal>max||newVal<min){
		cout<<"New Value of parameter: "<<name<<" is outside of bounds."<<endl;
	}
	val=getmin(newVal,max);
	val=getmax(newVal,min);
	if (val!=newVal){
		cout<<"Setting value to "<<val<<endl;
	}
}
Example #16
0
int Stack::push(int elem)
{
   int m = getmax();
   if (top < m)
   {
      put_elem(elem,top++);
      return 0;
   }
   else
      return -1;
}
Example #17
0
int isbst(struct tree *t)
{
    if( t==NULL )
        return 1;
    else if( t->left!=NULL && t->d < getmax(t->left) )
        return 0;
    else if( t->right!=NULL && t->d > getmin(t->right) )
        return 0;
    else
    return ( isbst( t->left ) && isbst ( t->right ) ) ;
    
} 
Example #18
0
int main()
{
    freopen("GSS5.in", "r", stdin);
    freopen("GSS5.out", "w", stdout);
    scanf("%d", &t);
    while (t--)
    {
	scanf("%d", &n);
	for (M = 1; n >= M; M <<= 1);
	for (int i = 1; i <= n; ++i) scanf("%d", sum + M + i), maxl[M + i] = maxr[M + i] = sub[M + i] = sum[M + i];
	for (int i = M - 1; i; --i)
	{
	    maxl[i] = max(maxl[i << 1], sum[i << 1] + maxl[i << 1 | 1]);
	    maxr[i] = max(maxr[i << 1 | 1], sum[i << 1 | 1] + maxr[i << 1]);
	    sum[i] = sum[i << 1] + sum[i << 1 | 1];
	    sub[i] = max(sub[i << 1], max(sub[i << 1 | 1], maxr[i << 1] + maxl[i << 1 | 1]));
	}
	scanf("%d", &m);
	for (int l1, r1, l2, r2; m--;)
	    if (scanf("%d%d%d%d", &l1, &r1, &l2, &r2), r1 < l2)
		printf("%d\n", getmax(l1, r1, 0) + getmax(r1 + 1, l2 - 1, 1) + getmax(l2, r2, 2));
	    else
		printf("%d\n", max(getmax(l1, l2 - 1, 0) + getmax(l2, r2, 2), max(getmax(l2, r1, 4), getmax(l2, r1, 0) + getmax(r1 + 1, r2, 2))));
    }
    return 0;
}
Example #19
0
int main()
{
int x,y;

GraphDriver= DETECT;
int graph(&GraphDriver, &GraphMode,":\\tc");
ErrorCode = graphresult();
if(ErrorCode != ok)
{
printf("Graphics System Error : %s\n", grapherrormsg(ErrorCode));
exit(1);
}
x=getmax();
y=getmax();
rectangle(x/4,y/4,3*x/4,3*y/4);
line(x/4,y/4,3*x/4,3*y/4);

line(3*x/4,y/4,x/4,3*y/4);
circle(x/2,y/2,200);

return 0;
}
Example #20
0
int main(int argc, char *argv[]){
	struct sockaddr_in cliaddr;
	char buf[MAXLINE+1];
	int i, j, nbyte, accp_sock, addrlen = sizeof(struct sockaddr_in);
	fd_set read_fds;

	if(argc != 2) {
		printf("사용법 : %s port\n", argv[0]);
		exit(0);
	}

	listen_sock = tcp_listen(INADDR_ANY, atoi(argv[1]), 5);
	while(1){
		FD_ZERO(&read_fds);
		FD_SET(listen_sock, &read_fds);
		for(i=0; i<num_chat; i++)
			FD_SET(clisock_list[i], &read_fds);
		maxfdp1 = getmax() + 1;
		puts("wait for client");
		if(select(maxfdp1, &read_fds, NULL, NULL, NULL)<0)
			errquit("select fail");

		if(FD_ISSET(listen_sock, &read_fds)){
			accp_sock=accept(listen_sock, (struct sockaddr*)&cliaddr, &addrlen);
			if(accp_sock == -1)
				errquit("accept fail");
			addClient(accp_sock, &cliaddr);
			send(accp_sock, START_STRING, strlen(START_STRING), 0);
			printf("%d번째 사용자 추가.\n", num_chat);
		}

		for(i=0; i<num_chat; i++){
			if(FD_ISSET(clisock_list[i], &read_fds)){
				nbyte = recv(clisock_list[i], buf, MAXLINE, 0);
				if(nbyte <=0){
					removeClient(i);
					continue;
				}
				buf[nbyte] = 0;
				if(strstr(buf, EXIT_STRING) != NULL) {
					removeClient(i);
					continue;
				}
				for(j=0; j<num_chat; j++)
					send(clisock_list[j], buf, nbyte, 0);
				printf("%s\n", buf);
			}
		}
	}
	return 0;
}
Example #21
0
double GROUP::calculateUCB(int total)
{
    //ucb_tuned1
    double v = getVariance() + sqrt(2*log(total) / AllUsedNumber);
    double V = (v>0.25) ? 0.25 : v;
    double max = getmax() , min = getmin();
    double newMean;
    if(max == min)
	newMean = 1.0;
    else
	newMean = (getMean() - min ) / (max - min);
    double tmp = newMean - sqrt(log(total) / AllUsedNumber *V);
    return tmp;
}
Example #22
0
int main(int argc, char const *argv[])
{
	link *head = NULL;
	link *curr = NULL;
	int guests,time,value,i;

	printf("enter the number of guests\n");
	scanf("%d",&guests);

    int array[2*guests];

	for (i = 0; i < 2*guests; ++i)
	{
		if (i<guests)
			value = 1;
		else
			value = -1;
		scanf("%d",&time);
		insert(&head,&curr,time,value);
	}
	int times[2*guests];
	int values[2*guests];
	gettimevalues(head,times,values);
	sortarrays(times,values,2*guests);

	for (i = 0; i < 2*guests; ++i)
		printf("%d \t",times[i]);
    printf("\n");

    for (i = 0; i < 2*guests; ++i)
		printf("%d",values[i]);

	changevalues(&head,&curr,times,values);


	for ( i= 0; i < 2*guests; ++i)
		array[i] = 0;

	getarray(&head,&curr,array);

	printf("\n");
	for ( i = 0; i < 2*guests; ++i)
		printf("%d\t",array[i]);

	printf("\n\n%d",getmax(array,2*guests));


	return 0;
}
Example #23
0
void Param ::CheckParam()
{
	if (min>max){
		cout<<"Error in input of parameter: "<<name<<", min>max."<<endl;
		double tempmin=min;
		min=getmin(min,max);
		max=getmax(tempmin,max);
		cout<<"Setting min: "<<min<<", setting max: "<<max<<endl;
	}
	if (min==max){
		cout<<"Error in input of parameter: "<<name<<", min=max."<<endl;
		exit(0);
	}
	ChangeVal(val);
}
Example #24
0
int getmax( struct tree *t)
{   
    if( t == NULL )
       return -1;
       
    if( t->left== NULL && t->right==NULL )
        return t->d ;
    else
    {
        int left=getmax(t->left);
        int right=getmax(t->right);
        int max;
        if( left > right )
            max=left;
        else
            max=right;
        
        if( t->d > max )
            return t->d;
        else
            return max;
        
    }
}
Example #25
0
int main(){

	int i,maxr;
	scanf("%d\n",&N);

	for(i = 0;i < N; i++){
		A[i] = getchar() - 'a';
	}

	maxr = getmax();

	printf("%d",maxr);

	return 0;
}
Example #26
0
int main()
{
	double a, b;
	printf("input 2 float numbers:");
	scanf("%lf%lf", &a, &b);
	double* p;
	getmax(&p, &a, &b);
	printf("max:%f\n", *p);
	//printf("%p\n", &&a);不存在“地址的地址”
	printf("&a=%p, &b=%p\n", &a, &b);
	printf("p=%p, &p=%p\n", p, &p);
	double** q = &p;
	double*** r = &q;//*r==q, **r==*q==p, ***r==**q==*p==大者
	printf("%f\n", ***r);
	return 0;
}
Example #27
0
void GROUP::push(Node n , int curFE)
{
    Node tmp(length);
    LastModifiedFE = curFE;
    tmp.allele = n.allele;
    tmp.fitness = n.fitness;
    tmp.isEvaluated = true;
    nodes.push_back(tmp);

    sum += n.fitness;
    sum2+= n.fitness * n.fitness;

    AllUsedNumber ++ ;
    update_mean();
    max = getmax();
    return ;
}
Example #28
0
int main()
{

    int i,j,k;
    int fron,rear;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        memset(b,0,sizeof(b));
        for(i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
        }
        getmin();
        getmax();
        output();
    }
    return 0;
}
Example #29
0
int main(int argc, char *argv[])
{
    setIO("sample");
    n = gi,m=gi;
    for(int i = 1;i<=n;i++)
     a[i] = gi,p=max(p,a[i]+m+1);
    m++;
    int u = 0;
    for(int i = 1;i<=n;i++)
     for(int j = m;j>=1;j--)
     {
      int ans = getmax(a[i]+j,j);
      u = max(ans+1,u);
      change(a[i]+j,j,ans+1);
     }
    printf("%d\n",u);
    closeIO();
    return EXIT_SUCCESS;
}
Example #30
0
/**
 * Remove a propnode named 'key' from the AVL root 'root'.  This function
 * is recursive.
 *
 * The node (removed from the AVL structure) is returned, or NULL if it
 * was not found.
 *
 * @private
 * @param key the prop name to remove
 * @param root the root node to start the removal process from
 *
 * @return the removed node - you should probably free it with free_propnode
 */
static PropPtr
remove_propnode(char *key, PropPtr * root)
{
    PropPtr save;
    PropPtr tmp;
    PropPtr avl = *root;
    int cmpval;

    save = avl;

    if (avl) {
        cmpval = strcasecmp(key, PropName(avl));

        if (cmpval < 0) {
            save = remove_propnode(key, &(avl->left));
        } else if (cmpval > 0) {
            save = remove_propnode(key, &(avl->right));
        } else if (!(avl->left)) {
            avl = avl->right;
        } else if (!(avl->right)) {
            avl = avl->left;
        } else {
            tmp = remove_propnode(PropName(getmax(avl->left)), &(avl->left));

            if (!tmp) { /* this shouldn't be possible. */
                panic("remove_propnode() returned NULL !");
            }

            tmp->left = avl->left;
            tmp->right = avl->right;
            avl = tmp;
        }

        if (save) {
            save->left = NULL;
            save->right = NULL;
        }

        *root = balance_node(avl);
    }

    return save;
}