您的位置:首页 > 其它

[Updating]Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals)解题报告

2017-03-06 08:13 411 查看
A-Andryusha and Socks

模拟一遍即可

//Author: Lixiang
#include<stdio.h>
const int maxn=200001;
inline int max(int a,int b){
return a>b?a:b;
}
struct Andryusha_and_Socks{
bool hash[maxn];
int N,ans;
void init(){
scanf("%d",&N);
N*=2;
int cnt=0;
for(int i=1,tmp;i<=N;i++){
scanf("%d",&tmp);
if(hash[tmp]){
hash[tmp]=0;
cnt--;
}
else{
hash[tmp]=1;
cnt++;
ans=max(cnt,ans);
}
}
}
void work(){
printf("%d\n",ans);
}
}sol;
int main(){
sol.init();
sol.work();
return 0;
}

B-The Meeting Place Cannot Be Changed

二分+模拟(最近CF怎么这么多二分)

//Author: Lixiang
#include<stdio.h>
const double eps=1e-6;
const int maxn=60001;
template <class T>
inline T abs(T a){
return a>0?a:-a;
}
struct The_Meeting_Place_Cannot_Be_Changed{
double v[maxn],sum;
int x[maxn],N;
void init(){
scanf("%d",&N);
for(int i=1;i<=N;i++)
scanf("%d",&x[i]),sum+=x[i];
for(int i=1;i<=N;i++)
scanf("%lf",&v[i]);
}
double get(double pos){
double s=0;
for(int i=1;i<=N;i++)
s+=abs(x[i]-pos)/v[i];
return s;
}
bool check(double tim){
double L=x[1]-v[1]*tim,R=x[1]+v[1]*tim,l,r;
for(int i=2;i<=N;i++){
l=x[i]-v[i]*tim;r=x[i]+v[i]*tim;
if(l>R)return 0;
if(r<L)return 0;
if(l>L)L=l;
if(r<R)R=r;
}
return 1;
}
void work(){
double L=0,R=get(sum/(1.0*N)),M;
double ans=R;
while((R-L)>eps){
M=(L+R)/2.0;
if(check(M)){
ans=M;
R=M;
}
else L=M;
}
printf("%.12lf\n",ans);
}
}sol;
int main(){
sol.init();
sol.work();
return 0;
}
C-Andryusha and Colored Balloons

就是给每个点染色,需保证和每个点存在一条长度<=2的任意一个节点的颜色都与这个点不一样,

dfs,对于每一个点先把它所有相邻节点染上色,再继续dfs

//Author: Lixiang
#include<stdio.h>
#include<vector>
using namespace std;
const int maxn=200001;
inline int max(int a,int b){
return a>b?a:b;
}
struct Andryusha_and_Colored_Balloons{
vector <int> ADJL[maxn];
int fa[maxn],col[maxn];
int N,ans;
void init(){
scanf("%d",&N);
for(int i=1,s,t;i<N;i++){
scanf("%d%d",&s,&t);
ADJL[s].push_back(t);
ADJL[t].push_back(s);
}
}
void dfs(int id){
vector <int>::iterator it;
int now=1;
for(it=ADJL[id].begin();it!=ADJL[id].end();it++){
if(*it==fa[id])continue;
fa[*it]=id;
while(col[id]==now||col[fa[id]]==now)now++;
col[*it]=now;
now++;
}
ans=max(now-1,ans);
for(it=ADJL[id].begin();it!=ADJL[id].end();it++)
if(*it!=fa[id])
dfs(*it);

}
void work(){
col[1]=1;
dfs(1);
printf("%d\n",ans);
for(int i=1;i<N;i++)
printf("%d ",col[i]);
printf("%d\n",col
);
}
}sol;
int main(){
sol.init();
sol.work();
return 0;
}


D-Innokenty and a Football League

大模拟啊

读进来就截取first name的前3位,second name的第一位

按照first name排序

如果同一个first name有多个队伍,则只能选择second option。

