zhousir1991

让开源路人皆知~!
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

数列极差问题

Posted on 2011-05-20 11:35  zhousir1991  阅读(3853)  评论(2编辑  收藏  举报

题目描述:

在黑板上写了N个正整数作成的一个数列,进行如下操作:每一次擦去其中的两个数a和b,然后在数列中加入一个数a*b+1,如此下去直至黑板上剩下一个数,在所有按这种操作方式最后得到的数中,最大的max,最小的为min,则该数列的极差定义为M=max-min。

题目分析:

当看到此题时,我们会发现求max与求min是两个相似的过程。若我们把求解max与min的过程分开,着重探讨求max的问题。下面我们以求max为例来讨论此题用贪心策略求解的合理性。
讨论:

假设经(N-3)次变换后得到3个数:a, b, max' (max'>= a >= b), 其中max'是 (N-2)个数经 (N-3) 次f变换后所得的最大值,此时有两种求值方式,设其所求值分别为Z1 ,Z2 ,则有:Z1=(a*b+1)*max'+1,Z2 =(a*max'+1)*b+1 所以 Z1-Z2 =max'-b>=0 若经 (N-2) 次变换后所得的3个数为:m,a,b   (m >= a >= b)且m不为 (N-2) 次变换后的最大值,即m<max'则此时所求得的最大值为: Z=(a*b+1)*m+1 此时 Z1-Z2 =(a*b+1)(max'-m) > 0 所以此时不为最优解。

所以若使第k(1 <= k <= N-1) 次变换后所得值最大,必使 (k-1) 次变换后所得值最大(符合贪心策略的特点2),在进行第k次变换时,只需取在进行(k-1)次变换后所得数列中的两最小数p,q施加f操作:p←p*q+1,q←∞即可(符合贪心策略特点1),因此此题可用贪心策略求解。讨论完毕。

在求min时,我们只需在每次变换的数列中找到两个最大数p, q施加作用f:p ←p*q+1,q← -∞即可.原理同上

 

 

