A11218 | Almost Permutation
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Recently Ivan noticed an array $a$ while debugging his code. Now Ivan can't remember this array, but the bug he was trying to fix didn't go away, so Ivan thinks that the data from this array might help him to reproduce the bug.
Ivan clearly remembers that there were $n$ elements in the array, and each element was not less than $1$ and not greater than $n$ . Also he remembers $q$ facts about the array. There are two types of facts that Ivan remembers:
- $1$ $l_{i}$ $r_{i}$ $v_{i}$ — for each $x$ such that $l_{i}<=x<=r_{i}$ $a_{x}>=v_{i}$ ;
- $2$ $l_{i}$ $r_{i}$ $v_{i}$ — for each $x$ such that $l_{i}<=x<=r_{i}$ $a_{x}<=v_{i}$ .
Also Ivan thinks that this array was a permutation, but he is not so sure about it. He wants to restore some array that corresponds to the $q$ facts that he remembers and is very similar to permutation. Formally, Ivan has denoted the $cost$ of array as follows:
, where $cnt(i)$ is the number of occurences of $i$ in the array.
Help Ivan to determine minimum possible $cost$ of the array that corresponds to the facts!
Ivan clearly remembers that there were $n$ elements in the array, and each element was not less than $1$ and not greater than $n$ . Also he remembers $q$ facts about the array. There are two types of facts that Ivan remembers:
- $1$ $l_{i}$ $r_{i}$ $v_{i}$ — for each $x$ such that $l_{i}<=x<=r_{i}$ $a_{x}>=v_{i}$ ;
- $2$ $l_{i}$ $r_{i}$ $v_{i}$ — for each $x$ such that $l_{i}<=x<=r_{i}$ $a_{x}<=v_{i}$ .
Also Ivan thinks that this array was a permutation, but he is not so sure about it. He wants to restore some array that corresponds to the $q$ facts that he remembers and is very similar to permutation. Formally, Ivan has denoted the $cost$ of array as follows:
, where $cnt(i)$ is the number of occurences of $i$ in the array.
Help Ivan to determine minimum possible $cost$ of the array that corresponds to the facts!
输入格式
The first line contains two integer numbers $n$ and $q$ ( $1<=n<=50$ , $0<=q<=100$ ).
Then $q$ lines follow, each representing a fact about the array. $i$ -th line contains the numbers $t_{i}$ , $l_{i}$ , $r_{i}$ and $v_{i}$ for $i$ -th fact ( $1<=t_{i}<=2$ , $1<=l_{i}<=r_{i}<=n$ , $1<=v_{i}<=n$ , $t_{i}$ denotes the type of the fact).
Then $q$ lines follow, each representing a fact about the array. $i$ -th line contains the numbers $t_{i}$ , $l_{i}$ , $r_{i}$ and $v_{i}$ for $i$ -th fact ( $1<=t_{i}<=2$ , $1<=l_{i}<=r_{i}<=n$ , $1<=v_{i}<=n$ , $t_{i}$ denotes the type of the fact).
输出格式
If the facts are controversial and there is no array that corresponds to them, print -1. Otherwise, print minimum possible $cost$ of the array.
输入输出样例
输入 #1
3 0
输出 #1
3
输入 #2
3 1 1 1 3 2
输出 #2
5
输入 #3
3 2 1 1 3 2 2 1 3 2
输出 #3
9
输入 #4
3 2 1 1 3 2 2 1 3 1
输出 #4
-1
暂无题解
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted