發表文章

目前顯示的是 5月, 2018的文章

[Codeforces] 477 D. Resource Distribution

題目連結: http://codeforces.com/contest/967/problem/D   只有兩個要處理的服務,所以就會猜是某種單調性或前綴或後綴之類的,通常是先O(N)或O(nlgn)之類的預處理前或後綴去處理其中一個,再O(N)或O(nlgn)處理剩下的之類的吧? #include<bits/stdc++.h> # define debug ( a ) cout << #a << " = " << a << endl; # define pb push_back # define int long long # define all ( v ) (v).begin(),(v).end() using namespace std ; typedef pair< int , int > pi; int n,x1,x2; pi c[ 300009 ]; vector< int > ans1,ans2; signed main () { cin >> n >> x1 >> x2; for(int i = 0; i < n; i++) { cin >> c[i] .first ; c[i] .second = i; } sort( c,c+n); int maxu = 1e9; bool ok = 0; int dd; for(int i = n-1; i >= 0; i--) { int ddd = x2/c[i] .first ; if(x2%c[i] .first != 0) ddd += 1; if(n-i >= ddd) { dd = ddd; maxu = i; ok = 1; break; } } for(int i = 0; i < n &

[Codeforces] 477 C. Stairs and Elevators

題目連結: http://codeforces.com/contest/967/problem/C   指有可能是走最近的樓梯或電梯其中一種,但要小心如果在同一層的話要特判。 #include<bits/stdc++.h> # define debug ( a ) cout << #a << " = " << a << endl; # define pb push_back # define int long long # define all ( v ) (v).begin(),(v).end() using namespace std ; typedef pair< int , int > pi; int n,m,cl,ce,v; vector< int > cll,cee; signed main () { cin >> n >> m >> cl >> ce >> v; for(int i = 0; i < cl; i++) { int a; cin >> a; cll. pb ( a); } for(int i = 0; i < ce; i++) { int a; cin >> a; cee. pb ( a); } sort( all ( cll)); sort( all ( cee)); int q; cin >> q; while(q--) { int x11,y11,x22,y22; cin >> x11 >> y11 >> x22 >> y22; swap( x11,y11); swap( x22,y22); if(y11 == y22) { cout << abs(x11-x22) <

[Codeforces] 478 D. Ghosts

題目連結: http://codeforces.com/contest/975/problem/D   列方程式然後解可得:a = (v1-v2)/(w1-w2) => av1-w1 = av2-w2,這樣就好做了,不過要判掉v1 == v2 && w1 == w2的case。 #include<bits/stdc++.h> # define debug ( a ) cout << #a << " = " << a << endl; # define pb push_back # define all ( v ) (v).begin(),(v).end() # define int long long using namespace std ; const int N = 200000 + 9 ; int n,a,b,ans; int v[N], w[N]; pair< int , int > h[N]; int k[N]; main () { cin >> n >> a >> b; for(int i = 0; i < n; i++) cin >> b >> v[i] >> w[i]; for(int i = 0; i < n; i++) h[i] = {v[i],w[i]}; for(int i = 0; i < n; i++) k[i] = v[i]*a-w[i]; sort( k,k+n); int cnt = 0; if(n == 1) { cout << 0 << endl; return 0; } sort( h,h+n); for(int i = 0; i < n; i++) cnt += upper_bound( h+i+1,h+n,h[i])- lower_bound ( h+i+1,h+n,h[i]); //for(int i = 0; i <

[Codeforces] 478 C. Valhalla Siege

題目連結: http://codeforces.com/contest/975/problem/C   巧思一下,運用前綴和的概念之類的。 #include<bits/stdc++.h> # define debug ( a ) cout << #a << " = " << a << endl; # define pb push_back # define all ( v ) (v).begin(),(v).end() # define int long long using namespace std ; int n,q; int a[ 200009 ]; int k[ 200009 ]; main () { cin >> n >> q; for(int i = 1; i <= n; i++) { cin >> a[i]; a[i] += a[i-1]; } int die = 0, sol = 0; for(int i = 1; i <= q; i++) { cin >> k[i]; k[i] += k[i-1]; int d = upper_bound( a+1,a+n+1,k[i])-a; if(d == n+1) { cout << n << endl; k[i] = 0; } else cout << n+1-d << endl; } }