标签:有一个 计算 clu ems isp span sed abs algo
看过一本书,[挑战程序设计竞赛(第2版)].巫泽俊.(提取码:w81q)
里面有一个问题是:
灵感便源于此,我们这样考虑一下,两只蚂蚁从两端往中间爬,相撞之后分别掉头,这时把他看作是擦肩而过会发现,其实是一样的,因此我们就不用考虑掉头的问题。
想明白之后会发现代码异常的简单。
1 #include<iostream> 2 #include<cstring> 3 #include<cmath> 4 5 using namespace std; 6 7 const int MAX = 51; 8 int n = 0; 9 10 int fun(int *a) 11 { 12 int c = 1; 13 int r, l; r = l = 0; 14 if(a[0] > 0){ // 往右 15 for(int i=1;i<n;i++){ 16 if(abs(a[i]) > abs(a[0]) && a[i] < 0) 17 r++; // 右边感冒蚂蚁的数目 18 if(abs(a[i]) < abs(a[0]) && a[i] > 0) 19 l++; 20 } 21 c = (r == 0)?1:c+l+r; // 往右没有被传染的蚂蚁,左边也不可能被传染 22 return c; 23 } 24 else{ // 往左 a[0] < 0 25 for(int i=1;i<n;i++){ 26 if(abs(a[i]) < abs(a[0]) && a[i] > 0) 27 l++; 28 if(abs(a[i]) > abs(a[0]) && a[i] < 0) 29 r++; 30 } 31 c = (l == 0)?1:c+l+r; 32 return c; 33 } 34 } 35 36 int main() 37 { 38 int a[MAX]; 39 memset(a, 0, sizeof(a)); 40 while(cin>>n) 41 { 42 for(int i=0;i<n;i++) cin>>a[i]; 43 cout<<fun(a)<<endl; 44 } 45 return 0; 46 }
2019-02-05
18:55:04
标签:有一个 计算 clu ems isp span sed abs algo
原文地址:https://www.cnblogs.com/mabeyTang/p/10352981.html