再注意下冲突处理就行了。

PS:这题数据似乎挺弱的,我一开始冲突处理完全写错了还过了15个点。

//Author: Lixiang
#include<stdio.h>
#include<string>
#include<map>
#include<algorithm>
#include<iostream>
using namespace std;
const int maxn=5000;
struct Name{
string first,second;
string name;
int id;
bool operator <(Name b)const {
if(first==b.first)return second[0]<b.second[0];
else return first<b.first;
}
void in(int i){
cin>>first>>second;
first.erase(3);
second.erase(1);
id=i;
}
void firstoption(){
name=first;
}
void secondoption(){
name=first;
name.erase(2);
name+=second;
}
};
struct Innokenty_and_a_Football_League{
map <string,int> hash;
Name a[maxn];
bool ok[maxn];
int tmp[maxn],N;
void init(){
scanf("%d",&N);
for(int i=1;i<=N;i++)
a[i].in(i);
sort(a+1,a+N+1);
}
bool check(){
int st,ed,id,tid;
for(int i=1;i<=N;i++){
st=ed=i;
while(ed<N&&a[ed].first==a[ed+1].first)
ed++;
if(st==ed){
a[i].firstoption();
bool flag=1;
id=hash[a[i].name];
hash[a[i].name]=i;
if(id>0){
while(id>0&&ok[id]){
a[id].secondoption();
ok[id]=0;
tid=id;
id=hash[a[id].name];
hash[a[tid].name]=tid;
}
if(id>0&&!ok[id])flag=0;
}
if(flag){
ok[i]=1;
continue;
}
a[i].secondoption();
id=hash[a[i].name];
hash[a[i].name]=i;
if(id>0){
while(id>0&&ok[id]){
a[id].secondoption();
ok[id]=0;
tid=id;
id=hash[a[id].name];
hash[a[tid].name]=tid;
}
if(id>0&&!ok[id])return 0;
}
}
else{
for(int j=st;j<=ed;j++){
a[j].secondoption();
ok[j]=0;
id=hash[a[j].name];
hash[a[j].name]=j;
if(id>0){
while(id>0&&ok[id]){
a[id].secondoption();
ok[id]=0;
tid=id;
id=hash[a[id].name];
hash[a[tid].name]=tid;
}
if(id>0&&!ok[id])return 0;
}
}
}
i=ed;
}
return 1;
}
void work(){
if(!check())puts("NO");
else{
puts("YES");
for(int i=1;i<=N;i++)
tmp[a[i].id]=i;
for(int i=1;i<=N;i++)
cout<<a[tmp[i]].name<<endl;
}
}
}sol;
int main(){
sol.init();
sol.work();
return 0;
}

E-Underground Lab

思路题

看别人代码才懂的,我太鶸了。。

//Author: Lixiang
#include<stdio.h>
#include<vector>
#include<queue>
using namespace std;
const int maxn=200001;
struct Underground_Lab{
vector <int> ADJL[maxn];
queue <int> Q;
bool vis[maxn];
int N,M,K;
void init(){
scanf("%d%d%d",&N,&M,&K);
for(int i=1,s,t;i<=M;i++){
scanf("%d%d",&s,&t);
ADJL[s].push_back(t);
ADJL[t].push_back(s);
}
}
void dfs(int id){
Q.push(id);
vis[id]=1;
vector <int>::iterator it;
for(it=ADJL[id].begin();it!=ADJL[id].end();it++)
if(!vis[*it]){
dfs(*it);
Q.push(id);
}
}
void work(){
dfs(1);
int step=Q.size()/K,rest=Q.size()%K;
for(int i=0,t;i<K;i++){
t=step+(i<rest);
printf("%d",t);
for(int i=1;i<=t;i++){
printf(" %d",Q.front());
Q.pop();
}
puts("");
}
}
}sol;
int main(){
sol.init();
sol.work();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  codeforces
相关文章推荐