SRM 586 DIV2 500
2013-08-02 13:58
351 查看
Problem Statement | ||||||||||||
F is a function that is defined on all real numbers from the closed interval [1,N]. You are given a vector <int>Y with N elements. For each i (1 <= i <= N) we have F(i) =Y[i-1]. Additionally, you know that F is piecewise linear: for each i, on the interval [i,i+1] F is a linear function. The function F is uniquely determined by this information. For example, if F(4)=1 and F(5)=6 then we must have F(4.7)=4.5. As another example, this is the plot of the function F for Y = {1, 4, -1, 2}. You are also given a vector <int> query. For each i, compute the number of solutions to the equation F(x) =query[i]. Note that sometimes this number of solutions can be infinite. Return a vector <int> of the same length as query. For each i, element i of the return value should be -1 if the equation F(x) =query[i] has an infinite number of solutions. Otherwise, element i of the return value should be the actual number of solutions this equation has. | ||||||||||||
Definition | ||||||||||||
| ||||||||||||
Constraints | ||||||||||||
- | Y will contain between 2 and 50 elements, inclusive. | |||||||||||
- | Each element of Y will be between -1,000,000,000 and 1,000,000,000, inclusive. | |||||||||||
- | query will contain between 1 and 50 elements, inclusive. | |||||||||||
- | Each element of query will be between -1,000,000,000 and 1,000,000,000, inclusive. | |||||||||||
Examples | ||||||||||||
0) | ||||||||||||
| ||||||||||||
1) | ||||||||||||
| ||||||||||||
2) | ||||||||||||
| ||||||||||||
3) | ||||||||||||
|
题目意思:
两个vector, Y 和 query
Y代表的是 纵坐标,(横坐标为 i+1); (如上图所示)
query代表查询; 询问针对每个query[i] 能在那些线性区间中找到几个点.满足 纵坐标为query[i],即点(x,query[i])
#include <vector> #include <list> #include <map> #include <set> #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; class PiecewiseLinearFunctionDiv2 { public: vector <int> countSolutions(vector <int>, vector <int>); }; vector <int> PiecewiseLinearFunctionDiv2::countSolutions(vector <int> Y, vector <int> query) { vector <int> ans; int num; for(int i = 0; i < query.size(); ++i) { num = 0; for(int j = 0; j < Y.size()-1; ++j) { if(Y[j]==Y[j+1] && query[i]==Y[j]) { num = -1; break; } if(min(Y[j],Y[j+1]) < query[i] && query[i] < max(Y[j+1],Y[j])) num++; if(query[i] == Y[j]) num++; } if(num!=-1 && query[i]==Y[Y.size()-1]) num++; ans.push_back(num); } return ans; } <%:testing-code%> //Powered by [KawigiEdit] 2.0!
相关文章推荐
- SRM 586 div2
- topcoder SRM 500 div2 level3
- SRM 669 DIV 2 CombiningSlimes 500-point
- TopCoder SRM 649 Div2 Problem 500 - CartInSupermarketEasy (区间DP)
- TopCoder SRM 655 Div2 Problem 500 - FoldingPaper2 (思维)
- Topcoder SRM 503 DIV1 500 KingdomXCitiesandVillages
- [TopCoder] SRM 586 DIV 2, 500p, 1000p, Solution
- SRM 554 - 500 TheBrickTowerMediumDivOne
- SRM 606 div2 500 EllysNumberGuessing
- Topcoder SRM 543 DIV2 1000 EllysThreeRivers & DIV1 500 EllysRivers
- TopCoder SRM 653 Div2 Problem 500 - RockPaperScissorsMagicEasy (DP)
- SRM 597 DIV2 500
- SRM 552 - 500 FoxAndFlowerShopDivOne
- Topcoder: Problem Statement SRM 39 DIV1 500 Point
- TC SRM 671 Div2 500 BearDartsDiv2
- [TopCoder] SRM 586 DIV 2, 500p, 1000p, Solution
- Topcoder SRM 660 DIV1 500 Privateparty(数学)
- SRM 586 DIV1
- SRM 615 DIV1 500
- SRM 586 Div II Level Three: StringWeightDiv2,Dynamic Programming or Math