Exam Results
双指针写法
/*
*@author spnooyseed
*/
#pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline")
#pragma GCC optimize(3 , "Ofast" , "inline")
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <unordered_map>
#include <vector>
#include <map>
#include <list>
#include <queue>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <stack>
#include <set>
#include <bitset>
#include <deque>
using namespace std ;
#define ios ios::sync_with_stdio(false) , cin.tie(0)
#define x first
#define y second
#define pb push_back
#define ls rt << 1
#define rs rt << 1 | 1
typedef long long ll ;
const double esp = 1e-6 , pi = acos(-1) ;
typedef pair<int , int> PII ;
const int N = 1e6 + 10 , INF = 0x3f3f3f3f , mod = 1e9 + 7;
PII a[N] ;
int ca = 0 , b[N] ;
vector<PII> v ;
int work()
{
int n , p ;
cin >> n >> p ;
v.clear() ;
for(int i = 1; i <= n ;i ++ ) {
cin >> a[i].x >> a[i].y ;
v.push_back({a[i].x , i}) ;
v.push_back({a[i].y , i}) ;
if(a[i].x > a[i].y) swap(a[i].x , a[i].y) ;
}
sort(v.begin() , v.end()) ;
sort(a + 1 , a + n + 1) ;
for(int i = 1; i <= n ;i ++ ) b[i] = 0 ;
int cnt = 0 , ans = 0 ;
for(int r = 0 , l = 0 ;r < v.size() ; r ++ ) {
b[v[r].y] ++ ;
if(b[v[r].y] == 1) cnt ++ ;
if(v[r].x < a[n].x) continue ;
ll xx = v[r].x * p ;
while(l < v.size() && 1ll * v[l].x * 100 < xx) {
b[v[l].y] -- ;
if(b[v[l].y] == 0) cnt -- ;
l ++ ;
}
ans = max(ans , cnt) ;
}
cout << "Case #" << ++ ca << ": " << ans << endl ;
return 0 ;
}
int main()
{
// freopen("C://Users//spnooyseed//Desktop//in.txt" , "r" , stdin) ;
// freopen("C://Users//spnooyseed//Desktop//out.txt" , "w" , stdout) ;
int n ;
cin >> n ;
while(n --)
work() ;
return 0 ;
}
/*
*/
主席树加二分
/*
*@author spnooyseed
*/
#pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline")
#pragma GCC optimize(3 , "Ofast" , "inline")
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <unordered_map>
#include <vector>
#include <map>
#include <list>
#include <queue>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <stack>
#include <set>
#include <bitset>
#include <deque>
using namespace std ;
#define ios ios::sync_with_stdio(false) , cin.tie(0)
#define x first
#define y second
#define pb push_back
#define ls rt << 1
#define rs rt << 1 | 1
typedef long long ll ;
const double esp = 1e-6 , pi = acos(-1) ;
typedef pair<ll , ll> PII ;
const int N = 2e5 + 10 , INF = 0x3f3f3f3f , mod = 1e9 + 7;
int ca = 0 ;
PII a[N] ;
ll b[N] , c[N] ;
int root[N * 45] , tot ;
struct node {
int l , r , sum ;
}t[N * 45] ;
void build(int &now , int l , int r){
t[now = ++ tot].sum = 0 ;
if(l == r) return ;
int mid = l + r >> 1 ;
build(t[now].l , l , mid) ;
build(t[now].r , mid + 1 , r) ;
return ;
}
void up(int now){
t[now].sum = t[t[now].l].sum + t[t[now].r].sum ;
}
void update(int &now , int last , int l , int r , int pos){
t[now = ++ tot] = t[last] ;
if(l == r) {
t[now].sum ++ ;
return ;
}
int mid = l + r >> 1 ;
if(pos <= mid) update(t[now].l , t[last].l , l , mid , pos) ;
else update(t[now].r , t[last].r , mid + 1 , r , pos) ;
up(now) ;
return ;
}
int ask(int now , int last , int l , int r , int k){
if(!k) return 0 ;
if(r <= k) return t[now].sum - t[last].sum ;
int mid = l + r >> 1 , ans = 0 ;
if(k <= mid) ans += ask(t[now].l , t[last].l , l , mid , k) ;
else ans += ask(t[now].l , t[last].l , l , mid , k) + ask(t[now].r , t[last].r , mid + 1 , r , k) ;
return ans ;
}
ll d[N] ;
int work()
{
int n , p , cnt = 0;
scanf("%d%d" , &n , &p) ;
tot = 0 ;
for(int i = 1; i <= n ;i ++ ) {
scanf("%lld%lld" , &a[i].x , &a[i].y) ;
a[i].x *= 100l , a[i].y *= 100l ;
if(a[i].x > a[i].y) swap(a[i].x , a[i].y) ;
}
sort(a + 1 , a + n + 1) ;
for(int i = 1; i <= n ;i ++ ) c[i] = a[i].x ;
c[n + 1] = 1e12 ;
for(int i = 1; i <= n ;i ++ ) {
if(a[i].x >= a[n].x) b[++ cnt] = a[i].x ;
if(a[i].y >= a[n].x) b[++ cnt] = a[i].y ;
}
sort(b + 1 , b + cnt + 1) ;
for(int i = 1; i <= n ;i ++ ) d[i] = a[i].y ;
sort(d + 1 , d + n + 1) ;
d[n + 1] = 1e12 ;
for(int i = 1; i <= n ;i ++ )
a[i].y = lower_bound(d + 1 , d + n + 1 , a[i].y) - d ;
build(root[0] , 1 , n) ;
for(int i = 1; i <= n ;i ++ )
update(root[i] , root[i - 1] , 1 , n , a[i].y) ;
ll ans = 0 ;
for(int i = 1; i <= cnt; i ++ ) {
ll x = b[i] / 100l ;
ll t = lower_bound(c + 1 , c + n + 1 , 1ll * x * p) - c;
ll y1 = lower_bound(d + 1 , d + n + 1 , 1ll * b[i]) - d ;
y1 = min(1ll * n , y1) ;
if(d[y1] > b[i]) y1 -- ;
ll y2 = upper_bound(d + 1 , d + n + 1 , 1ll * x * p - 1) - d - 1 ;
ll res = 0 ;
if(t - 1 > 0)
res = ask(root[t - 1] , root[0] , 1 , n , y1) - ask(root[t - 1] , root[0] , 1 , n , y2) ;
ans = max(ans , n - t + 1 + res) ;
}
cout << "Case #" << ++ ca << ": " << ans << endl ;
return 0 ;
}
int main()
{
// freopen("C://Users//spnooyseed//Desktop//in2.txt" , "r" , stdin) ;
// freopen("C://Users//spnooyseed//Desktop//out.txt" , "w" , stdout) ;
int n ;
cin >> n ;
while(n --)
work() ;
return 0 ;
}
/*
1
9 97
564 6736
3658 28141
4595 16759
5441 28611
7014 30529
14225 26768
15885 24472
20431 25127
21544 32369
*/
Kingdom's Power
树形dp
/*
*@author spnooyseed
*/
#pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline")
#pragma GCC optimize(3 , "Ofast" , "inline")
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <unordered_map>
#include <vector>
#include <map>
#include <list>
#include <queue>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <stack>
#include <set>
#include <bitset>
#include <deque>
using namespace std ;
#define ios ios::sync_with_stdio(false) , cin.tie(0)
#define x first
#define y second
#define pb push_back
#define ls rt << 1
#define rs rt << 1 | 1
typedef long long ll ;
const double esp = 1e-6 , pi = acos(-1) ;
typedef pair<int , int> PII ;
const int N = 1e6 + 10 , INF = 0x3f3f3f3f , mod = 1e9 + 7;
vector<int> v[N] ;
int deep[N] ;
void dfs(int u) {
int maxn = 0 ;
for(auto x : v[u]) {
dfs(x) ;
maxn = max(maxn , deep[x]) ;
}
deep[u] = maxn + 1 ;
return ;
}
bool cmp(int a , int b) {
return deep[a] < deep[b] ;
}
int ca = 0 ;
int a[N] ;
ll dfs(int u , int f , int dep) {
a[u] = a[f] + 1 ;
ll maxn = a[u] ;
for(int i = 0 ;i < v[u].size() ;i ++ ) {
int x = v[u][i] ;
if(x == f) continue ;
maxn = max(maxn , dfs(x , u , dep + 1));
if(i != v[u].size() - 1)
a[u] = maxn + min(deep[x] , dep);
}
return maxn ;
}
int work()
{
int n ;
scanf("%d" , &n) ;
for(int i = 1; i <= n ; i ++ ) v[i].clear() ;
for(int i = 2; i <= n ;i ++ ) {
int x ;
scanf("%d" , &x) ;
v[x].push_back(i) ;
}
dfs(1) ;
for(int i = 1; i <= n ;i ++ )
if(v[i].size())
sort(v[i].begin() , v[i].end() , cmp) ;
ll ans = 0 ;
for(auto x : v[1])
ans += 1ll * dfs(x , 1 , 1) ;
cout << "Case #" << ++ ca << ": " << ans << endl ;
return 0 ;
}
int main()
{
// freopen("C://Users//spnooyseed//Desktop//in.txt" , "r" , stdin) ;
// freopen("C://Users//spnooyseed//Desktop//out.txt" , "w" , stdout) ;
int n ;
cin >> n ;
while(n --)
work() ;
return 0 ;
}
/*
1
15
1 2 3 3 2 6 1 8 8 9 10 10 10 8
Case #1: 20
*/