须求是输入先序中序得出后序,Zhenya则想让其尽量小

intdellist(linklistL,inti){Node*pre,*r;intk;pre=L;k=0;while(pre!=NULLki-1){pre=pre-next;k=k+1;}if(pre-next==NULL){printf(“失败!”);returnERROR;}r=pre-next;pre-next=r-next;free(r);returnOK;}

Given an array with positive and negative numbers, find the maximum
average subarray which length should be greater or equal to given
length k.

F. Financiers Game

求c++大神!帮忙改程序!要求是输入先序中序得出后序,并打印二叉树。
结果例如输入:先序ABCDE,中序BADCE
输出:后序BDECA并打印二叉树
A
|_B
|_C
|_D
|_E
代码如下
#include
#include “stdio.h”
using namespace std;


time limit per test

class BinarytreeNode
{
public:
int data;
BinarytreeNode *left;
BinarytreeNode *right;
BinarytreeNode(int value=0, BinarytreeNode *l=NULL,BinarytreeNode
*r=NULL):data,left,right
{}

思路:

对平均值进行二分。一个数组平均的最大不会超过数组最大值,也不会低于数组最小值。也就是说,这个平均值肯定在数组最大值和最小值的范围区间内。

用前缀和数组判断是否数组中是否有一段长度大于等于k的子数组,平均数大于等于mid。如果存在,就把left=mid,如果不存在就把right=mid。这样不断所有l和r的距离,直到重合。left
就是所能找到的最大平均值。

复杂度:

时间: O(nlog(max_val−min_val))O(nlog(max_val−min_val))

空间:O(n)

2 seconds

};


memory limit per test

BinarytreeNode* createtree(int *in, int *pre, int n)
澳门新浦京8455com,{
if return NULL;
int k=0;
while(pre[0]!=in[k]){k++;}
BinarytreeNode *rootelement=new
BinarytreeNode;//应该不会内存泄露的,程序结束时,对象调用析构函数,自动释放
rootelement->left=createtree(pre+1,in,k);
rootelement->right=createtree(pre+k+1,in+k+1,n-k-1);
return rootelement;

public double maxAverage(int[] nums, int k) {

       double result = 0.0;

        if(nums == null || nums.length < k) {

             return result;

         }

         double l = Integer.MAX_VALUE;

         double r = Integer.MIN_VALUE;

         //find the largest number and smallest number in the array

          for(int i = 0; i < nums.length; i++){

                if(nums[i] < l) {

                      l = nums[i];

                }

                if(nums[i] > r){

                      r = nums[i];

               }

          }

          while(r – l >= 1e-6){

                 double mid = (l + r) / 2.0;

                if(check_valid(nums, mid, k)){

                     l = mid;

                 }else{

                    r = mid;

               }

        }

        return l;

     }

      private boolean check_valid(int nums[], double average, int
k) {

            int n = nums.length;

            double min_pre = 0;

            double[] sum = new double[n + 1];

            sum[0] = 0;

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

             // 原数组中的每一个数减去这个mid

                  sum[i] = sum[i – 1] + nums[i – 1] – average;

                 if (i >= k && sum[i] – min_pre >= 0) {

                       return true;

                 }

                 if (i >= k)  

                         min_pre = Math.min(min_pre, sum[i – k +
1]);

          }

           return false;

  }

512 megabytes

}

input

void printelement(BinarytreeNode *element)
{
cout<data<<“,”;
}

standard input

void postorder(BinarytreeNode *subtree)
{

output

 if(subtree!=NULL) { postorder(subtree->left); postorder(subtree->right); printelement; }

standard output

}

This problem has unusual memory
constraint.

int main()
{
int pre[7]={1,2,4,5,3,6,7};
int in[7]={4,2,5,1,6,3,7};
BinarytreeNode *element=createtree;

At evening, Igor and Zhenya the financiers became boring, so they
decided to play a game. They prepared n papers
with the income of some company for some time periods. Note that the
income can be positive, zero or negative.

postorder;while; return 0;

Igor and Zhenya placed the papers in a row and decided to take turns
making moves. Igor will take the papers from the left side, Zhenya will
take the papers from the right side. Igor goes first and takes 1 or 2 (on his choice) papers
from the left. Then, on each turn a player can take k or k + 1 papers from his
side if the opponent took exactly k papers in
the previous turn. Players can’t skip moves. The game ends when there
are no papers left, or when some of the players can’t make a
move.

}

Your task is to determine the difference between the sum of incomes on
the papers Igor took and the sum of incomes on the papers Zhenya took,
assuming both players play optimally. Igor wants to maximize the
difference, Zhenya wants to minimize it.

Input

The first line contains single positive integer n (1 ≤ n ≤ 4000) — the
number of papers.

相关文章

You can leave a response, or trackback from your own site.

Leave a Reply

网站地图xml地图