热身题
Posted ymir-taomee
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了热身题相关的知识,希望对你有一定的参考价值。
1.三角形
- 题目大意:n根棍子,棍子i的长度ai,选出3根棍子组成周长尽可能长的三角形,输出最大周长,无法组成三角形则输出0
- 限制条件:3≤n≤100,1≤ai≤106
- 做法1:很自然的可以想到穷举所有的方案,复杂度是O(n3)的,n的限制条件1s足够,这里介绍一种O(nlogn)的做法
- 做法2:将棍子按长度从大到小进行排序,判断长度最长的三根棍子能否构成三角形,如果可以则找到满足要求的解,相加即得到最大周长,如果不能构成三角形,说明最长的棍子不能与其他棍子构成三角形,丢弃它,在剩下的棍子里再进行判断,这样可以把复杂度控制在O(nlogn)+O(n)
- 代码:
1 #include <iostream> 2 #include <ctime> 3 #define random(l,r) (rand()%(r-l+1)+l) 4 using namespace std; 5 6 int n; 7 int * a; 8 9 void qsort(int, int); 10 void swap(int &, int &); 11 12 int main(int argc, char * argv[]) 13 { 14 bool flag = false; 15 srand((unsigned)time(NULL)); 16 cin >> n; 17 a = new int[n]; 18 for (int i=0; i<n; i++) 19 { 20 cin >> a[i]; 21 } 22 qsort(0,n-1); 23 for (int i=0; i<n-2; i++) 24 { 25 if (a[i]<a[i+1]+a[i+2]) 26 { 27 cout << a[i]+a[i+1]+a[i+2] << endl; 28 flag = true; 29 break; 30 } 31 } 32 if (!flag) cout << 0 << endl; 33 } 34 35 void qsort(int l, int r) 36 { 37 int i=l; 38 int j=r; 39 int mid=a[random(l,r)]; 40 do 41 { 42 while (a[i]>mid) i++; 43 while (a[j]<mid) j--; 44 if (i<=j) swap(a[i++],a[j--]); 45 } 46 while (!(i>j)); 47 if (l<j) qsort(l,j); 48 if (i<r) qsort(i,r); 49 } 50 51 void swap(int &x, int &y) 52 { 53 int t=x; 54 x=y; 55 y=t; 56 }
2.Ants (POJ1852)
- 原题如下:
Ants
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 24129 | Accepted: 9652 |
Description
An army of ants walk on a horizontal pole of length l cm, each with a constant speed of 1 cm/s. When a walking ant reaches an end of the pole, it immediatelly falls off it. When two ants meet they turn back and start walking in opposite directions. We know the original positions of ants on the pole, unfortunately, we do not know the directions in which the ants are walking. Your task is to compute the earliest and the latest possible times needed for all ants to fall off the pole.
Input
The first line of input contains one integer giving the number of cases that follow. The data for each case start with two integer numbers: the length of the pole (in cm) and n, the number of ants residing on the pole. These two numbers are followed by n integers giving the position of each ant on the pole as the distance measured from the left end of the pole, in no particular order. All input integers are not bigger than 1000000 and they are separated by whitespace.
Output
For each case of input, output two numbers separated by a single space. The first number is the earliest possible time when all ants fall off the pole (if the directions of their walks are chosen appropriately) and the second number is the latest possible such time.
Sample Input
2 10 3 2 6 7 214 7 11 12 7 13 176 23 191
Sample Output
4 8 38 207
- 题解:
- 最短时间很简单,所有蚂蚁往较近的一端走即可,这种情况下也不会发生两只蚂蚁相遇的情况,最短时间就是每只蚂蚁所走最短时间的最大值
- 对于最长时间,我们需要考虑两只蚂蚁相遇的情况,而事实上,两只蚂蚁相遇之后各自反向前进与两只蚂蚁相遇之后保持原样交错而过继续前进是等效的,因此可认为每只蚂蚁都是独立运动的,最长时间就是每只蚂蚁往较长一端走的时间的最大值
- 代码:
1 #include <iostream> 2 3 using namespace std; 4 5 template <typename T> 6 T minn(T, T); 7 8 template int minn<int>(int, int); 9 10 template <typename T> 11 T maxn(T, T); 12 13 template<> int maxn<int>(int, int); 14 15 int main(int argc, char * argv[]) 16 { 17 int k; 18 for (cin >> k; k>0; k--) 19 { 20 int l, n; 21 cin >> l >> n; 22 int * a = new int[n]; 23 for (int i=0; i<n; i++) cin >> a[i]; 24 int tmin=0, tmax=0; 25 for (int i=0; i<n; i++) 26 { 27 tmin=maxn(tmin,minn(a[i],l-a[i])); 28 tmax=maxn(tmax,maxn(a[i],l-a[i])); 29 } 30 cout << tmin << ‘ ‘ << tmax << endl; 31 } 32 return 0; 33 } 34 35 template <typename T> 36 T minn(T x, T y) 37 { 38 if (x<y) return x; 39 return y; 40 } 41 42 template <typename T> 43 T maxn(T x, T y) 44 { 45 if (x>y) return x; 46 return y; 47 } 48 49 template<> int maxn<int>(int x, int y) 50 { 51 if (x>y) return x; 52 return y; 53 }
3.增加难度的抽签问题
- 题目大意:检查数组k中是否存在ka+kb+kc+kd=m,要求将四重循环的穷举算法进行优化,四重循环如下:
for (int a = 0; a < n; a++) { for (int b = 0; b < n; b++) { for (int c = 0; c < n; c++) { for (int d = 0; d < n; d++){ if (k[a]+k[b]+k[c]+k[d] == m) { f = true; } } } } }
- 优化1:考虑最内侧关于d的循环,做的事就是检查是否有d使得ka+kb+kc+kd = m,通过移相,可得kd = m-ka-kb-kc ,也即是说检查数组k中所有元素,判断是否有m-ka-kb-kc ,于是得到三重循环+二分搜索的O(n3logn)的算法
- 优化2:继续优化1的思路,考虑内侧的两个循环,内测的两个循环是在检查是否有c和d使得kc+kd = m-ka-kb ,这种情况下只要预先枚举出kc+kd 所得的n2个数字(实际上在去除重复之后n(n+1)/2个数字就够了)并排好序,就可以继续利用二分搜索了,总的时间复杂度是O(n2logn)
- 代码:(就放个二分查找的模板好了,枚举k[c]+k[d]的时候把它放在k[c*n+d]里就好了→_→)
1 #include <iostream> 2 #define random(l,r) (rand()%(r-l+1)+l) 3 4 using namespace std; 5 6 int n,m; 7 int * k; 8 9 bool binary_search(int); 10 void qsort(int, int); 11 12 int main(int argc, char * argv[]) 13 { 14 cin >> n >> m; 15 k = new int[n]; 16 for (int i=0; i<n; i++) 17 { 18 cin >> k[i]; 19 } 20 qsort(0,n-1); 21 for (int i=0; i<n; i++) 22 { 23 cout << k[i] << ‘ ‘; 24 } 25 cout << endl; 26 cout.setf(ios_base::boolalpha); 27 cout << binary_search(m) << endl; 28 } 29 30 bool binary_search(int x) 31 { 32 int l=0, r=n-1; 33 while (l<=r) 34 { 35 int mid = (l+r)/2; 36 if (k[mid]==x) return true; 37 else if (k[mid]<x) l=mid+1; 38 else r=mid-1; 39 } 40 return false; 41 } 42 43 void qsort(int l, int r) 44 { 45 int i=l, j=r; 46 int mid=k[random(l,r)]; 47 do 48 { 49 while (k[i]<mid) i++; 50 while (k[j]>mid) j--; 51 if (i<=j) 52 { 53 int temp=k[i]; 54 k[i]=k[j]; 55 k[j]=temp; 56 i++;j--; 57 } 58 } 59 while (i<=j); 60 if (i<r) qsort(i,r); 61 if (j>l) qsort(l,j); 62 }
以上是关于热身题的主要内容,如果未能解决你的问题,请参考以下文章