2018年四川ICPC省賽J題  2813: JoJo’s Bizarre Adventure (思維 + STL)

 2813: JoJo’s Bizarre Adventure

Time Limit: 1000 MS Memory Limit: 1048576 KB
Total Submit: 60 Accepted: 12 Page View: 187
Submit Status Discuss

Description 

Dio and JoJo are playing a game.

At the beginning, JoJo writes down nn non-negative integers a1;a2;...;ana1;a2;...;an(0=a1≤a2≤⋅⋅⋅≤an0=a1≤a2≤···≤an) on a sheet of paper and a1=0a1=0 is guaranteed. Then, he makes a multiset Smulti={ai+aj|1≤i<j≤n}Smulti={ai+aj|1≤i<j≤n}, and tells the numbers to Dio within Smulti in non-decreasing order one after another.

As for Dio, he wins the game if and only if he can determine the original nn numbers aiai(1≤i≤n1≤i≤n) after receiving some of the numbers in Smulti given by JoJo.

If Dio can't win the game as soon as possible, he won't be a human anymore. So can you help him determine the earliest time to win?

Input 

The first line contains a integer TT representing the number of test cases. In each test case, the first line contains one integer nn. The second line contains n×(n−1)2n×(n−1)2 integers, the i−thi−th integer sisi represents the i−thi−th element in SmultiSmulti. It is guaranteed that the numbers in SmultiSmulti are generated by some nn non-negative integers a1;...;ana1;...;an and given in non-decreasing order. • 1≤T≤101≤T≤10 • 1≤n≤5001≤n≤500 • 0≤si≤1090≤si≤109 • 0<∑n≤10000<∑n≤1000

Output 

For each test case, output a number xx in one line indicating that after receiving xx sums, Dio can determine the original nn numbers.

2 4 1 2 3 3 4 5 4 1 2 2 3 3 4

4 3

Hint 

In the first test case, {0;1;2;3}{0;1;2;3} can be determined as the initial numbers after receiving 4 numbers. In the second test case, {0;1;2;2}{0;1;2;2} can be determined as the initial numbers after receiving 3 numbers.

Source 

The 2018 Sichuan Provincial Collegiate Programming Contest

Tags  

 

告訴你一個s的集合,以非遞減順序給出

問你得到s的前多少項,可以得到這個s的集合所有元素

a_i + a_j = s_k

 

可以考慮刪數, 1 2 3 3 4 5 

剛開始我們有0,所以拿出來 1 ,可以得到 1 , 直接把1的第一個位置刪除

現在就有 0 和 1,再拿出來 2 ,可以得到 2,3  然後又把第一個出現 2 ,3 的位置刪除

然後現在又有0,1,2,3 , 再拿出來 3, 可以得到 3,4,5,6 刪除第一次出現的會發現 只需要在第2個3的時候,就可以得到這個s集合裏所有的數字了,所以ans = 4

 

因爲全都是非遞減給出的 , 那我們先把所有的數都入set,然後對於每個最小的set中的元素,每次二分一下最開始的位置就可以了。當set都空的時候,就可以找到答案了。


#include <bits/stdc++.h>
#include <time.h>
#define fi first
#define se second
#define endll "\n"
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define INF 0x3f3f3f3f3f3f3f3f
#define inf 0x3f3f3f3f
///vector(len,val);
using namespace std;

typedef long long ll;
typedef double db;
int xx[4] = {1,-1,0,0};
int yy[4] = {0,0,1,-1};
const double eps = 1e-9;
typedef pair<int,int>  P;
const int maxn = 2e6 + 5000;
const ll mod = 1e9 + 7;
inline int sign(db a) { return a < -eps ? -1 : a > eps;}
inline int cmp(db a,db b){ return sign(a - b);}
void debug(int x){  cout << x << endl; }
ll mul(ll a,ll b,ll c) { ll res = 1; while(b) {  if(b & 1) res *= a,res %= c;  a *= a,a %= c,b >>= 1;  }  return res;}
ll phi(ll x) {  ll res = x;  for(ll i = 2; i * i <= x; i++) { if(x % i == 0) res = res / i * (i - 1);   while(x % i == 0) x /= i;   }  if(x > 1) res = res / x  * (x - 1);    return res;}

int fa[maxn];
int Find(int x) {   if(x != fa[x]) return fa[x] = Find(fa[x]);  return fa[x];}
ll c,n,k;
set<P>s;
int main() { int m,t;
    scanf("%d",&t);
    while(t--){   scanf("%d",&n);s.clear();
        for(int i = 1;i <= n * (n - 1) / 2;i++){
            int x;scanf("%d",&x);s.insert(P(x,i));
        }
        vector<int>ans; ans.push_back(0);
        int res = 0;
        while(!s.empty()){
            P p = *s.begin(); res = p.se;
            for(auto d:ans) s.erase(s.upper_bound(P(d + p.fi,-1)));
            ans.push_back(p.fi);
        }
        printf("%d\n",res);
    }
    cerr << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章