目录
B. Queries about less or equal elements
D. Area of Two Circles' Intersection
A. Extract Numbers
Solution:按照题意模拟,细节很多,也可以算是双指针找符合题目要求的序列。
#include<iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include<stack>
#include <unordered_map>
using namespace std;
typedef long long LL;
typedef unsigned long long uLL;
typedef pair<int, int> PII;
const int mod = 998244353;
const int N = 1e9+10;
const int inf = 0x3f3f3f3f;
LL gcd(LL a, LL b)
{
return b ? gcd(b, a % b) : a;
}
int lowbit(int x) {return x&-x;}
void solve()
{
string s;cin>>s;
vector<string>a,b;
int n=s.size();
for(int i=0;i<n;i++){
int j=i,st=i;
string t="";bool fl=false;
while((s[j]!=','&&s[j]!=';')&&j<n) {
if(!(s[j]>='0'&&s[j]<='9')) fl=true;
t+=s[j];j++;
}
if(t=="") {
b.push_back(t);
}
else{
if(!fl) {
if(t=="0") a.push_back(t);
else{
if(t[0]!='0') a.push_back(t);
else b.push_back(t);
}
}
else b.push_back(t);
}
i=j;
}
if(s[n-1]==','||s[n-1]==';') b.push_back("");
if(!a.size()) cout<<'-'<<'\n';
else{
// cout<<a.size()<<'\n';
cout<<'"';
for(int i=0;i<a.size();i++) {
cout<<a[i];
if(i<a.size()-1) cout<<",";
}
cout<<'"'<<'\n';
}
if(!b.size()) cout<<'-'<<'\n';
else {
// cout<<b.size()<<'\n';
cout<<'"';
for(int i=0;i<b.size();i++) {
cout<<b[i];
if(i!=b.size()-1) cout<<",";
}
cout<<'"'<<'\n';
}
}
int main()
{
ios::sync_with_stdio(0); cin.tie(0),cout.tie(0);
int t;cin>>t;
while(t--) solve();
}
B. Queries about less or equal elements
Solution:对a数组排序后二分,直接使用upper_bound()函数。
#include<iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include<stack>
#include <unordered_map>
using namespace std;
typedef long long LL;
typedef unsigned long long uLL;
typedef pair<int, int> PII;
const int mod = 998244353;
const int N = 1e9+10;
const int inf = 0x3f3f3f3f;
LL gcd(LL a, LL b)
{
return b ? gcd(b, a % b) : a;
}
int lowbit(int x) {return x&-x;}
void solve()
{
int n,m;
cin>>n>>m;
vector<int>a(n),b(m);
for(int i=0;i<n;i++) cin>>a[i];
for(int i=0;i<m;i++) cin>>b[i];
sort(a.begin(),a.end());
for(int i=0;i<m;i++){
int pos=upper_bound(a.begin(),a.end(),b[i])-a.begin();
if(pos==m){
cout<<m<<" ";
}else{
cout<<pos<<" ";
}
}
}
int main()
{
ios::sync_with_stdio(0); cin.tie(0),cout.tie(0);
int t;cin>>t;
while(t--) solve();
}
C. Make Palindrome
Solution:构造题+贪心,题目给出一个字符串,要求最小操作数构造出字典序最小的回文字符串,每次操作可以改一个字符。由于回文串是对称的,因此我们可以统计字符串出现次数,偶数直接按字典序一半左边一半右边,对于出现次数为奇数,可以两两配对,把一个奇数次数的加一,另一个配对的次数减一,则次数全是偶数,直接使用。再分类讨论一下出现次数为奇数的字符个数,如果为奇数一定存在一个无法配对,因此将其中一个放在中间即可。剩下的可以使用。这样构造出来的即为最优解。
#include<iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include<stack>
#include <unordered_map>
using namespace std;
typedef long long LL;
typedef unsigned long long uLL;
typedef pair<int, int> PII;
const int mod = 998244353;
const int N = 1e9+10;
const int inf = 0x3f3f3f3f;
LL gcd(LL a, LL b)
{
return b ? gcd(b, a % b) : a;
}
int lowbit(int x) {return x&-x;}
void solve()
{
string s;cin>>s;
int n=s.size();
map<char,int>mp;
for(int i=0;i<n;i++) mp[s[i]]++;
vector<char>res;
for(char i='a';i<='z';i++){
if(mp[i]&1){
res.push_back(i);
}
}
// for(auto t:res){ cout<<t<<" ";}
for(int i=0;i<res.size()/2;i++){
mp[res[i]]++;
mp[res.size()-1-i]--;
}
string ans="";
if(res.size()&1){
ans+=res[res.size()/2];
}
for(int i='z';i>='a';i--){
ans=string(mp[i]/2,i)+ans+string(mp[i]/2,i);
}
cout<<ans<<'\n';
}
int main()
{
ios::sync_with_stdio(0); cin.tie(0),cout.tie(0);
int t;cin>>t;
while(t--) solve();
}
D. Area of Two Circles' Intersection
Solution:计算几何题板子题,余弦定理求圆相交面积。(注意精度问题)
#include<iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include<iomanip>
using namespace std;
typedef long long LL;
typedef unsigned long long uLL;
typedef pair<int, int> PII;
const int mod = 998244353;
const int N = 1e9+10;
const int inf = 0x3f3f3f3f;
const long double Pi=acos(-1.0);
LL gcd(LL a, LL b)
{
return b ? gcd(b, a % b) : a;
}
int lowbit(int x) {return x&-x;}
void solve()
{
long double x1,y1,r1,x2,y2,r2;
cin>>x1>>y1>>r1>>x2>>y2>>r2;
long double d=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
//cout<<d<<'\n';
cout<<fixed<<setprecision(10);
if(d>=r1+r2){
cout<<0<<'\n';
return;
}
if(r1+d<=r2){
cout<<Pi*r1*r1<<'\n';
return;
}
if(r2+d<=r1){
cout<<Pi*r2*r2<<'\n';
return;
}
//cout<<fixed<<setprecision(10);
long double ans=0;
long double a1=acos((r1*r1+d*d-r2*r2)/(2*r1*d));
//cout<<a1<<'\n';
ans+=r1*r1*a1;
ans-=r1*sin(a1)*r1*cos(a1);
long double a2=acos((r2*r2+d*d-r1*r1)/(2*r2*d));
ans+=r2*r2*a2;
ans-=r2*sin(a2)*r2*cos(a2);
cout<<ans<<'\n';
}
int main()
{
ios::sync_with_stdio(0); cin.tie(0),cout.tie(0);
solve();
}
E. Lomsat gelral
Solution:树上启发式合并板子题(dsu on tree)。
#include<iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include<stack>
#include <unordered_map>
using namespace std;
typedef long long LL;
typedef unsigned long long uLL;
typedef pair<int, int> PII;
const int mod = 998244353;
const int N = 1e5+10;
const int inf = 0x3f3f3f3f;
LL gcd(LL a, LL b)
{
return b ? gcd(b, a % b) : a;
}
int lowbit(int x) {return x&-x;}
vector<LL>v[N],col(N),siz(N),son(N),cnt(N),ans(N);//siz记录重儿子的子树有的结点个数量,son记录重链
LL sum,flag,maxc;
void dfs_1(int u,int f){//重链剖分
siz[u]=1;
for(auto t:v[u]){
if(t==f) continue;
dfs_1(t,u);
siz[u]+=siz[t];
if(siz[t]>siz[son[u]]){//更新重链
son[u]=t;
}
}
}
void count(int u,int f ,int val){
cnt[col[u]]+=val;
if(cnt[col[u]]>maxc){
maxc=cnt[col[u]];
sum=col[u];
}
else if(cnt[col[u]]==maxc) sum+=col[u];
for(auto t:v[u]){
if(t==f||t==flag) continue;
count(t,u,val);
}
}
void dfs_2(int u,int f,bool keep){//dsu on tree
//先计算轻链
for(auto t:v[u]){
if(t==f||t==son[u]) continue;
dfs_2(t,u,false);
}
//算重链
if(son[u]) {
dfs_2(son[u],u,true);
flag=son[u];
}
count(u,f,1);
flag=0;ans[u]=sum;
//删除轻链贡献
if(!keep){
count(u,f,-1);
sum=maxc=0;
}
}
void solve()
{
int n;cin>>n;
for(int i=1;i<=n;i++) cin>>col[i];
int x,y;
for(int i=0;i<n-1;i++){
cin>>x>>y;
v[x].push_back(y);
v[y].push_back(x);
}
dfs_1(1,0);
dfs_2(1,0,0);
for(int i=1;i<=n;i++) cout<<ans[i]<<" ";
}
int main()
{
ios::sync_with_stdio(0); cin.tie(0),cout.tie(0);
solve();
}