相对大小关系
本帖最后由 aimisiyou 于 2021-2-10 15:46 编辑题目如图。可以看成数值序列相对大小关系(相邻三者)的一种简写。如甲>乙>丙,可用(2 1 0)表示;甲>乙 且乙<丙,可用(1 0 1)表示。
本帖最后由 aimisiyou 于 2021-2-10 16:41 编辑
(defun refun (lst1 vvlst)
(setq vvlst (vl-sort (cons lst1 (vl-remove (assoc (car lst1) vvlst) vvlst)) '(lambda (ea eb) (< (car ea) (car eb)))))
)
(defun tfun(lst)
;;;(setq lst '(3 7 6 2 5 8 4) n (length lst))
(setq n (length lst))
(setq lst (mapcar '(lambda (x y) (if (< x y) 1 (if (= x y) 0 -1))) (reverse (cdr (reverse lst))) (cdr lst)))
(setq i -1nlst (mapcar '(lambda (x) (setq i (+ 1 i))) (cons 0 lst)))
(setq sum 0 vlst (cons 0 (mapcar '(lambda (x) (setq sum (+ x sum))) lst)))
(setq vvlst (mapcar 'list nlst vlst (cons 0 lst) (mapcar '(lambda (x) (if (< x 0) nil t)) nlst)))
(while (apply 'or (mapcar 'last vvlst))
(setq plst (car (vl-sort (vl-remove nil (mapcar '(lambda (x) (if (last x) x nil)) vvlst))
'(lambda (ea eb) (< (cadr ea) (cadr eb))))))
(setq vvlst (refun (list (car plst) 0 -1 nil) vvlst))
(setq klst (assoc (car plst) vvlst))
(setq flag1 t flag2 t llst klst rlst klst)
(whileflag1
(setq lnlst (assoc (- (car llst) 1) vvlst))
(if lnlst
(if (last lnlst)
(if (= (caddr llst) 1)
(setq flag1 nil)
(if (= (caddr llst) 0)
(setq vvlst (refun (list (car lnlst) (cadr llst) (caddr lnlst) nil) vvlst) llst (assoc (car lnlst) vvlst))
(setq vvlst (refun (list (car lnlst) (+ 1 (cadr llst)) (caddr lnlst) nil) vvlst) llst (assoc (car lnlst) vvlst))
)
)
(setq flag1 nil)
)
(setq flag1 nil)
)
)
(whileflag2
(setq rnlst (assoc (+ (car rlst) 1) vvlst))
(if rnlst
(if (last rnlst)
(if (= (caddr rnlst) -1)
(setq flag2 nil)
(if (= (caddr rnlst) 0)
(setq vvlst (refun (list (car rnlst) (cadr rlst) (caddr rnlst) nil) vvlst) rlst (assoc (car rnlst) vvlst))
(setq vvlst (refun (list (car rnlst) (+ 1 (cadr rlst)) (caddr rnlst) nil) vvlst) rlst (assoc (car rnlst) vvlst))
)
)
(setq flag2 nil)
)
(setq flag2 nil)
)
)
)
(mapcar 'cadr vvlst)
)_$ (tfun '(180 160 100 150 145 142 138 138 138 140))
(2 1 0 3 2 1 0 0 0 1)
故题目结果为200*10+(2+1+0+3+2+1+0+0+0+1)*100=3000
本帖最后由 aimisiyou 于 2021-2-10 16:33 编辑
找最深的V(底部设为0),向左更新,向右更新;再找余下部分最深的V(底部设为0),向左更新,向右更新;……更新过程中须判断左顶点和右顶点是否已经更新过,更新过就不再处理。 采用字符串处理思路先将含有>=<的字符串分解,分解后的每个小字符串对应着数值序列,再将数值序列按规则合并。
_$ (defun ttfun(lst)
(setq strlst (apply 'strcat (mapcar '(lambda (x y) (if (< x y) "<" (if (= x y) "=" ">"))) (reverse (cdr (reverse lst))) (cdr lst))) valst nil)
(defun f1 (strlst)
(setq str (substr (vl-string-trim "=" strlst) 1 1))
(if (= str ">")
(progn
(setq strlst (vl-string-translate ">=" "10" strlst))
(setq nlst (mapcar '(lambda (x) (- x 48)) (vl-string->list strlst)))
(setq sum (apply '+ nlst) vlst (cons sum (mapcar '(lambda (x) (setq sum (- sum x))) nlst)))
)
(progn
(setq strlst (vl-string-translate "<=" "10" strlst))
(setq nlst (mapcar '(lambda (x) (- x 48)) (vl-string->list strlst)))
(setq sum 0 vlst (cons 0 (mapcar '(lambda (x) (setq sum (+ sum x))) nlst)))
)
)
vlst
)
(defun f2 (lst1 lst2)
(if (= 0 (last lst1) (car lst2))
(setq vlst (append lst1 (cdr lst2)))
(if (>= (last lst1) (car lst2))
(setq vlst (append lst1 (cdr lst2)))
(if (= (cadr (reverse lst1)) (last lst1))
(progn
(setq lst (reverse lst1))
(setq m (car lst) i 0)
(setq lst (cdr lst))
(while (= m (car lst))
(setq i (+ 1 i))
(setq lst (cdr lst))
)
(setq vlst (append(reverse lst) (repeat i (setq lst2 (cons (car lst2) lst2)))))
)
(setq vlst (append (reverse (cdr (reverse lst1))) lst2))
)
)
)
)
(while (/= strlst "")
(setq str (substr (vl-string-trim "=" strlst) 1 1))
(if (= str ">")
(progn
(setq n1 (strlen strlst) sslst strlst)
(setq strlst (vl-string-left-trim ">=" strlst))
(setq valst (cons (substr sslst 1 (- n1 (strlen strlst))) valst))
)
(progn
(setq n1 (strlen strlst) sslst strlst)
(setq strlst (vl-string-left-trim "<=" strlst))
(setq valst (cons (substr sslst 1 (- n1 (strlen strlst))) valst))
)
)
)
(setq lst (mapcar 'f1 (reversevalst)) klst (car lst))
(if (cdr lst)
(foreach ea (cdr lst)
(setq klst (f2 klst ea))
)
(setq klst (car lst))
)
)
TTFUN
_$ (ttfun '(3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4 6 2 6 4 3 3))
(1 0 1 0 1 2 0 2 1 0 1 2 3 0 2 1 0 1 2 0 1 0 2 1 0 0)
_$ {:1_9:}{:1_9:} {:1_9:}{:1_13:} 新年快乐,牛年大吉
页:
[1]