A
int w,b;
void solve(){cin>>w>>b;w*=1000;cout<<w/b+1;
}
B
int n,m;
void solve(){cin>>n>>m;map<int,int>mp;map<int,int>cnt;rep(i,1,n){int a,b;cin>>a>>b;cnt[a]++;mp[a]+=b;}for(int i=1;i<=m;i++){cout<<fixed<<setprecision(20)<<mp[i]*1.0/cnt[i]<<endl;}
}
C
题意:
笨鸟先飞 , 有若干个障碍物 ,初始位置为\(h\) , 每到\(t_i\) ,要求位置在\(l_i\)和\(r_i\)之间 ,求是否可能
思路:
只要维护当前时间最高能飞多少和最低能飞多少即可
int n,h;
int t[M],l[M],r[M];
void solve(){cin>>n>>h;rep(i,1,n){cin>>t[i]>>l[i]>>r[i];}int tmp =0;int L = h,R=h;rep(i,1,n){L-=(t[i]-tmp);R+=(t[i]-tmp);if(L>r[i] || R <l[i]){cout<<"No"<<endl;return;}L = max(L,l[i]);R = min(R,r[i]);tmp=t[i];}cout<<"Yes"<<endl;
}
D
题意:
在2000x2000的方格纸上有若干个矩形 , 对于所有矩形 ,请输出当它消失时方格纸上没有被矩形覆盖的方格个数
思路:
对于\(i\) ,答案就是 没有被任何矩形覆盖的方格个数 + 只被\(i\)覆盖的方格个数
想到二维差分,不妨给每一个矩形赋值一个哈希值,这样有多个矩形覆盖的方格值和只有某个矩形覆盖的方格值有所不同
int g[2005][2005];
int n;
ull ha[M];
int res[M];
map<ull,int>mp;
void solve(){cin>>n;for(int i=1;i<=n;i++){ha[i] =rnd();mp[ha[i]]=i;}//(u,l) (d,r)auto work =[&](int x,int y,int a,int b,int s)->void{int xx= x+a,yy=y+b;g[x][y]+=s;g[xx][y]-=s;g[x][yy]-=s;g[xx][yy]+=s;};rep(i,1,n){int u,d,l,r;cin>>u>>d>>l>>r;int a=d-u+1;int b=r-l+1;work(u,l,a,b,ha[i]);}int sum = 0;for(int i=1;i<=2000;i++){for(int j=1;j<=2000;j++){g[i][j]=g[i][j]+g[i-1][j]+g[i][j-1]-g[i-1][j-1];if(mp.count(g[i][j])){res[mp[g[i][j]]]++;}if(g[i][j]==0){sum++;}}}rep(i,1,n){cout<<sum+res[i]<<endl;}
}
E
题意:
\(n\)个兔子 ,每个兔子位置在\(x_i\),能跳\(r_i\)距离 ,求每一个兔子都跳一次 ,位置坐标不同的个数最大值
思路:
二分图最大匹配
int n;
int x[M], r[M];
struct HopcroftKarp {int nL, nR;vector<vector<int>> adj; vector<int> dist, pairU, pairV;HopcroftKarp(int _nL, int _nR): nL(_nL), nR(_nR) {adj.assign(nL+1, {});pairU.assign(nL+1, 0);pairV.assign(nR+1, 0);dist.assign(nL+1, 0);}void addEdge(int u, int v) {adj[u].push_back(v);}bool bfs() {queue<int> q;for (int u = 1; u <= nL; ++u) {if (pairU[u] == 0) {dist[u] = 0;q.push(u);} else dist[u] = INT_MAX;}bool reachableFree = false;while (!q.empty()) {int u = q.front(); q.pop();for (int v : adj[u]) {int pu = pairV[v];if (pu == 0) {reachableFree = true;} else if (dist[pu] == INT_MAX) {dist[pu] = dist[u] + 1;q.push(pu);}}}return reachableFree;}bool dfs(int u) {for (int v : adj[u]) {int pu = pairV[v];if (pu == 0 || (dist[pu] == dist[u] + 1 && dfs(pu))) {pairU[u] = v;pairV[v] = u;return true;}}dist[u] = INT_MAX;return false;}int maxMatching() {int result = 0;while (bfs()) {for (int u = 1; u <= nL; ++u) {if (pairU[u] == 0) {if (dfs(u)) ++result;}}}return result;}
};void solve()
{cin >> n;vector<int>v;rep(i, 1, n) {cin>>x[i]>>r[i];v.pb(x[i]-r[i]);v.pb(x[i]+r[i]);}sort(v.begin(),v.end());v.erase(unique(v.begin(),v.end()),v.end());int ans = 0;int m=v.size();HopcroftKarp hk(n, m);for(int i=1;i<=n;i++){int a=x[i]-r[i];int b=x[i]+r[i];int ida = (lower_bound(v.begin(), v.end(), a) - v.begin()) + 1;int idb = (lower_bound(v.begin(), v.end(), b) - v.begin()) + 1;hk.addEdge(i,ida);if(ida!=idb)hk.addEdge(i,idb);}cout << hk.maxMatching();
}