public class Array_1 {
 
public static int[] maxarray = null;
 
public static int[] minarray = null;
 
public static int num;
 
public static void handle() { //等待调用;
  int min, max, i, j, k, temp, a = 0, b;
  String text_temp
=new String("");
  
for (i = 0; i < num; i++//minarray数组从大到小排序
  {
   
for (k = i, j = i + 1; j < num; j++)
    
if (minarray[k] < minarray[j])
     k 
= j;
   temp 
= minarray[k];
   minarray[k] 
= minarray[i];
   minarray[i] 
= temp;
  }
  
for (i = 0; i < num; i++){
   maxarray[i] 
= minarray[num - i - 1]; //maxarray数组从小到大排序
  }
  
for (i = 1; i < num; i++){
   minarray[i] 
= minarray[i] * minarray[i - 1+ 1;
   System.out.print(minarray[i] 
+ " ");
   text_temp 
= text_temp + Integer.toString(minarray[i]) + "  ";
   
for (int iner= i+1;iner<num;iner++){ 
    System.out.print(minarray[iner] 
+ " ");
    text_temp 
= text_temp + Integer.toString(minarray[iner])+"  ";
   }text_temp 
= text_temp + "  ";
   System.out.print(
"\n");
   text_temp 
= text_temp + "\n";
  }
  text_temp 
= text_temp + "\n";
  System.out.print(
"\n");
  
  min 
= minarray[num - 1];
  
for (i = 0; i < num - 1; i++//还有问题需要修改;
  {
   a 
= 0;
   b 
= 1;
   
for (j = 2; j < num; j++//a,b存放当前数组中最小的2个数
   {
    
if (maxarray[j] < maxarray[a] || maxarray[j] < maxarray[b])
     
if (maxarray[a] < maxarray[b])
      b 
= j;
     
else
      a 
= j;
   }
   maxarray[a] 
= maxarray[a] * maxarray[b] + 1;
   maxarray[b] 
= 999999999;
//  System.out.print(maxarray[a]+" ");
   
  
for (int outer = 0; outer < num; outer++){
   
if(maxarray[outer]!=999999999){
   System.out.print(maxarray[outer] 
+ " ");
   text_temp 
= text_temp + Integer.toString(maxarray[outer])+"  ";
   }
  } 
  text_temp 
= text_temp + "\n"
   System.out.print("\n");
  }System.out.print("\n");
  max 
= maxarray[a];
  
int m = max -min;
  text_temp 
= text_temp + "\n" +"M="+max+"-"+min+"="+m;
  Graph.textArea3.setText(text_temp);
  System.out.println(m);
 }
}
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JButton;
import java.awt.Font;
import javax.swing.JScrollPane;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.net.URL;
public class Graph extends JFrame {
 
private static final long serialVersionUID = 1L;
 
public static String number = null;
 
public static int no;
 
private JPanel contentPane;
 
public static JTextField textField = new JTextField();
 
public static JTextArea textArea1 = new JTextArea();
 
public static JTextArea textArea3 = new JTextArea();
 
private JLabel label_1;
 
public static void main(String[] args) {
  EventQueue.invokeLater(
new Runnable() {
   
public void run() {
    
try {
     Graph frame 
= new Graph();
     frame.setVisible(
true);
    } 
catch (Exception e) {
     e.printStackTrace();
    }
   }
  });
 }
 
class MyEvent implements ActionListener
{
  
public void actionPerformed(ActionEvent e)
   {
    
if (e.getActionCommand().equals("问题描述"))
     {
      URL url
=null;
      
try {
      url 
= new URL(" http://202.202.111.145/~zhousir");
      System.out.println(url.getContent());
      System.out.println(url.getAuthority());
      } 
catch (Exception e1) {
       e1.printStackTrace();
      }
      
try {
       Runtime.getRuntime().exec(
"rundll32 url.dll,FileProtocolHandler "+" http://202.202.111.145/~zhousir" );
       } 
catch (IOException e2) {
       e2.printStackTrace();
      }
     }

   }
}
 
public Graph() {
  setTitle(
"数列极差_贪心算法");
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  setBounds(
100100604380);
  contentPane 
= new JPanel();
  contentPane.setBorder(
new EmptyBorder(5555));
  setContentPane(contentPane);
  contentPane.setLayout(
null);
  {
   JLabel label 
= new JLabel("\u8BF7\u8F93\u5165\u6570\u636E\u4E2A\u6570\uFF1A");
   label.setFont(
new Font("微软雅黑", Font.BOLD, 15));
   label.setBounds(
231016634);
   contentPane.add(label);
  }
  {
//   textField = new JTextField();
   textField.setBounds(234219234);
   textField.setText(
"5");
//   no = Integer.parseInt(textField.getText());
   contentPane.add(textField);
   textField.setColumns(
10);
  }
  {
   label_1 
= new JLabel(
     
"\u8BF7\u4F9D\u6B21\u8F93\u5165\u6570\u636E\uFF1A");
   label_1.setFont(
new Font("微软雅黑", Font.BOLD, 15));
   label_1.setBounds(
238616630);
   contentPane.add(label_1);
  }
  {
   JButton button 
= new JButton("\u95EE\u9898\u63CF\u8FF0");
   button.setFont(
new Font("微软雅黑", Font.BOLD, 14));
   button.setBounds(
2328210734);
   button.addActionListener(
new MyEvent());
   contentPane.add(button);
  }
  {
   JScrollPane scrollPane 
= new JScrollPane();
   scrollPane.setBounds(
23125192147);
   contentPane.add(scrollPane);
   {
//    JTextArea textArea1 = new JTextArea();
    textArea1.setText("1 2 3 4 5");
//    number = textArea1.getText();
    scrollPane.setViewportView(textArea1);
   }
  }
  {
   JButton button 
= new JButton("\u786E\u5B9A");
   button.addActionListener(
new ActionListener() {
    
public void actionPerformed(ActionEvent e) {
     
if (e.getActionCommand().equals("确定")) {
      
// String number = textField.getText();
      Array_1.num = Integer.parseInt(textField.getText());
      System.out.println(Array_1.num);
      
      String[] data 
= textArea1.getText().split(" ");
//      char[] data = number.toCharArray(); // 数组长度出错;
      
// System.out.println('a');
//      System.out.println(data.length);
      System.out.println(textArea1.getText());
      Array_1.maxarray 
= new int[Array_1.num];
      Array_1.minarray 
= new int[Array_1.num];
      
      
for (int i = 0, j = 0; i < data.length; i++, j++) {
       Array_1.maxarray[j] 
= Array_1.minarray[j] = Integer.parseInt(data[i]);
      }
      Array_1.handle();
     }
    }
   });
   button.setFont(
new Font("微软雅黑", Font.BOLD, 14));
   button.setBounds(
1402827534);
   contentPane.add(button);
  }
  {
   JLabel label 
= new JLabel("\u6267\u884C\u8FC7\u7A0B");
   label.setFont(
new Font("微软雅黑", Font.BOLD, 15));
   label.setBounds(
369208415);
   contentPane.add(label);
  }
  {
   JScrollPane scrollPane 
= new JScrollPane();
   scrollPane.setBounds(
24842321273);
   contentPane.add(scrollPane);
   {
    scrollPane.setViewportView(textArea3);
   }
  }
 }
}