Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <list>
- #include <map>
- #include <set>
- #include <deque>
- #include <stack>
- #include <queue>
- #include <algorithm>
- #include <sstream>
- #include <iostream>
- #include <iomanip>
- #include <cstdio>
- #include <cmath>
- #include <cstdlib>
- #include <memory.h>
- #include <ctime>
- #include <bitset>
- #include <unordered_map>
- using namespace std;
- #define ABS(a) ((a>0)?a:-(a))
- #define MIN(a,b) ((a<b)?(a):(b))
- #define MAX(a,b) ((a<b)?(b):(a))
- #define FOR(i,a,n) for (int i=(a);i<(n);++i)
- #define FI(i,n) for (int i=0; i<(n); ++i)
- #define pnt pair <int, int>
- #define mp make_pair
- #define PI 3.1415926535897
- #define MEMS(a,b) memset(a,b,sizeof(a))
- #define LL long long
- #define U unsigned
- int used[30];
- set<int> s;
- int a[30];
- int c[30];
- const int INF = 1000000001;
- vector<pair<int,pnt > > get1(int n)
- {
- vector<pair<int,pnt > > res;
- int i = 1;
- for (i = 1; i*i<=n; ++i)
- {
- res.push_back(mp(n/i,mp(i,i)));
- }
- while (i<=n)
- {
- int k = n/i;
- int to = n/k;
- res.push_back(mp(k,mp(i,to)));
- i = to + 1;
- }
- res.push_back(mp(0,mp(n+1,INF)));
- reverse(res.begin(), res.end());
- return res;
- }
- vector<pnt > all;
- void get2(int a, int b, int v)
- {
- vector<pair<int,pnt > > res1 = get1(a);
- vector<pair<int,pnt > > res2 = get1(b);
- int i = 0, j = 0;
- while ((i<res1.size()) && (j<res2.size()))
- {
- if (res1[i].first == res2[j].first)
- {
- int l = max(res1[i].second.first, res2[j].second.first);
- int r = min(res1[i].second.second, res2[j].second.second);
- if (l<=r)
- {
- //cout<<l<<" "<<r<<endl;
- all.push_back(mp(l,v));
- all.push_back(mp(r+1,-v));
- }
- i++;
- j++;
- }
- else
- if (res1[i].first<res2[j].first)
- i++;
- else
- j++;
- }
- //cout<<endl;
- }
- vector<int> order;
- int min1[30];
- int max1[30];
- void brut(int x, int y)
- {
- FOR(i,1,y+2)
- {
- if (x/i == y/i)
- cout<<i<<endl;
- }
- }
- int main() {
- #ifdef Fcdkbear
- freopen("in.txt", "r", stdin);
- double beg = clock();
- //freopen("out.txt", "w", stdout);
- #endif
- /*brut(2000,2015);
- return 0;*/
- /*vector<pair<int, pnt > > t = get1(25);
- FOR(i,0,t.size())
- {
- cout<<t[i].first<<": "<<t[i].second.first<<" "<<t[i].second.second<<endl;
- }
- return 0;*/
- int n;
- cin>>n;
- FOR(i,0,n)
- {
- cin>>a[i]>>c[i];
- a[i]--;
- s.insert(c[i]);
- if ((order.size() == 0) || (order.back() != c[i])) {
- order.push_back(c[i]);
- min1[c[i]] = a[i];
- }
- max1[c[i]] = a[i];
- }
- if (s.size() == 1)
- {
- cout<<-1<<endl;
- return 0;
- }
- used[c[0]]=1;
- FOR(i,1,n)
- {
- if ((c[i]!=c[i-1]) && (used[c[i]]))
- {
- cout<<0<<endl;
- return 0;
- }
- }
- int tot = 0;
- FOR(i,0,n)
- FOR(j,i,n)
- if (c[i] == c[j])
- {
- tot++;
- get2(a[i],a[j],1);
- }
- else
- get2(a[i],a[j],-1);
- sort(all.begin(), all.end());
- int last = 0;
- int res = 0;
- int p = 0;
- int cnt = 0;
- while (p<all.size())
- {
- if (all[p].first > 1000000000)
- break;
- int c = p;
- while ((c<all.size()) && (all[c].first == all[p].first))
- {
- cnt+=all[c].second;
- c++;
- }
- if (cnt == tot)
- {
- //cout<<all[p].first << " " << all[c].first - 1<<endl;
- res += all[c].first - all[p].first;
- }
- p = c;
- }
- cout<<res<<endl;
- #ifdef Fcdkbear
- double end = clock();
- fprintf(stderr, "*** Total time = %.3lf ***\n",
- (end - beg) / CLOCKS_PER_SEC);
- #endif
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement