ural 1273. Tie

1273. Tie

Time limit: 1.0 second
Memory limit: 64 MB

The subway constructors are not angels. The work under the ground and… Well, they are not angels. And where have you seen angels? It is all in a lifetime! Show me first somebody who has never… and then… all of us are people. And Vasya and me, too. May be we’ve overdrunked ourselves. But a little. And the ties lie crookedly… At that time they seemed to lie straight. No, we can’t say that it must be so — criss-cross, but not all of them criss-cross! Some of the ties lie almost properly… Crookedly you say? And I’d say normally… After the yesterday’s party? May be, may be… The ties that lie criss-cross we’ll take away and it’ll be OK, the train will pass on term, not by this New Year but by the next one. There’s not much to disjoint. We’ll pull out this tie and may be that one. Next to nothing! One, two, three…

Rails are two parallel straight lines that are for the users’ accommodation parallel to the Y axis and have the coordinates X=0 and X=1. The “pell-mell” ties are arbitrary segments with the vertices on the rails in the integer points of the coordinate scale. At the first elimination of defects step you are to remove several ties that would disappear all the crossings. And, of course, after the yesterday’s party the less you work the better, so you are to remove the minimal possible number of ties.

Input

The first line contains integer K (0 ≤ K ≤ 100) — the number of laid ties. Then there are Klines, each of them contains two integers Y1 and Y2 that describe the location of the next in turn tie — the tie described by the pair Y1 and Y2 connects the points (0, Y1) и (1, Y2). The absolute values of the numbers Y1 and Y2 don’t exceed 1000. There are no identical among the numbers Y1 and among the numbers Y2.

Output

the minimal number of ties that are to be removed in order to eliminate crossings.

Sample

input output
3
0 1
3 0
1 2
1

Problem Author: Magaz Asanov (prepared Leonid Volkov)
Problem Source: Ural State University championship, October 25, 2003

Tags: none  (hide tags for unsolved problems)

Difficulty: 478

题意:两排点,两两连边,问最多多少不相交。

分析:比较裸Dp,或者最长上升子序列

  1 /**
  2 Create By yzx - stupidboy
  3 */
  4 #include <cstdio>
  5 #include <cstring>
  6 #include <cstdlib>
  7 #include <cmath>
  8 #include <deque>
  9 #include <vector>
 10 #include <queue>
 11 #include <iostream>
 12 #include <algorithm>
 13 #include <map>
 14 #include <set>
 15 #include <ctime>
 16 #include <iomanip>
 17 using namespace std;
 18 typedef long long LL;
 19 typedef double DB;
 20 #define For(i, s, t) for(int i = (s); i <= (t); i++)
 21 #define Ford(i, s, t) for(int i = (s); i >= (t); i--)
 22 #define Rep(i, t) for(int i = (0); i < (t); i++)
 23 #define Repn(i, t) for(int i = ((t)-1); i >= (0); i--)
 24 #define rep(i, x, t) for(int i = (x); i < (t); i++)
 25 #define MIT (2147483647)
 26 #define INF (1000000001)
 27 #define MLL (1000000000000000001LL)
 28 #define sz(x) ((int) (x).size())
 29 #define clr(x, y) memset(x, y, sizeof(x))
 30 #define puf push_front
 31 #define pub push_back
 32 #define pof pop_front
 33 #define pob pop_back
 34 #define ft first
 35 #define sd second
 36 #define mk make_pair
 37 inline void SetIO(string Name)
 38 {
 39     string Input = Name+".in",
 40     Output = Name+".out";
 41     freopen(Input.c_str(), "r", stdin),
 42     freopen(Output.c_str(), "w", stdout);
 43 }
 44
 45
 46 inline int Getint()
 47 {
 48     int Ret = 0;
 49     char Ch = ‘ ‘;
 50     bool Flag = 0;
 51     while(!(Ch >= ‘0‘ && Ch <= ‘9‘))
 52     {
 53         if(Ch == ‘-‘) Flag ^= 1;
 54         Ch = getchar();
 55     }
 56     while(Ch >= ‘0‘ && Ch <= ‘9‘)
 57     {
 58         Ret = Ret * 10 + Ch - ‘0‘;
 59         Ch = getchar();
 60     }
 61     return Flag ? -Ret : Ret;
 62 }
 63
 64 const int N = 110;
 65 typedef pair<int, int> II;
 66 int n;
 67 II Data[N];
 68 int Arr[N], Dp[N];
 69
 70 inline void Input()
 71 {
 72     scanf("%d", &n);
 73     For(i, 1, n) scanf("%d%d", &Data[i].ft, &Data[i].sd);
 74 }
 75
 76 inline void Solve()
 77 {
 78     sort(Data + 1, Data + 1 + n);
 79     For(i, 1, n) Arr[i] = Data[i].sd;
 80     For(i, 1, n)
 81     {
 82         Dp[i] = 1;
 83         For(j, 1, i - 1)
 84             if(Arr[j] < Arr[i])
 85                 Dp[i] = max(Dp[i], Dp[j] + 1);
 86     }
 87
 88     int Ans = 0;
 89     For(i, 1, n) Ans = max(Ans, Dp[i]);
 90     printf("%d\n", n - Ans);
 91 }
 92
 93 int main()
 94 {
 95     #ifndef ONLINE_JUDGE
 96     SetIO("I");
 97     #endif
 98     Input();
 99     Solve();
100     return 0;
101 }

