我和乘子交替方向法admm_找到最大和交替子序列

我和乘子交替方向法admm

Problem statement:

問題陳述:

Given a sequence of numbers, you have to find the maximum sum alternating subsequence and print the value. A sequence is an alternating sequence when it will be maintain like (increasing) -> (decreasing) ->(increasing) ->(decreasing).

給定一個數字序列,您必須找到最大和交替子序列并打印值 。 當序列將像(增加)->(減少)->(增加)->(減少)一樣被維護時,它是一個交替的序列。

Input:
T Test case
T no. of input string will be given to you.
E.g.
3
2 3 4 8 2 5 6 8
2 3 4 8 2 6 5 4
6 5 9 2 10 77 5
Constrain:
1≤ A[i] ≤50
Output:
Print the value of maximum sum alternating subsequence.

Example

T=3
Input:
2 3 4 8 2 5 6 8 
Output:
22 ( 8+6+8)
Input:
2 3 4 8 2 6 5 4
Output:
20 ( 8+ 2+ 6+ 4)
Input:
6 5 9 2 10 77 5
Output:
98 (5+ 9+ 2+ 77+5)

Explanation with example:

舉例說明:

Let N be the number of elements say, X1, X2, X3, ..., Xn

N為元素的數量,即X1,X2,X3,...,Xn

Let f(a) = the value at the index a of the increasing array, and g(a) = the value at the index a of the decreasing array.

f(a) =遞增數組的索引a處的值, g(a) =遞減數組的索引a處的值。

To find out the maximum sum alternating sequence we will follow these steps,

為了找出最大和交替序列,我們將按照以下步驟操作,

  1. We take two new arrays, one is increasing array and another is decreasing array and initialize it with 0. We start our algorithm with the second column. We check elements that are before the current element, with the current element.

    我們采用兩個新數組,一個是遞增數組,另一個是遞減數組,并將其初始化為0。我們從第二列開始我們的算法。 我們使用當前元素檢查當前元素之前的元素。

  2. If any element is less than the current element then,

    如果任何元素小于當前元素,

    f(indexofthecurrentelement) = max?

    f(當前元素的索引)=max?

  3. If the element is greater than the current element then,

    如果元素大于當前元素,

    g(indexofthecurrentelement) = max?

    g(當前元素的索引)=max?

C++ Implementation:

C ++實現:

#include <bits/stdc++.h>
using namespace std;
int sum(int* arr, int n)
{
int inc[n + 1], dec[n + 1];
inc[0] = arr[0];
dec[0] = arr[0];
memset(inc, 0, sizeof(inc));
memset(dec, 0, sizeof(dec));
for (int i = 1; i < n; i++) {
for (int j = 0; j < i; j++) {
if (arr[j] > arr[i]) {
dec[i] = max(dec[i], inc[j] + arr[i]);
}
else if (arr[i] > arr[j]) {
inc[i] = max(inc[i], dec[j] + arr[i]);
}
}
}
return max(inc[n - 1], dec[n - 1]);
}
int main()
{
int t;
cout << "Test Case : ";
cin >> t;
while (t--) {
int n;
cout << "Number of element : ";
cin >> n;
int arr[n];
cout << "Enter the elements : ";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
cout << "Sum of the alternating sequence : " << sum(arr, n) << endl;
}
return 0;
}

Output

輸出量

Test Case : 3
Number of element : 8
Enter the elements : 2 3 4 8 2 5 6 8
Sum of the alternating sequence : 22
Number of element : 8              
Enter the elements : 2 3 4 8 2 6 5 4
Sum of the alternating sequence : 20
Number of element : 7
Enter the elements : 6 5 9 2 10 77 5
Sum of the alternating sequence : 98

翻譯自: https://www.includehelp.com/icp/find-the-maximum-sum-alternating-subsequence.aspx

我和乘子交替方向法admm

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/378915.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/378915.shtml
英文地址,請注明出處:http://en.pswp.cn/news/378915.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Dojo學習筆記(一):Hello Dojo!

歡迎來到Dojo世界&#xff01;在這篇文章中你將會學習到如何加載Dojo以及探索Dojo的一些核心功能。你還會了解Dojo的基于AMD的模塊架構&#xff0c;探索如何加載額外的模塊來增加功能到您的Web站點或應用程序&#xff0c;并找出在出錯的時如何得到幫助。讓我們開始吧 開始學習D…

轉:我眼中的Visual Studio 2010架構工具

來自&#xff1a;http://www.cnblogs.com/wayfarer/archive/2010/07/30/1788398.html我眼中的Visual Studio 2010架構工具影響架構質量的是構建體系架構的思想、原則、實踐與架構師的經驗&#xff0c;絕不是工具。即使是最優秀的架構工具&#xff0c;也不可能像倚天寶劍一般——…

VMware創建Ubuntu操作系統到網絡配置詳細流程

一、創建虛擬機 Ubuntu下載鏈接 1&#xff0c;看個人需求了&#xff0c;有更高的版本&#xff0c;下載Ubuntu鏡像 2&#xff0c;VMware官網隨便下載即可 3&#xff0c;創建新的虛擬機 4&#xff0c;自定義 5&#xff0c;默認即可 6&#xff0c;稍后安裝操作系統 7&#xf…

djiango配置mysql_數據庫MySQL相關環境配置以及數據庫與Go的連接

Linux下安裝好MySQL后&#xff0c;Windows安裝可視化工具navicatLinux下MySQL與Windows下navicat進行連接:安裝的過程很是揪心&#xff0c;各種查網站、大致把坑都寫了出來&#xff1a;1、在Linux下的mysql語句中&#xff0c;mysql> select host,user,authentication_string…

緩沖文件系統(fopen/fread/fwrite)和非緩沖文件系統(open/read/write)

open&#xff1a;系統調用&#xff0c;返回的是文件描述符&#xff0c;即文件句柄&#xff0c;是文件在文件描述副表里的索引。 fopen&#xff1a;C語言庫函數&#xff0c;返回的是一個指向文件結構的指針。fopen是ANSI C標準中的C語言庫函數&#xff0c;在不同的操作系統中應…

java 繼承示例_Java中的繼承類型以及示例

java 繼承示例Prerequisite: Inheritance and its implementation in Java 先決條件&#xff1a; 繼承及其在Java中的實現 Java中的繼承類型 (Type of inheritance in Java) In Java programming, there are following types of the inheritances, 在Java編程中&#xff0c;有…

基于HtmlParser的網絡爬蟲

一、 目標 獲取網頁中的超鏈接及鏈接名&#xff0c;如從http://www.hao123.com/開始&#xff0c;抓取所有hao123鏈接到的超鏈接&#xff0c;再以獲取到的鏈接網頁為目標&#xff0c;獲取它所鏈接到的網頁。 二、環境及開發工具 環境&#xff1a;Java 工具&#xff1a;MyEclip…

VMware下Ubuntu無法全屏顯示問題

一、運行Ubuntu的時候無法全屏顯示&#xff0c;如圖所示下載VMware Tools 二、之后將下載的文件拷貝到home文件夾下 三、解壓該壓縮包 由于該壓縮包是.tar.gz結尾的故壓縮命令&#xff1a;tar -zxvf VMwareTools-10.2.5-8068393.tar.gz&#xff0c;當然各版本有可能不一樣&am…

AMQP RabbitMQ

轉載&#xff1a;http://blog.ftofficer.com/2010/03/translation-rabbitmq-python-rabbits-and-warrens/官方介紹&#xff1a;http://www.rabbitmq.com/erlang-client-user-guide.html開始吧AMQP當中有四個概念非常重要&#xff1a;虛擬主機&#xff08;virtual host&#xff…

fsync與fflush的關系和區別

read/write/fsync與fread/fwrite/fflush的關系和區別 read/write/fsync&#xff1a; linux底層操作&#xff1b; 內核調用&#xff0c; 涉及到進程上下文的切換&#xff0c;即用戶態到核心態的轉換&#xff0c;這是個比較消耗性能的操作。 fread/fwrite/fflush&#xff1a;…

lumanager mysql密碼_LuManager單獨安裝mysqli

首先確定你正在使用的php版本以及php.ini的位置&#xff0c;LuManager自帶了幾個版本。如果是默認安裝&#xff0c;應該是5.2.17。php.ini的位置應該是在/usr/local/php_fcgi/lib/php.ini要確定這些信息&#xff0c;可以自己編寫一個 info.phpphpinfo();?>把文件存放到網站…

數據庫系統數據庫管理系統_數據庫管理系統介紹

數據庫系統數據庫管理系統數據庫 (Database) A database is a collection of related data. In database any user can efficiently access the data which users want to retrieve. It can be anything from a simple collection of roll numbers, names, addresses and phone…

vba將select的值直接賦給變量

strSql ""strSql strSql & " select max(number) from dbo.#DATA" & vbCrLfrss.Open strSql, cnn numb rss.Fields(0)rss.Close轉載于:https://www.cnblogs.com/zigewb/archive/2013/02/06/2900645.html

set_exception_handler 自定義異常處理

剛才已經說過了set_error_handler這個函數&#xff0c;作用就是自定義錯誤處理&#xff0c; 那么現在就來簡單的說一下set_exception_handler&#xff0c;看名字我們就能發現&#xff0c;這說的是自定義異常處理。 呵呵&#xff0c;我聰明吧&#xff1f;來&#xff0c;先看一下…

如何獲取ubuntu源碼包里面的源碼進行編譯

如何獲取ubuntu源碼包里面的源碼進行編譯 1、在獲取源碼包之前&#xff0c;確保在軟件源配置文件 /etc/apt/sources.list中添加了deb-src項 2、使用如下命令獲取xxx源碼包的詳細信息: sudo apt-cache showsrc xxx 這用來查詢當前鏡像站點中是否有該源碼包。 3、源碼包中通常…

python 示例_帶有示例的Python字典popitem()方法

python 示例字典popitem()方法 (Dictionary popitem() Method) popitem() method is used to remove random/last inserted item from the dictionary. popitem()方法用于從字典中刪除隨機/最后插入的項目。 Before the Python version 3.7, it removes random item and from …

優化算法的意義,之二。

前一篇分析了求質數的兩個算法&#xff0c;在代碼執行效率和系統開銷兩方面進行了比較。 這在通信系統的設計和實現中&#xff0c;是非常重要的兩點。因為需要同時面對的是巨大的用戶群&#xff0c;和復雜的業務應用&#xff0c;通信系統的設計經常要面臨魚與熊掌間的選擇。 用…

srs配置文件分析

配置文件中的每一項都是一個SrsConfDirective對象。 例子&#xff1a;vhost 1、 整個vhost 是一個SrsConfDirective對象。 1.1、名字&#xff1a;std::string name vhost 1.2、參數&#xff1a;std::vectorstd::string args第0個值 defaultVhost 1.3、子SrsConfDirective&a…

寄存器(CPU工作原理)03 - 零基礎入門學習匯編語言08

第二章&#xff1a;寄存器&#xff08;CPU工作原理&#xff09;03 讓編程改變世界 Change the world by program 物理地址 CPU訪問內存單元時要給出內存單元的地址。所有的內存單元構成的存儲空間是一個一維的線性空間。 我們將這個唯一的地址稱為物理地址。 16位結構的CPU…