HDU 6215 Brute Force Sorting(链表)

http://acm.hdu.edu.cn/showproblem.php?pid=6215

题意:
给出一个序列,对于每个数,它必须大于等于它前一个数,小于等于后一个数,如果不满足,就删去。然后继续去判断剩下的数,直到最后都满足。

思路:

建立双向链表,如果一个数是需要删除的,那么它只会影响它前一个的数和后一个的数,这样只需要把它前面的数保存下来,下次再跑即可。

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstring>
 4 #include<cstdio>
 5 #include<sstream>
 6 #include<vector>
 7 #include<stack>
 8 #include<queue>
 9 #include<cmath>
10 #include<map>
11 #include<set>
12 using namespace std;
13 typedef long long ll;
14 typedef pair<int,ll> pll;
15 const int INF = 0x3f3f3f3f;
16 const int maxn = 100000+5;
17
18 int n ,top;
19 int a[maxn],pre[maxn],nxt[maxn];
20 int que[maxn];
21
22 template <class T>
23 inline void scan_d(T &ret)
24 {
25     char c;
26     ret = 0;
27     while ((c = getchar()) < ‘0‘ || c > ‘9‘);
28     while (c >= ‘0‘ && c <= ‘9‘)
29     {
30         ret = ret * 10 + (c - ‘0‘), c = getchar();
31     }
32 }
33
34 int main()
35 {
36     //freopen("in.txt","r",stdin);
37     int T;
38     scan_d(T);
39     while(T--)
40     {
41         top=0;
42         scan_d(n);
43         for(int i=1;i<=n;i++)
44         {
45             scan_d(a[i]);
46             pre[i]=i-1;
47             nxt[i]=i+1;
48             que[top++]=i;
49         }
50         a[0]=0,a[n+1]=INF;
51         nxt[0]=1,pre[n+1]=n;
52         bool flag = true;
53         int ans = n;
54         while(flag)
55         {
56             int s = 0;
57             flag = false;
58             int now = 0;
59             while(now<top)
60             {
61                 int cnt = 0;
62                 int it = que[now];
63                 while(a[it]>a[nxt[it]])  {cnt++;it=nxt[it];flag=true;}
64                 if(cnt)
65                 {
66                     ans-=(cnt+1);
67                     nxt[pre[que[now]]]=nxt[it];
68                     pre[nxt[it]]=pre[que[now]];
69                     que[s++]=pre[que[now]];;
70                 }
71                 while(que[now]<=it && now<top) now++;
72             }
73             top=s;
74         }
75         printf("%d\n",ans);
76         for(int i=0;nxt[i]!=n+1;i=nxt[i])
77             printf("%d ",a[nxt[i]]);
78         printf("\n");
79     }
80     return 0;
81 }
时间: 2025-01-06 01:43:12

HDU 6215 Brute Force Sorting(链表)的相关文章

HDU 6215 Brute Force Sorting

一层一层删 链表模拟 #include <iostream> #include <cstring> #include <algorithm> #include <cstdio> using namespace std; const int maxn = 1e5 + 7; int que[maxn], a[maxn], pre[maxn], suf[maxn], n, top; int main() { int _; cin >> _; while

HDU - 6215 2017 ACM/ICPC Asia Regional Qingdao Online J - Brute Force Sorting

Brute Force Sorting Time Limit: 1 Sec  Memory Limit: 128 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=6215 Description Beerus needs to sort an array of N integers. Algorithms are not Beerus's strength. Destruction is what he excels. He can destr

HDU 4971 A simple brute force problem.(dp)

HDU 4971 A simple brute force problem. 题目链接 官方题解写的正解是最大闭合权,但是比赛的时候用状态压缩的dp也过掉了- -,还跑得挺快 思路:先利用dfs预处理出每个项目要完成的技术集合,那么dp[i][j]表示第i个项目,已经完成了j集合的技术,由于j这维很大,所以利用map去开数组 代码: #include <cstdio> #include <cstring> #include <algorithm> #include &l

【最小割】HDU 4971 A simple brute force problem.

说是最大权闭合图.... 比赛时没敢写.... 题意 一共有n个任务,m个技术 完成一个任务可盈利一些钱,学习一个技术要花费钱 完成某个任务前需要先学习某几个技术 但是可能在学习一个任务前需要学习另几个任务 求最多能赚多少钱咯 先将缩点将需要一起学掉的技术缩成一个点 建s--任务 权值为该任务盈利多少钱 建技术(缩点后)-t 权值为学习这技术的花费(总) 任务-技术 (完成该任务所需的每个技术都需要建边)权值为INF #include<stdio.h> #include<stdlib.h

HDU 4971 A simple brute force problem.(最小割,最大权闭合图)

http://acm.hdu.edu.cn/showproblem.php?pid=4971 A simple brute force problem. Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 182    Accepted Submission(s): 115 Problem Description There's a com

HDU - 4971 A simple brute force problem. (DP)

Problem Description There's a company with several projects to be done. Finish a project will get you profits. However, there are some technical problems for some specific projects. To solve the problem, the manager will train his employee which may

HDU 4971 A simple brute force problem.

A simple brute force problem. Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on HDU. Original ID: 497164-bit integer IO format: %I64d      Java class name: Main There's a company with several projects to be done. Finish a projec

HDU 4971 A simple brute force problem. 强连通缩点+最大权闭合图

题意: 给定n个项目,m个技术难题 下面一行n个数字表示每个项目的收益 下面一行m个数字表示攻克每个技术难题的花费 下面n行第i行表示 第一个数字u表示完成 i 项目需要解决几个技术难题,后面u个数字表示需要解决的问题标号. 下面m*m的矩阵 (i,j) = 1 表示要解决j问题必须先解决i问题. (若几个问题成环,则需要一起解决) 问:最大收益. 思路: 先给问题缩点一下,每个缩点后的点权就是这个点内所有点权和. 然后跑一个最大权闭合图. #include<stdio.h> #include

HDU 4971 A simple brute force problem. 最大权闭合图

点击打开链接 A simple brute force problem. Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 157    Accepted Submission(s): 99 Problem Description There's a company with several projects to be done. Fi