This is on Windows 10
Hello, world
2016年7月8日 星期五
2016年7月7日 星期四
Codeforces Round #361 (Div. 2)
http://codeforces.com/contest/689
Problems :
pA : http://codeforces.com/contest/689/problem/A
pB : http://codeforces.com/contest/689/problem/B
pC : http://codeforces.com/contest/689/problem/C
pD : http://codeforces.com/contest/689/problem/D
pE : http://codeforces.com/contest/689/problem/E
My Solution :
pA : http://codeforces.com/contest/689/submission/18922621
pB : http://codeforces.com/contest/689/submission/18924209
pC : http://codeforces.com/contest/689/submission/18932251
pD : http://codeforces.com/contest/689/submission/18932696
pE : http://codeforces.com/contest/689/submission/19054541
pA :
I use the concept of vector & brute force.
First, I give all the digits a coordinate. '0' is (2,1), '1' is (1,4), ... etc.
Then, I think that if there's another way to do the finger movements, the first digits must be different. So I try all digits as the first, and then try the movements the problem gave. During the process, if the coordinate is out of range, then it fails.
Time complexity : O(1)
pB :
I use BFS(Breadth-First-Search).
If you got a new point x, you should update x-1, x+1, and a[x].
Time Complexity : O(n)
pC :
I use binary search.
It's obvious that the greater m is, the greater n is. So you can do binary search on the n, from 8 to 5*10^15(when m = 10^15, the answer is about 4.949 * 10^15).
But how can we know the m when we get an n? First, the sequences are geometric sequence, so we can infer that the maximum number is a1*r^3, which a1 is the first number of geometric sequence and r is the ratio(2nd number / 1st number). So, we try every possible r that a1*r^3 < n , then we can quickly count the number of a1 by n/r^3.
Time Complexity : O(10^5 * log10^15)
pD :
Binary Search again. lol
First, we build a segment tree on array a and b. Let's call them seg_a and seg_b. For every i, 1<=i<=n, if a[i]>b[i], then it's impossible to find any possible answers. If a[i] == b[i], then we can do binary search on a & b that max(a) or min(b) changes first. We can query the value using seg_a & seg_b. If a[i]<b[i], then we can also do binary search! Think it clearly, min(b)-max(a) is increasing! So when min(b)-max(a) = 0. Then we got the answer. We can query the val using seg_a & seg_b, too.
Time Complexity : O(n lgn lgn)
pE :
First, we build dp saving C(n,k). Then we can quickly calculate that if there are i elements in [l,r], the answer should add (r-l+1) * dp[i]. So, we can sort all the input by the first value and build a minimum heap. When the input's first number is smaller than the top of the heap, we can update the value, ans so on. You can see my code. ^_^
Another round that solve all the questions. Happy~~
Problems :
pA : http://codeforces.com/contest/689/problem/A
pB : http://codeforces.com/contest/689/problem/B
pC : http://codeforces.com/contest/689/problem/C
pD : http://codeforces.com/contest/689/problem/D
pE : http://codeforces.com/contest/689/problem/E
My Solution :
pA : http://codeforces.com/contest/689/submission/18922621
pB : http://codeforces.com/contest/689/submission/18924209
pC : http://codeforces.com/contest/689/submission/18932251
pD : http://codeforces.com/contest/689/submission/18932696
pE : http://codeforces.com/contest/689/submission/19054541
pA :
I use the concept of vector & brute force.
First, I give all the digits a coordinate. '0' is (2,1), '1' is (1,4), ... etc.
Then, I think that if there's another way to do the finger movements, the first digits must be different. So I try all digits as the first, and then try the movements the problem gave. During the process, if the coordinate is out of range, then it fails.
Time complexity : O(1)
pB :
I use BFS(Breadth-First-Search).
If you got a new point x, you should update x-1, x+1, and a[x].
Time Complexity : O(n)
pC :
I use binary search.
It's obvious that the greater m is, the greater n is. So you can do binary search on the n, from 8 to 5*10^15(when m = 10^15, the answer is about 4.949 * 10^15).
But how can we know the m when we get an n? First, the sequences are geometric sequence, so we can infer that the maximum number is a1*r^3, which a1 is the first number of geometric sequence and r is the ratio(2nd number / 1st number). So, we try every possible r that a1*r^3 < n , then we can quickly count the number of a1 by n/r^3.
Time Complexity : O(10^5 * log10^15)
pD :
Binary Search again. lol
First, we build a segment tree on array a and b. Let's call them seg_a and seg_b. For every i, 1<=i<=n, if a[i]>b[i], then it's impossible to find any possible answers. If a[i] == b[i], then we can do binary search on a & b that max(a) or min(b) changes first. We can query the value using seg_a & seg_b. If a[i]<b[i], then we can also do binary search! Think it clearly, min(b)-max(a) is increasing! So when min(b)-max(a) = 0. Then we got the answer. We can query the val using seg_a & seg_b, too.
Time Complexity : O(n lgn lgn)
pE :
First, we build dp saving C(n,k). Then we can quickly calculate that if there are i elements in [l,r], the answer should add (r-l+1) * dp[i]. So, we can sort all the input by the first value and build a minimum heap. When the input's first number is smaller than the top of the heap, we can update the value, ans so on. You can see my code. ^_^
Another round that solve all the questions. Happy~~
2016年7月6日 星期三
(TOJ) 245 / 逆序數對2
http://sprout.tw/oj/pro/245/
我的想法:
用treap維護好之前的數字,然後查詢 -q+aj ~ -p+aj 之間的數字XD
我的想法:
用treap維護好之前的數字,然後查詢 -q+aj ~ -p+aj 之間的數字XD
#include <iostream> #include <stdio.h> #include <ctime> #include <cstdlib> using namespace std; struct Treap { Treap* lc; Treap* rc; int key,pri,size; Treap(int _key) { lc=rc=NULL; key=_key; pri=rand(); size=1; } }; int Size(Treap* t) { return t ? t->size : 0; } void pull(Treap* t) { t->size = 1 + Size(t->lc) + Size(t->rc); } Treap* merge(Treap* a,Treap* b) { if (!a||!b) return a?a:b; else if (a->pri > b->pri) { a->rc=merge(a->rc,b); pull(a); return a; } else { b->lc=merge(a,b->lc); pull(b); return b; } } void split(Treap* t,int k,Treap* &a,Treap* &b) { if (!t) a=b=NULL; else if (t->key <= k) { a=t; split(t->rc,k,a->rc,b); pull(a); } else { b=t; split(t->lc,k,a,b->lc); pull(b); } } //p<=aj-ai<=q //-q+aj<=ai<=-p+aj int query(Treap* root,int val,int p,int q) { Treap* tl; Treap* tr; // cout<<-q+val-1<<" ~ "<<-p+val <<endl; split(root,-q+val-1,tl,root); split(root,-p+val,root,tr); if (root!=NULL) { // cout<<"root->key = "<<root->key<<endl; pull(root); } else { // cout<<"empty\n"; } int ret=Size(root); root=merge(merge(tl,root),tr); return ret; } void dfs(Treap* t) { cout<<t->key<<' '; if (t->lc) dfs(t->lc); if (t->rc) dfs(t->rc); } int main () { srand(time(NULL)); int T; scanf("%d",&T); while (T--) { int n,p,q; scanf("%d %d %d",&n,&p,&q); long long ans=0; Treap* root=NULL; for (int x=0;n>x;x++) { // cout<<"x="<<x<<endl; int i; scanf("%d",&i); ans+=query(root,i,p,q); //root=merge(root,new Treap(i)); ==> 錯的 Treap* tl; Treap* tr; split(root,i-1,tl,root); split(root,i,root,tr); root=merge(root,new Treap(i)); root=merge(merge(tl,root),tr); } printf("%lld\n",ans); } }
(TOJ) 80 / RMQ 練習(1) [treap 區間max,min]
http://sprout.tw/oj/pro/80/
Treap 解
Treap 解
#include <iostream> #include <stdio.h> #include <ctime> #include <cstdlib> using namespace std; struct Treap { Treap* lc; Treap* rc; int val; int pri; int key; int mx; Treap(int _val,int _key) { lc=rc=NULL; val=_val; key=_key; pri=rand(); mx=_val; } }; const int INF= 1e9+7; int Mx(Treap* t) { return t?t->mx:INF; } void pull(Treap* t) { t->mx = min(Mx(t->lc),Mx(t->rc)); t->mx = min(t->mx,t->val); } Treap* merge(Treap* a,Treap* b) { if (!a||!b) return a?a:b; else if (a->pri > b->pri) { a->rc=merge(a->rc,b); pull(a); return a; } else { b->lc=merge(a,b->lc); pull(b); return b; } } void split(Treap* t,int k,Treap* &a,Treap* &b) { if (!t) a=b=NULL; else if (t->key <= k) { a=t; split(t->rc,k,a->rc,b); pull(a); } else { b=t; split(t->lc,k,a,b->lc); pull(b); } } void insert(Treap* root,int k,int val) { Treap* tl; Treap* tr; split(root,k-1,tl,root); split(root,k,root,tr); root->val = root->mx = val; merge(merge(tl,root),tr); } int query(Treap* root,int l,int r) { Treap* tl; Treap* tr; split(root,l-1,tl,root); split(root,r,root,tr); int ret=root->mx; merge(merge(tl,root),tr); return ret; } int main () { srand(time(NULL)); int T,n; while (scanf("%d %d",&T,&n) != EOF) { Treap* root=NULL; for (int x=0;n>x;x++) { // cout<<"x="<<x<<endl; int i; scanf("%d",&i); root = merge(root,new Treap(i,x)); } while (T--) { int i,j,k; scanf("%d %d %d",&i,&j,&k); if (i==1) { printf("%d\n",query(root,j,k)); } else { insert(root,j,k); } } } }
a457: TOI2010 第五題:餐廳評鑑
http://zerojudge.tw/ShowProblem?problemid=a457
先按照第一個值(s)排序之後,尋找第二個值(r)的逆序數對個數(在不同第一個值之間)。
先按照第一個值(s)排序之後,尋找第二個值(r)的逆序數對個數(在不同第一個值之間)。
#include <iostream> #include <stdio.h> #include <utility> #include <algorithm> using namespace std; const int MAX_N = 2e5 + 6; typedef pair<int,int> pii; vector<int> num; struct Node { Node* lc; Node* rc; int val; Node() { lc=rc=NULL; val=0; } void pull() { val=lc->val + rc->val; } }; Node* Build(int L,int R) { Node* node = new Node(); if (L==R) { return node; } int mid=(L+R)>>1; node->lc=Build(L,mid); node->rc=Build(mid+1,R); node->pull(); return node; } void modify(Node* node,int L,int R,int pos) { if (L==R) { node->val += 1; return; } int mid=(L+R)>>1; if (pos<=mid) modify(node->lc,L,mid,pos); else modify(node->rc,mid+1,R,pos); node->pull(); return; } int query(Node* node,int L,int R,int l,int r) { if (L>r || l>R) return 0; else if (l<=L && R<=r) return node->val; int mid=(L+R)>>1; return query(node->lc,L,mid,l,r) + query(node->rc,mid+1,R,l,r); } pii s[MAX_N]; int main () { int k,m; while (scanf("%d %d",&k,&m) != EOF) { for (int x=0;k>x;x++) { int i; scanf("%d",&i); s[x].first=i; } for (int x=0;k>x;x++) { int i; scanf("%d",&i); s[x].second=i; } sort(s,s+k); for (int x=0;k>x;x++) { num.push_back(s[x].second); } sort(num.begin(),num.end()); num.resize(unique(num.begin(),num.end()) - num.begin()); for (int x=0;k>x;x++) { s[x].second = lower_bound(num.begin(),num.end(),s[x].second) - num.begin() + 1; } long long ans=0; int n=num.size(); Node* root = Build(1,n); for (int x=0;k>x;x++) { vector<int> tmp; tmp.push_back(s[x].second); int val=s[x].first; while (k>x && s[x+1].first == val) { x++; tmp.push_back(s[x].second); } for (auto iter=tmp.begin();iter!=tmp.end();iter++) { int t=*iter; ans+=query(root,1,n,t+1,n); } for (auto iter=tmp.begin();iter!=tmp.end();iter++) { int t=*iter; modify(root,1,n,t); } } printf("%lld\n",ans); } }
2016年7月5日 星期二
(Zj) a300: NOIP2011 Day1.2.选择客栈
http://zerojudge.tw/ShowProblem?problemid=a300
我的想法是說:
先觀察有一個很神奇的單調性:假設固定一個開始的客棧a , 如果a ~ b的選擇是okay的,那a~c (c>b) 也一定是okay的!!!(可以仔細想想為甚麼!)
藉由這個神奇的性質,我們可以開一棵線段樹維護最小值,然後binary search,就好了。
我的想法是說:
先觀察有一個很神奇的單調性:假設固定一個開始的客棧a , 如果a ~ b的選擇是okay的,那a~c (c>b) 也一定是okay的!!!(可以仔細想想為甚麼!)
藉由這個神奇的性質,我們可以開一棵線段樹維護最小值,然後binary search,就好了。
#include <iostream> #include <stdio.h> #include <vector> using namespace std; #define int long long const int MAX_N = 2e5 + 6; const int INF = 1e15+7; vector<int> kind[MAX_N]; struct Node { Node* lc; Node* rc; int val; Node() { lc=rc=NULL; val=0; } void pull() { val=min(lc->val,rc->val); } }; int v[MAX_N]; int kd[MAX_N]; //kind Node* Build(int L,int R) { Node* node = new Node(); if (L==R) { node->val=v[L]; return node; } int mid=(L+R)>>1; node->lc=Build(L,mid); node->rc=Build(mid+1,R); node->pull(); return node; } int query(Node* node,int L,int R,int l,int r) { if (l>R || L>r) return INF; else if (l<=L && R<=r) return node->val; int mid=(L+R)>>1; return min(query(node->lc,L,mid,l,r) , query(node->rc,mid+1,R,l,r)); } main () { int n,k,p; while (scanf("%lld %lld %lld",&n,&k,&p) != EOF) { for (int x=1;n>=x;x++) { scanf("%lld %lld",&kd[x],&v[x]); kind[kd[x]].push_back(x); } Node* root=Build(1,n); long long ans=0; for (int i=0;k>i;i++) { // cout<<"i="<<i<<endl; if (kind[i].size()>1) { int len=kind[i].size(); for (int j=0;len-1>j;j++) { // cout<<"j="<<j<<endl; int L=j,R=len-1; while (R-L!=1) { // cout<<"L ~ R : "<<L<<" ~ "<<R<<endl; int mid=(L+R)>>1; if (query(root,1,n,kind[i][j],kind[i][mid]) <= p) R=mid; else L=mid; } // cout<<"j="<<j<<" , L="<<L<<endl; if (query(root,1,n,kind[i][j],kind[i][R]) > p) break; else ans+=(len-R); // cout<<"ans = "<<ans<<endl; } } // cout<<"ans="<<ans<<endl; } printf("%lld\n",ans); } }
2016年6月30日 星期四
(POJ) Find them, Catch them
http://poj.org/problem?id=1703
先開一個2*MAX_N的disjoint set,其中1~MAX_N是在第一個幫派的可能,MAN_N + 1 ~ MAX_N + MAX_N 是第二個幫派的可能,那麼,當做到D操作時,可以合併[A],[B] + MAX_N和 [A] + MAX_N, [B],那查詢的時候,可以依照這個樣子去查,就okay囉!
時間複雜度:O(alpha(N))
先開一個2*MAX_N的disjoint set,其中1~MAX_N是在第一個幫派的可能,MAN_N + 1 ~ MAX_N + MAX_N 是第二個幫派的可能,那麼,當做到D操作時,可以合併[A],[B] + MAX_N和 [A] + MAX_N, [B],那查詢的時候,可以依照這個樣子去查,就okay囉!
時間複雜度:O(alpha(N))
#include <iostream> #include <stdio.h> using namespace std; const int MAX_N = 1e5 + 3; struct DisjointSet { int p[2 * MAX_N]; void init(int i){ for (int x=0;2*MAX_N>x;x++) { p[x]=x; } } int Find(int x) { return p[x]==x?x:p[x]=Find(p[x]); } void Union(int x,int y) { p[Find(x)]=Find(y); } } djs; int main() { int T; scanf("%d",&T); while (T--) { int n,m; scanf("%d %d",&n,&m); djs.init(n); for (int x=0;m>x;x++) { char a; int b,c; scanf("\n%c %d %d",&a,&b,&c); if (a=='A') { if (djs.Find(b) == djs.Find(c)) { puts("In the same gang."); } else if (djs.Find(b) == djs.Find(c+MAX_N)) { puts("In different gangs."); } else { puts("Not sure yet."); } } else { djs.Union(b,c+MAX_N); djs.Union(c,b+MAX_N); } } } }
訂閱:
文章 (Atom)