Educational Codeforces Round 2 个人总结A-E
Educational Codeforces Round 2
A. Extract Numbers
- 简单的模拟
bool check(string op)
{
if(op.size()==1&&op[0]=='0')
return true;
if(op.size()==0||(op[0]<'1'||op[0]>'9'))
return false;
bool st=false;
for(int i=1;i<op.size();i++)
{
if( !(op[i]>='0'&&op[i]<='9'))
st=true;
}
if(st)
return false;
else return true;
}
void solve()
{
string op; cin>>op;
string t="";
vector<string> a;
int len=op.size();
for(int i=0;i<len;i++)
{
if(op[i]!=','&&op[i]!=';')
t=t+op[i];
else
{
a.pb(t);
t="";
}
}
if(!t.empty())
a.pb(t);
if(op[op.size()-1]==','||op[op.size()-1]==';')
a.pb("");
vector<string> b,c;
for(auto it:a)
{
if(check(it))
b.pb(it);
else
c.pb(it);
}
if(b.size()!=0)
{
cout<<"\"";
len=b.size();
for(int i=0;i<len;i++)
{
cout<<b[i];
if(i!=len-1)
cout<<",";
}
cout<<"\""<<endl;
}
else
cout<<"-\n";
if(c.size()!=0)
{
cout<<"\"";
len=c.size();
for(int i=0;i<len;i++)
{
cout<<c[i];
if(i!=len-1)
cout<<",";
}
cout<<"\""<<endl;
}
else
cout<<"-\n";
return;
}
B. Queries about less or equal elements
- 二分查找板子
vector<LL> a,b;
void solve()
{
int n,m; cin>>n>>m;
for(int i=1;i<=n;i++)
{
int x; cin>>x;
a.pb(x);
}
sort(all(a));
for(int i=1;i<=m;i++)
{
int x; cin>>x;
b.pb(x);
}
for(int i=0;i<m;i++)
{
int l=0,r=n-1;
while(l<r)
{
int mid=(l+r+1)>>1;
if(a[mid]<=b[i]) l=mid;
else r=mid-1;
}
if(l==0&&b[i]<a[l])
cout<<0<<" ";
else
cout<<l+1<<" ";
}
cout<<endl;
return;
}
C. Make Palindrome
大意:输出使 s 为回文串,在操作次数最少的情况下,s 的字典序最小的字符串
根据回文串的性质,若不是回文串,即有一个或多个字母个数为奇数,为使操作次数最少,即将这些单个字母取出来,有区间 \(b[l,...r]\)从两端进行修改操作,修改操作为 \(b[r]=b[l]\) \((l<r)\)
将 \(b\) 放回 \(s\),重新构造一下就出来了
int cnt[30],n;
string op;
vector<int> b;
void solve()
{
cin>>op; n=op.size();
for(int i=0;i<n;i++)
cnt[int(op[i]-'a'+1)]++;
for(int i=1;i<=26;i++)
if(cnt[i]%2==1)
{
cnt[i]--;
b.pb(i);
}
int l=0,r=b.size()-1;
while(l<r)
b[r]=b[l],l++,r--;
for(auto it:b)
cnt[it]++;
string ans="";
char t;
for(int i=1;i<=26;i++)
{
int k=cnt[i]/2;
if(cnt[i]%2==1)
t=char('a'+i-1);
while(k--)
ans.pb(char('a'+i-1));
}
string res=ans;
reverse(all(res));
cout<<ans;
if(n%2==1)
cout<<t;
cout<<res<<endl;
return;
}
D. Area of Two Circles' Intersection
- 高中数学知识,用到了正弦定理和余弦定理,然后可以试着去做
- 这题WA34是由于精度原因,计算几何题要避免浮点数的子运算或加法
long double xa,ya,ra,xb,yb,rb;
void solve()
{
cin>>xa>>ya>>ra>>xb>>yb>>rb;
if(ra>rb)
{
swap(xa,xb);
swap(ya,yb);
swap(ra,rb);
}
long double len=sqrt((xa-xb)*(xa-xb)+(ya-yb)*(ya-yb));
if(ra+rb<len)
cout<<0<<endl;
else if(rb>=len+ra)
{
long double ans=ra*ra*pi;
cout<<setprecision(10)<<ans<<endl;
}
else
{
long double ang1=2.0*acos((len*len+rb*rb-ra*ra)/(2.0*len*rb));
long double ang2=2.0*acos((len*len+ra*ra-rb*rb)/(2.0*len*ra));
long double ans=ang1*rb*rb/2.0+ang2*ra*ra/2.0-0.5*sin(ang1)*rb*rb-0.5*sin(ang2)*ra*ra;
cout<<setprecision(10)<<ans<<endl;
}
return;
}
E. Lomsat gelral
是一道很入门的dsu,去找dls课看,或者oiwiki
// AC one more times
////////////////////////////////////////INCLUDE//////////////////////////////////////////
#include <bits/stdc++.h>
using namespace std;
/////////////////////////////////////////DEFINE//////////////////////////////////////////
#define fi first
#define se second
#define pb push_back
#define endl '\n'
#define all(x) (x).begin(), (x).end()
#define inf64 0x3f3f3f3f3f3f3f3f
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef pair<long long,long long> PLL;
////////////////////////////////////////CONST////////////////////////////////////////////
const int inf = 0x3f3f3f3f;
const int maxn = 2e6 + 6;
const double eps = 1e-8;
///////////////////////////////////////FUNCTION//////////////////////////////////////////
template<typename T>
void init(T q[], int n, T val){ for (int i = 0; i <= n; i++) q[i] = val; }
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
bool cmp(int c, int d) { return c > d; }
//inline __int128 read(){__int128 x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-'){f=-1;}ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;}
//inline void write(__int128 x){if (x < 0){putchar('-');x = -x;}if (x > 9) write(x / 10);putchar(x % 10 + '0');}
LL get_quick_mod(LL a,LL b,LL p){ LL ans=1%p;while(b){ if(b&1) {ans=ans*a%p;} a=a*a%p,b>>=1; } return ans; }
const int mod = 1e9+7;
const int N = 1e5+10;
const int M = 1e6+10;
int n,c[N],sz[N],big[N],id[N],l[N],r[N],tot;
vector<int> e[N];
LL cnt[N],maxcnt,sumcnt,ans[N];
void dfs1(int u,int fa)
{
l[u]=++tot;
id[tot]=u;
sz[u]=1;
big[u]=-1;
for(int v:e[u])
{
if(v==fa) continue;
dfs1(v,u);
sz[u]+=sz[v];
if(big[u]==-1||sz[v]>sz[big[u]])
big[u]=v;
}
r[u]=tot;
}
void add(int x)
{
x=c[x];
cnt[x]++;
if(cnt[x]>maxcnt) maxcnt=cnt[x],sumcnt=0;
if(cnt[x]==maxcnt) sumcnt+=x;
}
void del(int x)
{
x=c[x];
cnt[x]--;
}
void dfs2(int u,int fa,bool st)
{
for(int v:e[u])
if(v!=fa&&v!=big[u])
dfs2(v,u,false);
if(big[u]!=-1)
dfs2(big[u],u,true);
for(int v:e[u])
{
if(v!=fa&&v!=big[u])
for(int x=l[v];x<=r[v];x++)
add(id[x]);
}
add(u);
ans[u]=sumcnt;
if(!st)
{
maxcnt=0;sumcnt=0;
for(int x=l[u];x<=r[u];x++)
del(id[x]);
}
}
void solve()
{
cin>>n;
for(int i=1;i<=n;i++)
cin>>c[i];
for(int i=1;i<n;i++)
{
int u,v; cin>>u>>v;
e[u].pb(v); e[v].pb(u);
}
dfs1(1,0);
dfs2(1,0,false);
for(int i=1;i<=n;i++)
cout<<ans[i]<<" ";
cout<<endl;
return;
}
int main()
{
std::ios::sync_with_stdio(false); cin.tie(nullptr), cout.tie(nullptr);
// 特殊输入 请注释上方,如__int128等
int TC = 1;
//cin >> TC;
for(int tc = 1; tc <= TC; tc++)
{
//cout << "Case #" << tc << ": ";
solve();
}
return 0;
}
Educational Codeforces Round 2 个人总结A-E的更多相关文章
- [Educational Codeforces Round 16]E. Generate a String
[Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...
- [Educational Codeforces Round 16]D. Two Arithmetic Progressions
[Educational Codeforces Round 16]D. Two Arithmetic Progressions 试题描述 You are given two arithmetic pr ...
- [Educational Codeforces Round 16]C. Magic Odd Square
[Educational Codeforces Round 16]C. Magic Odd Square 试题描述 Find an n × n matrix with different number ...
- [Educational Codeforces Round 16]B. Optimal Point on a Line
[Educational Codeforces Round 16]B. Optimal Point on a Line 试题描述 You are given n points on a line wi ...
- [Educational Codeforces Round 16]A. King Moves
[Educational Codeforces Round 16]A. King Moves 试题描述 The only king stands on the standard chess board ...
- Educational Codeforces Round 6 C. Pearls in a Row
Educational Codeforces Round 6 C. Pearls in a Row 题意:一个3e5范围的序列:要你分成最多数量的子序列,其中子序列必须是只有两个数相同, 其余的数只能 ...
- Educational Codeforces Round 9
Educational Codeforces Round 9 Longest Subsequence 题目描述:给出一个序列,从中抽出若干个数,使它们的公倍数小于等于\(m\),问最多能抽出多少个数, ...
- Educational Codeforces Round 37
Educational Codeforces Round 37 这场有点炸,题目比较水,但只做了3题QAQ.还是实力不够啊! 写下题解算了--(写的比较粗糙,细节或者bug可以私聊2333) A. W ...
- Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship
Problem Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...
- Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)
Problem Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems Time Limit: 3000 mSec P ...
随机推荐
- python34
Python成员运算符 除了以上的一些运算符之外,Python还支持成员运算符,测试实例中包含了一系列的成员,包括字符串,列表或元组. 运算符 描述 实例 in 如果在指定的序列中找到值返回 True ...
- shell mv cp image in parallel 多线程解压parallel
# apt install parallel # mkdir -p 1Kx1K/img # ls 1Kx1K/img_9*.jpg |parallel -j 80 mv {} 1Kx1K/img ht ...
- IDEA使用fastjson1时maven引入依赖没报错,但是用不了JSONObject工具类
删除项目下的.idea文件夹重新打开项目就行, 不知道为什么
- 解决vscode中,powershell中conda activate无效--更改vscode默认的shell为anaconda shell
问题记录: windows系统里,cmd可以正常使用conda activate 命令,但是在powershell中,使用conda activate既不报错(说明路径没问题),也没激活conda环境 ...
- Fiddle 简单用法
下载安装后,还要下载证书放到浏览器 https://zhuanlan.zhihu.com/p/439203346
- Python——01.环境及安装
Python介绍 -- Python是解释型,面向对象的语言,程序结构简洁,清晰 -- Python解释器分类: CPython(官方解释器):用C语言编写的Python解释器 PyPy:用Pytho ...
- VBoxNetAdpCtl: Error while adding new interface: failed to open /dev/vboxnetctl: No such file or directory.
macOS VirtualBox Bridged Adapter 不能用 I'm running macOS High Sierra 10.13.1 and VirtualBox 5.2.2. Thi ...
- 三步建立自己域名的主页,Github Pages功能简明手册
[task]把自己的页面上传到git上,用github pages功能托管网页. 准备工作: 1.自己的网页文件 2.有个自己的git账号 3.下载安装git.下载地址https://git-scm. ...
- react组件传参记录,防止以后忘记,欢迎指正讨论
c: 'Celsius', f: 'Fahrenheit' }; function toCelsius(fahrenheit) { return (fahrenheit - 32) * 5 / 9; ...
- 为什么gdb通过0地址显示偏移会提示地址错误
现象 在gdb中,如果想看一个struct的某个field的偏移量,和C语言一样可以通过对一个0地址变量取地址,然后取成员的偏移量获得.更神奇的地方在于和C语言一样,这里也不会触发内存访问异常. 另外 ...