跳过正文
  1. Posts/

codeforces 479D. Long Jumps

·2 分钟

http://codeforces.com/problemset/problem/479/D

题意是说有一把尺子,本身有一些刻度,然后需要测量x和y,问最少需要添加多少个刻度,如果需要,这些刻度分别添加在什么位置。

一开始没有看清题目,以为答案最多为4,但是发现,a[1]=0 a[n]=l这两个是确定的,所以答案最多为2,

而不会出现中间有两个刻度,无论是往前刻还是往后都会越界的情况。

先去看看已知的刻度里有没有直接满足的。

除此之外,如果在已知的刻度下无法测量x也无法测量y,我们还可以找找是否能有公用点使得只刻一个刻度就可以满足题意。公用点分两种情况,一种是在两个刻度之间,另一种是在两个刻度的同侧。

前一种没什么坑,后一种要判断是否越界!!!

如果在已知的刻度下能测量x或者能测量出y但不能同时测量的话,就没有必要找公用点。

还有就是查找的话如果线性找复杂度是O(n2),会TLE在第27个点。

我们用二分…

话说STL里竟然连二分查找也有。。。。简直orz。。。。真是省时省力。

代码:

 1
 2
 3   /* ***********************************************
 4   Author :111qqz
 5   Created Time :2016年02月22日 星期一 23时42分46秒
 6   File Name :code/cf/problem/479D.cpp
 7   ************************************************ */
 8
 9   #include <iostream>
10   #include <algorithm>
11   #include <cstring>
12   #include <cstdio>
13   #include <cmath>
14
15   using namespace std;
16
17   int n,l,x,y;
18   const int N=1E5+7;
19   int a[N];
20   int ans,p,q;
21   bool flag1,flag2,flag3,flag4;
22
23   int main()
24   {
25       scanf("%d %d %d %d",&n,&l,&x,&y);
26       flag1 = false;
27       flag2 = false;
28       flag3 = false;
29       flag4 = false;
30
31       for ( int i = 1 ; i <= n ; i++ )
32           scanf("%d",&a[i]);
33           ans = 2;
34           p = -1;
35           q = 0;
36       for ( int i = 1 ; i <= n ; i++ )
37       {
38           if(binary_search(a,a+n+1,a[i]+x))
39           {
40               flag1 = true;
41
42           }
43           if (binary_search(a,a+n+1,a[i]+y))
44           {
45               flag2 = true;
46           }
47           if (binary_search(a,a+n+1,a[i]+x+y))
48           {
49               flag3 = true;
50               p = a[i];
51           }
52           if (binary_search(a,a+n+1,a[i]+y-x)&&((a[i]+y<=l)||(a[i]-x>=0)))
53           {
54               flag4 = true;
55               p = a[i];
56           }
57       }
58
59           if ( flag1) {ans--;q = 1 ;}
60           if ( flag2 ){ ans--;q = 2 ;}
61           if ( ans==2&&(flag3||flag4))
62           {
63               ans--;
64           }
65           cout<<ans<<endl;
66       if ( ans==2 )
67       {
68           cout<<a[1]+x<<" "<<a[1]+y<<endl;
69       }
70       if ( ans==1 )
71       {
72           if ( p==-1 )
73           {
74               if ( q==1 )
75                   cout<<a[1]+y<<endl;
76               else cout<<a[1]+x<<endl;
77           }
78           else
79           {
80               if ( p+y<=l )
81               cout<<p+y<<endl;
82               else cout<<p-x<<endl;
83           }
84       }
85       return 0;
86   }

相关文章

codeforces 525 B. Pasha and String

·1 分钟
http://codeforces.com/problemset/problem/525/B 1题意是说一个字符串,进行m次颠倒变换(从a[i]位置到a[l-i+1]位置),问得到的字符串。容易发现,对于越在里边(对称,也就是越靠近中间位置)的字符,调换的次数越多。我们可以把a[i]从小到大排序。然后经过分析发现,把两个相邻的a[i]分为一组,做处理,如果m为奇数,最后还剩下a[m]没有被分组,要单独处理a[m]细节上要注意st数组是从st[0]开始的...好吧的确不方便,适牛也说我了。。数组下标以后还是从0开始吧。。。主要是受高中OI用的pascal的影响。。。那个数组下标随便啊。代码: 2 3 4 5 6 /* *********************************************** 7 Author :111qqz 8 Created Time :2016年02月22日 星期一 23时39分51秒 9 File Name :code/cf/problem/525B.cpp 10 ************************************************ */ 11 12 #include <iostream> 13 #include <algorithm> 14 #include <cstring> 15 #include <cmath> 16 #include <cstdio> 17 18 using namespace std; 19 20 int m,k,len; 21 const int N=2E5+7; 22 int a[N]; 23 char st[N]; 24 25 int main() 26 { 27 cin>>st; 28 scanf("%d",&m); 29 for ( int i = 1 ; i <= m ; i++ ) 30 scanf("%d",&a[i]); 31 sort(a+1,a+m+1); 32 k = 1; 33 len = strlen(st); 34 while (k<=m) 35 { 36 for ( int j = a[k] ; j <= a[k+1]-1 ; j++) 37 swap(st[j-1],st[len-j]); 38 k = k + 2; 39 } 40 if ( m %2==1 ) 41 for ( int i = a[m]; i <= len/2 ; i++ ) 42 swap(st[i-1],st[len-i]); 43 cout<<st<<endl; 44 return 0; 45 }

