↓ Skip to main content
  1. Posts/

codeforces edu #29 E. Turn Off The TV (思维,乱搞)

·737 words·2 mins
Note: This article is available in Chinese only. 本文暂无英文版本。 View original

题目链接

题意:有若干线段,给出起点和终点,问是否有一个线段是冗余的。冗余的意思是说,对于该线段所覆盖的所有整数点,没有该线段,也能被其他一个或者多个线段覆盖到。如果有,输出任意一个冗余线段即可。

思路:画画图? 显然可以按照第一关键字左端点升序,第二关键字右端点降序(降序是为了处理 n=2,[1,2],[1,3] 这样的case容易一些),先考虑2种最简单的情况。

第一种是a[i+1]完全被a[i]包裹在里面,准确得说不一定是a[i],而是之前所有线段的最大右端点的那条线段,此时a[i+1]就是冗余的线段。

第二种是a[i+1]的左端点在之前所有线段的最大右端点右边,此时没有冗余,继续进行。

接下来考虑比较复杂的相交情况,我们画图发现,当前线段是否冗余,只与a[i-1]和a[i+1]有关。

如果第i条线段的左端点,不超过第i-2条线段的右端点的右边一个位置,此时第i-1条线段就是冗余的。

wa了2次。。原因是没认真看题,l,r的范围的最小值是从0开始而不是1。所以总体来说是道水题(调教场的题这么水了么。。。

代码实现
 1/* ***********************************************
 2Author :111qqz
 3Created Time :2017年09月25日 星期一 02时57分03秒
 4File Name :E.cpp
 5************************************************ */
 6
 7#include <cstdio>
 8#include <cstring>
 9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19#define PB push_back
20#define fst first
21#define sec second
22#define lson l,m,rt<<1
23#define rson m+1,r,rt<<1|1
24#define ms(a,x) memset(a,x,sizeof(a))
25typedef long long LL;
26#define pi pair < int ,int >
27#define MP make_pair
28
29using namespace std;
30const double eps = 1E-8;
31const int dx4[4]={1,0,0,-1};
32const int dy4[4]={0,-1,1,0};
33const int inf = 0x3f3f3f3f;
34const int N=2E5+7;
35struct Node
36{
37    int l,r;
38    int id;
39}a[N];
40int n;
41int ans;
42vector<int>ret;
43bool cmp( Node x, Node y)
44{
45    if (x.l==y.l) return x.r>y.r;
46    return x.l<y.l;
47}
48int main()
49{
50    #ifndef  ONLINE_JUDGE
51    freopen("./in.txt","r",stdin);
52  #endif
53    cin>>n;
54    for ( int i =  1 ; i <= n ; i++)
55    {
56        scanf("%d %d",&a[i].l,&a[i].r);
57        a[i].id = i;
58    }
59    sort(a+1,a+n+1,cmp);
60    ans = 0 ;
61    int mxR=-1;
62    int lsR=-1;
63    for ( int i = 1 ; i <= n ; i++)
64    {
65        //puts("miao");
66        if (i>=3) lsR=a[i-2].r;
67        if (a[i].r<=mxR)
68        {
69        ans++;
70        ret.push_back(a[i].id);
71        }
72        else if (a[i].l>=mxR+1) //先考虑最easy的2个情况.
73        {
74        mxR=max(mxR,a[i].r);
75        }else if (lsR!=-1&&a[i].l<=lsR+1&&a[i].r)
76        {
77        ans++;
78        ret.push_back(a[i-1].id);
79        }
80
81        mxR = max(mxR,a[i].r);
82    }
83    if (ans==0) ans=-1;
84    else ans = ret[0];
85    printf("%d\n",ans);
86
87
88
89
90  #ifndef ONLINE_JUDGE
91  fclose(stdin);
92  #endif
93    return 0;
94}

Related

BZOJ 1637: [Usaco2007 Mar]Balanced Lineup (前缀和乱搞)

·1332 words·3 mins
1637: [Usaco2007 Mar]Balanced Lineup # Time Limit: 5 Sec Memory Limit: 64 MB Submit: 503 Solved: 336 [Submit][Status][Discuss] Description # Farmer John 决定给他的奶牛们照一张合影,他让 N (1 ≤ N ≤ 50,000) 头奶牛站成一条直线,每头牛都有它的坐标(范围: 0..1,000,000,000)和种族(0或1)。 一直以来 Farmer John 总是喜欢做一些非凡的事,当然这次照相也不例外。他只给一部分牛照相,并且这一组牛的阵容必须是“平衡的”。平衡的阵容,指的是在一组牛中,种族0和种族1的牛的数量相等。 请算出最广阔的区间,使这个区间内的牛阵容平衡。区间的大小为区间内最右边的牛的坐标减去最左边的牛的坐标。 输入中,每个种族至少有一头牛,没有两头牛的坐标相同。

codeforces #346 div 2 C. Tanya and Toys (暴力乱搞)

·613 words·2 mins
题目链接 题意:有1E9个礼物,第i个礼物价钱是i,然后现在已经有n个不重复的礼物,a[i],m元钱,想尽可能多得买不同种类的礼物,还能买多少个。 思路:先不考虑已经买的,从1连续买到k,然后考虑在这个区间内已经买的,等于实际上没有花钱。 反正就是暴力搞啊搞啊。。我也不知道怎么搞。。 结果最后。。方案数为0的时候。。。最后一个答案我是单独输出的。。忘了判断了。。。所以会输出两个0.宝宝心里苦啊。。。。。。。。

codeforces 484 B Maximum Value (暴力乱搞)

·504 words·2 mins
题目链接 题意:给出n个元素的序列,求出最大的a[i]%a[j] (i>=j) 思路:没思路。。。。 Let us iterate over all different a__j. Since we need to maximize , then iterate all integer x (such_x_ divisible by a__j) in range from 2_a__j_ to M, where M — doubled maximum value of the sequence. For each such x we need to find maximum a__i, such a__i < x. Limits for numbers allow to do this in time O(1) with an array. After that, update answer by value . Total time complexity is O(nlogn + MlogM)

codeforces #322 div 2 C. Developing Skills(乱搞)

·1107 words·3 mins
time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Petya loves computer games. Finally a game that he’s been waiting for so long came out! The main character of this game has n different skills, each of which is characterized by an integer a__i from 0 to 100. The higher the number_a__i_ is, the higher is the i-th skill of the character. The total rating of the character is calculated as the sum of the values of for all i from 1 to n. The expression ⌊ x⌋ denotes the result of rounding the number x down to the nearest integer.