Problem Statement
A store sells M different items, conveniently numbered 0 through M-1.For a shopping survey you interviewed N customers.Each customer responded to the survey with a list of items they've bought.Each customer bought at most one of each item.It is possible that some customers did not buy anything at all.
After collecting the responses, you've summed up the results and found that s[i] people have bought item i.Due to an unfortunate accident, you've then lost the actual survey responses.All you have left are the values s[i] you computed.
You are now supposed to report the number of big shoppers among the survey respondents.A big shopper is defined as a customer who has bought all M items.Of course, having lost the detailed responses, you might be unable to determine the actual number of big shoppers.
You are given the int N and the int[] s with M elements.Compute and return the smallest possible number of big shoppers.
Definition
- Class ShoppingSurveyDiv2
- Method minValue
- Parameters int , vector<int>
- Returns int
- Method signature int minValue(int N, vector<int> s)
Limits
- Time limit (s) 2.000
- Memory limit (MB) 256
Constraints
- N will be between 1 and 100, inclusive.
- s will contain between 1 and 100 elements, inclusive.
- Each element in s will be between 0 and N, inclusive.
Test cases
-
- N 5
- s { 3, 3 }
Returns 1There are 5 customers and 2 items in the store.Each of the items was bought by three of the customers.Since there are five people and a total of six bought items, we must have at least one big shopper.And we can easily verify that there could have been exactly one big shopper and four other customers who have bought one item each. -
- N 100
- s { 97 }
Returns 97 -
- N 10
- s { 9, 9, 9, 9, 9 }
Returns 5 -
- N 7
- s { 1, 2, 3 }
Returns 0 -
- N 5
- s { 3, 3, 3 }
Returns 0
本来是一道水题,开始往动规方向想了,没想出来⊙﹏⊙b汗
第一个物品,假设有s1个人拿了,你令1 2 3.. s1 人拿了
第二个物品,假设有s2个人拿了,你令s1+1, s1+2,.... s1+s2个人拿了
然后模拟完了统计一下
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <typeinfo>
#include <fstream>
using namespace std;
int p[110];
class ShoppingSurveyDiv2 {
public:
int minValue(int N, vector<int> s) {
memset(p,0,sizeof(p));
int M=s.size(),pre=0;;
for(int i=0;i<M;i++)
{
for(int j=1;j<=s.at(i);j++)
p[(pre+j)%N]++;
pre+=s.at(i);
}
int ans=0;
for(int i=0;i<N;i++) if(p[i]==M) ans++;
return ans;
}
};
// CUT begin
ifstream data("ShoppingSurveyDiv2.sample");
string next_line() {
string s;
getline(data, s);
return s;
}
template <typename T> void from_stream(T &t) {
stringstream ss(next_line());
ss >> t;
}
void from_stream(string &s) {
s = next_line();
}
template <typename T> void from_stream(vector<T> &ts) {
int len;
from_stream(len);
ts.clear();
for (int i = 0; i < len; ++i) {
T t;
from_stream(t);
ts.push_back(t);
}
}
template <typename T>
string to_string(T t) {
stringstream s;
s << t;
return s.str();
}
string to_string(string t) {
return "\"" + t + "\"";
}
bool do_test(int N, vector<int> s, int __expected) {
time_t startClock = clock();
ShoppingSurveyDiv2 *instance = new ShoppingSurveyDiv2();
int __result = instance->minValue(N, s);
double elapsed = (double)(clock() - startClock) / CLOCKS_PER_SEC;
delete instance;
if (__result == __expected) {
cout << "PASSED!" << " (" << elapsed << " seconds)" << endl;
return true;
}
else {
cout << "FAILED!" << " (" << elapsed << " seconds)" << endl;
cout << " Expected: " << to_string(__expected) << endl;
cout << " Received: " << to_string(__result) << endl;
return false;
}
}
int run_test(bool mainProcess, const set<int> &case_set, const string command) {
int cases = 0, passed = 0;
while (true) {
if (next_line().find("--") != 0)
break;
int N;
from_stream(N);
vector<int> s;
from_stream(s);
next_line();
int __answer;
from_stream(__answer);
cases++;
if (case_set.size() > 0 && case_set.find(cases - 1) == case_set.end())
continue;
cout << " Testcase #" << cases - 1 << " ... ";
if ( do_test(N, s, __answer)) {
passed++;
}
}
if (mainProcess) {
cout << endl << "Passed : " << passed << "/" << cases << " cases" << endl;
int T = time(NULL) - 1411725823;
double PT = T / 60.0, TT = 75.0;
cout << "Time : " << T / 60 << " minutes " << T % 60 << " secs" << endl;
cout << "Score : " << 500 * (0.3 + (0.7 * TT * TT) / (10.0 * PT * PT + TT * TT)) << " points" << endl;
}
return 0;
}
int main(int argc, char *argv[]) {
cout.setf(ios::fixed, ios::floatfield);
cout.precision(2);
set<int> cases;
bool mainProcess = true;
for (int i = 1; i < argc; ++i) {
if ( string(argv[i]) == "-") {
mainProcess = false;
} else {
cases.insert(atoi(argv[i]));
}
}
if (mainProcess) {
cout << "ShoppingSurveyDiv2 (500 Points)" << endl << endl;
}
return run_test(mainProcess, cases, argv[0]);
}
// CUT end