时间: 2024-08-03 20:44:13

ural 1273. Tie的相关文章

Ural 1741 Communication Fiend(隐式图+虚拟节点最短路)

1741. Communication Fiend Time limit: 1.0 second Memory limit: 64 MB Kolya has returned from a summer camp and now he's a real communication fiend. He spends all his free time on the Web chatting with his friends via ICQ. However, lately the protocol

URAL 2072 Kirill the Gardener 3

URAL 2072 思路: 1.状压dp+记忆化搜索 代码: #include<bits/stdc++.h> using namespace std; #define ll long long #define pb push_back #define mem(a,b) memset(a,b,sizeof(a)) const int INF=0x3f3f3f3f; int dp[(1<<20)+5]; int a[25]; int n; int dfs(int sum,int sta

URAL 1029 Ministry

URAL 1029 思路: dp+记录路径 状态:dp[i][j]表示到(i,j)这个位置为止的最少花费 初始状态:dp[1][i]=a[1][i](1<=i<=m) 状态转移:dp[i][j]=a[i][j]+max(dp[i-1][j],dp[i][j-1],dp[i][j+1])(注意扫的方向不同) 数组记录上一次的坐标,最后递归输出 代码: #include<bits/stdc++.h> using namespace std; #define ll long long #

Ural 1081 Binary Lexicographic Sequence(DP)

题目地址:Ural 1081 先用dp求出每个长度下的合法序列(开头为1)的个数.然后求前缀和.会发现正好是一个斐波那契数列.然后每次判断是否大于此时长度下的最少个数,若大于,说明这一位肯定是1,若小于,则肯定是0.就这样不断输出出来即可. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #in

URAL 1684. Jack&#39;s Last Word KMP

题目来源:URAL 1684. Jack's Last Word 题意:输入a b 把b分成若干段 每一段都是a的前缀 思路:b为主串 然后用a匹配b 记录到b的i位置最大匹配的长度 然后分割 分割的时候要从后往前 如果a = abac b = abab 那么如果从前往后 首先覆盖了aba 然后b就不能覆盖了 从后往前就可以了 首先覆盖ab 下一次还是ab 因为已经记录了到i位置的最大匹配长度 根据长度从末尾倒退 每次倒退的时候只要是最大的匹配的长度 因为如果在某一次的递推 记录的最大匹配的前缀

关于ios::sync_with_stdio(false);和 cin.tie(0)加速c++输入输出流

原文地址:http://www.hankcs.com/program/cpp/cin-tie-with-sync_with_stdio-acceleration-input-and-output.html http://www.clanfei.com/2012/03/235.html 在网上查看别人的ACM代码时,发现别人输入输出语句用的总是scanf与printf,有点不解,还以为他们用的都是C语言,而非C++,但今天做的一道题(Sort): 发现与网上的其他高手使用完全相同的方法,使用sca

CDOJ 1273 God Qing&#39;s circuital law

暴力枚举+idea.做的时候mod写错了,写成了1000000009,找了两个多小时才发现...... a[1],a[2],a[3]....a[N] b[1],b[2],b[3]....b[N] 首先需要枚举b[1]...b[N]与a[1]进行组合. 然后对a[2]...a[N]从小到大排序 对b[1],b[2],b[3]....b[N] 除当前与a[1]组合的以外,剩下的从大到小排序 然后找出每一个a[i]在不破坏a[0]最大值的情况下最大能与哪一个b[i]配对. 然后从第N个人开始往第2个人

使用std::ios::tie与std::ios_base::sync_with_stdio加速流

std::ios_base::sync_with_stdio static bool sync_with_stdio( bool sync = true ); 与cstdio流[静态]切换同步 打开或关闭所有的标准iostream流与它们对于的标准C流之间的同步. 实际上,这意味着C++和C流使用相同的缓冲区,因此,可以自由地混合使用流.同步C++标准流可以确保线程安全. 默认情况下,iostream对象和cstdio流同步.如果同步关闭,C++标准流独立地缓冲输入输出,在某些情况下,这是相当快

ural 1272. Non-Yekaterinburg Subway

1272. Non-Yekaterinburg Subway Time limit: 1.0 secondMemory limit: 64 MB A little town started to construct a subway. The peculiarity of the town is that it is located on small islands, some of them are connected with tunnels or bridges. The mayor is