A solution to the life of a 1229 battery

Insert picture description here

Analysis:
This question uses the greedy algorithm.

Personal idea one (it is just an idea that the timeout cannot be passed):
use a array to record the life of each battery.
What I thought at the beginning was to find the maximum value and the second largest value of lifespan each time. (It can be sorted every time, but the time complexity is very high) Each time the maximum value and the next largest value are subtracted by 0.5. (Because 0.5 is a factor for each battery) until the next largest value found is 0. ——The purpose is to keep as many batteries as possible, so every time you use a battery with a large remaining life.

Idea two:
change the way, start with the results.
Find the life and sum of all batteries.
If sum (and)-max (maximum value) <= max, it means that other batteries have been consumed and cannot (or just) offset the life of max, then the result is the limit of the sum of small battery life, sum-max.
If sum (max)-max (maximum value)> max, it means that the value of n must be> = 3. (Only three or more can occur in this case), in this case, the battery can be exchanged halfway , So each battery can be an integer, even multiple of 0.5 hours. For these even numbers, it must be coordinated (can be seen as 0.5 of two different batteries consumed each time), so that all batteries are exhausted. So the result is sum / 2;

Code:
Idea One (Timeout code cannot be over)

#include<iostream>
#include<algorithm>
#include<stdio.h>
using namespace std;

//思路:公约数 半个小时。 
//尽量选取两个较大的数的半个小时,保证有更多的没有用完的电池 

double r[1001];
double a[1001]; 
int m_sort(int s,int t)//归并排序
{
 if(s==t)
  return 0;
 int mid=(s+t)/2;
 m_sort(s,mid);
 m_sort(mid+1,t);
 
 int i=s,j=mid+1,k=s;
 while(i<=mid&&j<=t)
 {
  if(a[i]<=a[j])
  {
   r[k]=a[i];
   k++;
   i++;
  }
  else
  {
   r[k]=a[j];
   k++;
   j++;
  }
  
 }
 while(i<=mid)
 {
  r[k]=a[i];
  k++;
  i++;
 }
 while(j<=t)
 {
  r[k]=a[j];
  k++;
  j++;
 }
 
 for(int i=s;i<=t;i++)
  a[i]=r[i];
}


int main()
{
 int n;
 int nm=0;//记录可以用几个半个小时 
 int max_z,max_ci; 
 while(scanf("%d",&n)!=EOF)
 {
  nm=0; 
  for(int i=1;i<=n;i++)
  {
   cin>>a[i];
  }
  a[0]=0;
  m_sort(1,n);//排序 
  max_z=n;
  max_ci=n-1;//每次找最大和次大的两个值 
  while(a[max_ci]!=0)//直到第二大的那个为0 ,就剩1节电池或者没有电池了为止 
  {
   nm++;
   a[max_z]=a[max_z]-0.5;
   a[max_ci]=a[max_ci]-0.5;
   max_z=max_ci=0;//每次的初始化 
   for(int i=1;i<=n;i++)//先找最大值 
   {
    if(a[i]>a[max_z])
     max_z=i;
   }
   for(int i=1;i<=n;i++)//再找次大值 
   {
    if(a[i]>a[max_ci]&&max_z!=i)
     max_ci=i; 
   }
  }
  printf("%.1f\n",(float)nm/2);
 }
 return 0;
 } 

Code 2: You can pass

#include<iostream>
#include<algorithm>
#include<stdio.h>
using namespace std;
//思路:
//求一下sum总和,如果sum减去最大值小于最大值max就说明没有什么组合可以更大的抵消最大值,结果就是其他值轮番消耗最大值,小电池的和的极限,即sum-max。 
//第二种情况是sum减去最大值大于了大值,这种情况的结果是sum的一半。因为这样n一定会>=3,当>=3时就可以出现换电池的情况,因为每一个电池都是0.5的偶数倍,所以一定可以协调全部用完 
double r[1001];
double a[1001]; 
int m_sort(int s,int t)//归并排个序 
{
 if(s==t)
  return 0;
 int mid=(s+t)/2;
 m_sort(s,mid);
 m_sort(mid+1,t);
 
 int i=s,j=mid+1,k=s;
 while(i<=mid&&j<=t)
 {
  if(a[i]<=a[j])
  {
   r[k]=a[i];
   k++;
   i++;
  }
  else
  {
   r[k]=a[j];
   k++;
   j++;
  }
  
 }
 while(i<=mid)
 {
  r[k]=a[i];
  k++;
  i++;
 }
 while(j<=t)
 {
  r[k]=a[j];
  k++;
  j++;
 }
 
 for(int i=s;i<=t;i++)
  a[i]=r[i];
}


int main()
{
 int n;
 double sum=0;
 while(cin>>n)
 {
  sum=0;
  for(int i=1;i<=n;i++)
  {
   cin>>a[i];
   sum+=a[i];//求和 
  }
 // cout<<sum<<endl;
  m_sort(1,n);//排一下序
  double max_s=a[n];//求出最大值 
  if(sum-max_s<=max_s)//第一种 
   printf("%.1lf\n",sum-max_s);
  else if(sum-max_s>max_s)//第二种 
   printf("%.1lf\n",sum/2);
  
 }
 
 return 0;
}

————————————————
Copyright Statement: This article is an original article of CSDN blogger "Listen to the Bamboo Wind", which follows the CC 4.0 BY-SA copyright agreement. Please attach the original source link and reprint This statement.
Original link: https://blog.csdn.net/qq_40575034/article/details/103338672

Posted 33 original articles · liked 0 · visits 167

Guess you like

Origin blog.csdn.net/weixin_42790071/article/details/105430215