【题意】根据Y画出一条折线,分别求出query中的直线与这样折线的交点数,并返回。
【算法】
1.求query[i]与折线中每天线段的交点数(端点不算)a,如果与某条线段重合,return-1,否则继续2;
2.求query[i]与Y中顶点的交点数b;
3.return a+b;
【调试bug】
1.17行判断query[i]与线段(Y[j],Y[j+1])是否有交点时,注意数字比较大,不能采用一般的乘法来判断。
【Java代码】来自菜鸟
1 import java.util.*; 2 import java.util.regex.*; 3 import java.text.*; 4 import java.math.*; 5 6 7 public class PiecewiseLinearFunctionDiv2 8 { 9 public int[] countSolutions(int[] Y, int[] query) 10 { 11 int[] r = new int[query.length]; 12 int i,j; 13 Arrays.fill(r, 0); 14 15 for(i=0;i<query.length;i++){ 16 for(j=0;j<Y.length-1;j++){ 17 if(((query[i]-Y[j])+(query[i]-Y[j+1]))<Math.max(query[i]-Y[j], query[i]-Y[j+1])&&((query[i]-Y[j])+(query[i]-Y[j+1]))>Math.min(query[i]-Y[j], query[i]-Y[j+1])) 18 r[i]++; 19 else if(query[i]==Y[j]&&query[i]==Y[j+1]){ 20 r[i]=-1; 21 break; 22 } 23 } 24 if(j<Y.length-1) 25 continue; 26 for(j=0;j<Y.length;j++){ 27 if(query[i]==Y[j]) 28 r[i]++; 29 } 30 } 31 return r; 32 } 33 34 35 } 36 //Powered by KawigiEdit 2.1.4 (beta) modified by pivanof!
【改进版代码】
【分析】判断query[i]与线段有没有交点的方法,更新了一下。
1 import java.util.*; 2 import java.util.regex.*; 3 import java.text.*; 4 import java.math.*; 5 6 7 public class PiecewiseLinearFunctionDiv2 8 { 9 public int[] countSolutions(int[] Y, int[] query) 10 { 11 int[] r = new int[query.length]; 12 int i,j; 13 Arrays.fill(r, 0); 14 15 for(i=0;i<query.length;i++){ 16 for(j=0;j<Y.length-1;j++){ 17 if(query[i]>Y[j]&&query[i]<Y[j+1]||query[i]<Y[j]&&query[i]>Y[j+1]) 18 r[i]++; 19 else if(query[i]==Y[j]&&query[i]==Y[j+1]){ 20 r[i]=-1; 21 break; 22 } 23 } 24 if(j<Y.length-1) 25 continue; 26 for(j=0;j<Y.length;j++){ 27 if(query[i]==Y[j]) 28 r[i]++; 29 } 30 } 31 return r; 32 } 33 34 35 } 36 //Powered by KawigiEdit 2.1.4 (beta) modified by pivanof!
【Java代码】来自大神
【分析】算法差不多,只是在顶点的处理上有点不一样。
public class PiecewiseLinearFunctionDiv2 { public static int[] countSolutions(int[] Y, int[] query) { int[] count = new int[query.length]; for (int j = 0; j < query.length; j++) if (Y[0] == query[j]) count[j] = 1; for (int i = 1; i < Y.length; i++) { for (int j = 0; j < query.length; j++) { if (count[j] == -1) continue; if (Y[i - 1] == query[j] && Y[i] == query[j]) count[j] = -1; if (Y[i - 1] > query[j] && Y[i] <= query[j] || Y[i - 1] < query[j] && Y[i] >= query[j]) count[j]++; } } return count; } }
【C++代码】来自大神
【分析】算法几乎一样的。
include <vector> #include <list> #include <map> #include <set> #include <queue> #include <deque> #include <stack> #include <bitset> #include <algorithm> #include <functional> #include <numeric> #include <utility> #include <sstream> #include <iostream> #include <iomanip> #include <cstdio> #include <cmath> #include <cstdlib> #include <ctime> using namespace std; #define pb push_back class PiecewiseLinearFunctionDiv2 { public: vector <int> countSolutions(vector <int> Y, vector <int> query) { vector<int> ans; for(int i=0; i<query.size(); i++) { int temp=query[i]; int cnt=0; int flag=0; for(int j=0; j<Y.size()-1; j++) { int y1=Y[j], y2=Y[j+1]; if(temp==y1 && temp==y2) flag=1; if(temp>y1 && temp<y2) cnt++; else if(temp<y1 && temp>y2) cnt++; } for(int j=0; j<Y.size(); j++) if(Y[j]==temp) cnt++; if(flag==0) ans.pb(cnt); else ans.pb(-1); } return ans; } }; //Powered by KawigiEdit 2.1.7 (beta) modified by pivanof!
【总结】:题有点简单。