SRM 586 DIV2 500

來源:互聯網
上載者:User
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
 
Class: PiecewiseLinearFunctionDiv2
Method: countSolutions
Parameters: vector <int>, vector <int>
Returns: vector <int>
Method signature: vector <int> countSolutions(vector <int> Y, vector <int> query)
(be sure your method is public)
 
 
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, 4, -1, 2}
{-2, -1, 0, 1}
Returns: {0, 1, 2, 3 }
This is the example from the problem statement. The detailed information about the queries is:

  • There is no such x that F(x) = -2 is satisfied.
  • F(x) = -1 is only true for x = 3.
  • F(x) = 0 has two roots: 2.8 and 10/3.
  • F(x) = 1 has three roots: 1, 2.6 and 11/3.
1)  
 
{0, 0}
{-1, 0, 1}
Returns: {0, -1, 0 }
This function's plot is a horizontal segment between points (1, 0) and (2, 0). F(x) = 0 is satisfied for any x between 1 and 2 and thus the number of solutions is infinite. For any other value on the right-hand side, it has no solutions.
2)  
 
{2, 4, 8, 0, 3, -6, 10}
{0, 1, 2, 3, 4, 0, 65536}
Returns: {3, 4, 5, 4, 3, 3, 0 }
3)  
 
{-178080289, -771314989, -237251715, -949949900, -437883156, -835236871, -316363230, -929746634, -671700962}
{-673197622, -437883156, -251072978, 221380900, -771314989, -949949900, -910604034, -671700962, -929746634, -316363230}
Returns: {8, 6, 3, 0, 7, 1, 4, 8, 3, 4 }

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.
    

題目意思:

兩個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!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.