codeforces 482 A. Diverse Permutation(构造)

·1 分钟
C - C **Time Limit:**1000MS **Memory Limit:**262144KB 64bit IO Format:%I64d & %I64u Submit Status Description Permutation_p_ is an ordered set of integers _p_1, p_2, …, p__n, consisting of n distinct positive integers not larger than n. We’ll denote as_n the length of permutation _p_1, _p_2, …, p__n.

codeforces 47 C. Exams

·1 分钟
http://codeforces.com/problemset/problem/479/C 1/************************************************ 2Author :111qqz 3Created Time :2016年02月22日 星期一 23时31分10秒 4File Name :code/cf/problem/479C.cpp 5************************************************ */ 6 7#include <iostream> 8#include <algorithm> 9#include <cstring> 10#include <cstdio> 11 12#include <cmath> 13 14using namespace std; 15int n,ans; 16const int N=1E4+5; 17int a[N],b[N]; 18 19struct Q 20{int a,b; 21}q[N]; 22 23bool cmp(Q x, Q y) 24{ 25 if ( x.a<y.a) return true; 26 if ( x.a==y.a &&x.b<y.b ) return true; 27 return false; 28} 29 30int main() 31{ 32 scanf("%d",&n); 33 for ( int i = 1 ; i <= n ; i++ ) 34 scanf("%d %d",&q[i].a,&q[i].b); 35 sort(q+1,q+n+1,cmp); 36 37 ans=q[1].b; 38 for ( int i = 2 ; i <= n; i++ ) 39 { 40 if ( q[i].b>=ans ) 41 ans = q[i].b; 42 else ans = q[i].a; 43 } 44 printf("%d\n",ans); 45 return 0; 46}

HDOJ 4882 Loves Codefires

·2 分钟
ZCC Loves Codefires Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 988 Accepted Submission(s): 500 Problem Description Though ZCC has many Fans, ZCC himself is a crazy Fan of a coder, called “Memset137”. It was on Codefires(CF), an online competitive programming site, that ZCC knew Memset137, and immediately became his fan. But why? Because Memset137 can solve all problem in rounds, without unsuccessful submissions; his estimation of time to solve certain problem is so accurate, that he can surely get an Accepted the second he has predicted. He soon became IGM, the best title of Codefires. Besides, he is famous for his coding speed and the achievement in the field of Data Structures. After become IGM, Memset137 has a new goal: He wants his score in CF rounds to be as large as possible. What is score? In Codefires, every problem has 2 attributes, let’s call them Ki and Bi(Ki, Bi>0). if Memset137 solves the problem at Ti-th second, he gained Bi-KiTi score. It’s guaranteed Bi-KiTi is always positive during the round time. Now that Memset137 can solve every problem, in this problem, Bi is of no concern. Please write a program to calculate the minimal score he will lose.(that is, the sum of Ki*Ti).

poj 1065 Wooden Sticks

·2 分钟
Wooden Sticks Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 19008 Accepted: 8012 Description There is a pile of n wooden sticks. The length and weight of each stick are known in advance. The sticks are to be processed by a woodworking machine in one by one fashion. It needs some time, called setup time, for the machine to prepare processing a stick. The setup times are associated with cleaning operations and changing tools and shapes in the machine. The setup times of the woodworking machine are given as follows: (a) The setup time for the first wooden stick is 1 minute. (b) Right after processing a stick of length l and weight w , the machine will need no setup time for a stick of length l’ and weight w’ if l <= l’ and w <= w’. Otherwise, it will need 1 minute for setup. You are to find the minimum setup time to process a given pile of n wooden sticks. For example, if you have five sticks whose pairs of length and weight are ( 9 , 4 ) , ( 2 , 5 ) , ( 1 , 2 ) , ( 5 , 3 ) , and ( 4 , 1 ) , then the minimum setup time should be 2 minutes since there is a sequence of pairs ( 4 , 1 ) , ( 5 , 3 ) , ( 9 , 4 ) , ( 1 , 2 ) , ( 2 , 5 ) . Input