首页 > 其他分享 >CSU 1807 最长上升子序列~

CSU 1807 最长上升子序列~

时间:2022-11-09 19:35:40浏览次数:62  
标签:1807 now return int rep 序列 CSU include define


[ ​​Submit​​​][ ​​​Status​​​][ ​​​Web Board​​]

Description


1,p 2,…,p n.



1,p 2,…,p n 互不相同(即 p 1,p 2,…,p n



现在 Bobo 想知道,替换后最长上升子序列的长度恰好为 (n-1) 数列的数量。



Input


输入包含不超过 300 组数据,其中不超过 20 组的 n 超过 100.



5).



1,p 2,…,p n  (0≤p i≤n).



1,p 2,…,p n中非 0 的元素互不相同。





Output


对于每组数据,输出一个整数表示要求的值。


Sample Input

3
0 0 0
4
0 0 0 0
5
1 0 0 4 5

Sample Output

#include<set>
#include<map>
#include<ctime>
#include<cmath>
#include<stack>
#include<queue>
#include<bitset>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#define rep(i,j,k) for (int i = j; i <= k; i++)
#define per(i,j,k) for (int i = j; i >= k; i--)
#define loop(i,j,k) for (int i = j;i != -1; i = k[i])
#define lson x << 1, l, mid
#define rson x << 1 | 1, mid + 1, r
#define ff first
#define ss second
#define mp(i,j) make_pair(i,j)
#define pb push_back
#define pii pair<int,int>
#define in(x) scanf("%d", &x);
using namespace std;
typedef long long LL;
const int low(int x) { return x&-x; }
const double eps = 1e-8;
const int INF = 0x7FFFFFFF;
const int mod = 1e9 + 7;
const int N = 1e5 + 10;
int n, a[N];
LL dp[N];

void init()
{
dp[0] = dp[1] = 0; rep(i, 2, N - 1) dp[i] = dp[i - 1] + 2 * i - 3;
}

int check(int x)
{
int now = 1;
rep(i, 1, n)
{
if (now == a[x]) ++now;
if (i == x) continue;
if (a[i] && a[i] != now) return 0;
++now;
}
return 1;
}

int check(int x, int y)
{
if (x != y - 1) return 0;
rep(i, 1, n)
{
if (i == x || i == y) continue;
if (a[i] && a[i] != i) return 0;
}
return 1;
}

LL get(int x, int y)
{
int l = n + 1, r = 0;
rep(i, 1, n) if (a[i] && a[i] != i) l = min(l, i), r = max(r, i);
rep(i, l, r) if (a[i] && a[i] == i) return 0;
int L = l, R = n - r + 1;
per(i, l, 1) if (a[i] == i) { L = l - i; break; }
rep(i, r, n) if (a[i] == i) { R = i - r; break; }
return 1LL * (L - x) * (R - y);
}

LL get()
{
LL res = 0, now = 0;
rep(i, 1, n) if (a[i]) res += dp[a[i] - now - 1], now = a[i];
return res + dp[n - now];
}

int main()
{
init();
while (scanf("%d", &n) != EOF)
{
int flag = 0, l = 0, r = 0;
rep(i, 1, n)
{
scanf("%d", &a[i]);
if (!a[i]) continue;
if (abs(a[i] - i) > 1) flag = i;
if (a[i] - i == 1) l = i;
if (i - a[i] == 1) r = i;
}
if (flag) { printf("%d\n", check(flag)); continue; }
if (l&&r) { printf("%d\n", check(l, r)); continue; }
if (l || r) { printf("%lld\n", get(!l, !r)); continue; }
printf("%lld\n", get());
}
return 0;
}

标签:1807,now,return,int,rep,序列,CSU,include,define
From: https://blog.51cto.com/u_15870896/5838615

相关文章

  • CSU 1813 盖房子
    DescriptionBobo在ICPCCamp买了一块n×m的土地,其中有些格子是障碍。他想选择两个矩形区域,建造两座房子。很明显,用于盖房子的区域不能包含障碍。同时,两个......
  • CSU 1812 三角形和矩形
    DescriptionBobo有一个三角形和一个矩形,他想求他们交的面积。1,y1,x2,y2,x3,y3,x4,y4 描述。表示三角形的顶点坐标是(x1,y1),(x......
  • CSU 1808 地铁
    Description Bobo居住在大城市ICPCCamp。i 号线,位于站ai,bi 之间,往返均需要花费ti 分钟(即从ai 到bi 需要ti 分钟,从bi 到ai......
  • CSU 1810 Reverse
    Description1 d2…dn1…di-1 dj dj-1…di dj+1 dj+2…dn.Bobowouldliketofind9+7).InputTheinputcontains......
  • CSU 1809 Parenthesis
    Description1 p2…pnai andpbiParenthesissequenceSisbalancedifandonlyif:Sisempty;orthereexists balanced parenthesi......
  • CSU 1804 有向无环图
    DescriptionBobo有一个n个点,m条边的有向无环图(即对于任意点v,不存在从点v开始、点v结束的路径)。为了方便,点用1,2,…,n编号。设count(x,y)表示点x......
  • CSU 1803 2016
    Description 给出正整数n和m,统计满足以下条件的正整数对(a,b)的数量:1.1≤a≤n,1≤b≤m;2.a×b是2016的倍数。Input......
  • 单例模式实现的多种方式、pickle序列化模块、选课系统需求分析等
    目录单例模式实现的多种方式方式一:方式二:方式三方式四pickle序列化模块选课系统需求分析功能提炼选课系统架构设计三层架构选课系统目录搭建选课系统功能搭建单例模式实......
  • pickle序列化模块
    pickle序列化模块优势:能够序列化python中所有的类型缺陷:只能够再python中使用,无法跨语言传输需求:产生一个对象并保存到文件中,取出来还是一个对象classC1:def_......
  • 拓端tecdat|R语言ARIMA、ARIMAX、 动态回归和OLS 回归预测多元时间序列
    当ARIMA模型包括其它​​时间序列​​作为输入变量时,被称为传递函数模型(transferfunctionmodel)、多变量时间序列模型(multivariatetimeseriesmodel)、ARIMAX模型或B......