Codeforces Global Round 2
A:答案一定包含首位或末位。
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
#define ll long long
#define N 300010
char getc(){char c=getchar();while ((c<'A'||c>'Z')&&(c<'a'||c>'z')&&(c<'0'||c>'9')) c=getchar();return c;}
int gcd(int n,int m){return m==0?n:gcd(m,n%m);}
int read()
{
int x=0,f=1;char c=getchar();
while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar();
return x*f;
}
int n,a[N];
signed main()
{
#ifndef ONLINE_JUDGE
freopen("a.in","r",stdin);
freopen("a.out","w",stdout);
#endif
n=read();
for (int i=1;i<=n;i++) a[i]=read();
if (a[1]!=a[n]) cout<<n-1;
else
{
int ans=0;
for (int i=n;i>=1;i--) if (a[i]!=a[1]) ans=max(ans,i-1);
for (int i=1;i<=n;i++) if (a[i]!=a[n]) ans=max(ans,n-i);
cout<<ans;
}
return 0;
//NOTICE LONG LONG!!!!!
}
B:二分答案,大的匹配大的即可。
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
#define ll long long
#define N 1010
char getc(){char c=getchar();while ((c<'A'||c>'Z')&&(c<'a'||c>'z')&&(c<'0'||c>'9')) c=getchar();return c;}
int gcd(int n,int m){return m==0?n:gcd(m,n%m);}
int read()
{
int x=0,f=1;char c=getchar();
while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar();
return x*f;
}
int n,h,a[N],b[N];
bool check(int k)
{
for (int i=1;i<=k;i++) b[i]=a[i];
sort(b+1,b+k+1);
ll s=0;
for (int i=k;i>0;i-=2) s+=b[i];
return s<=h;
}
signed main()
{
#ifndef ONLINE_JUDGE
freopen("a.in","r",stdin);
freopen("a.out","w",stdout);
#endif
n=read(),h=read();
for (int i=1;i<=n;i++) a[i]=read();
int l=1,r=n,ans=1;
while (l<=r)
{
int mid=l+r>>1;
if (check(mid)) ans=mid,l=mid+1;
else r=mid-1;
}
cout<<ans;
return 0;
//NOTICE LONG LONG!!!!!
}
C:显然将两矩阵xor一下后相当于将矩阵变为全0矩阵,大胆猜想要求每行每列都有偶数个1即可。
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
#define ll long long
#define N 510
char getc(){char c=getchar();while ((c<'A'||c>'Z')&&(c<'a'||c>'z')&&(c<'0'||c>'9')) c=getchar();return c;}
int gcd(int n,int m){return m==0?n:gcd(m,n%m);}
int read()
{
int x=0,f=1;char c=getchar();
while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar();
return x*f;
}
int n,m,a[N][N],b[N][N];
signed main()
{
#ifndef ONLINE_JUDGE
freopen("a.in","r",stdin);
freopen("a.out","w",stdout);
#endif
n=read(),m=read();
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++)
a[i][j]=read();
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++)
a[i][j]^=read();
for (int i=1;i<=n;i++)
{
int s=0;
for (int j=1;j<=m;j++)
s^=a[i][j];
if (s&1) {cout<<"NO";return 0;}
}
for (int i=1;i<=m;i++)
{
int s=0;
for (int j=1;j<=n;j++)
s^=a[j][i];
if (s&1) {cout<<"NO";return 0;}
}
cout<<"YES";
return 0;
//NOTICE LONG LONG!!!!!
}
D:显然询问的答案只与长度有关,离线按长度从小到大排序,相当于一个不断合并间隙的过程,将间隙长度排序记录已经有多少个被填满即可。
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
#define ll long long
#define N 100010
char getc(){char c=getchar();while ((c<'A'||c>'Z')&&(c<'a'||c>'z')&&(c<'0'||c>'9')) c=getchar();return c;}
int gcd(int n,int m){return m==0?n:gcd(m,n%m);}
int read()
{
int x=0,f=1;char c=getchar();
while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar();
return x*f;
}
int n,q;
ll a[N],ans[N],d[N];
struct data
{
ll k,i;
bool operator <(const data&a) const
{
return k<a.k;
}
}b[N];
signed main()
{
#ifndef ONLINE_JUDGE
freopen("a.in","r",stdin);
freopen("a.out","w",stdout);
#endif
n=read();
for (int i=1;i<=n;i++) scanf("%I64d",&a[i]);
sort(a+1,a+n+1);
for (int i=2;i<=n;i++) d[i]=a[i]-a[i-1];
sort(d+2,d+n+1);
q=read();
for (int i=1;i<=q;i++)
{
ll l,r;scanf("%I64d%I64d",&l,&r);
b[i].k=r-l+1;b[i].i=i;
}
sort(b+1,b+q+1);
int cnt=n,x=1;
for (int i=1;i<=q;i++)
{
ans[b[i].i]=ans[b[i-1].i];
while (x<n&&d[x+1]<=b[i].k) ans[b[i].i]-=b[i-1].k,x++,cnt--,ans[b[i].i]+=d[x];
ans[b[i].i]+=1ll*cnt*(b[i].k-b[i-1].k);
}
for (int i=1;i<=q;i++) printf("%I64d ",ans[i]);
return 0;
//NOTICE LONG LONG!!!!!
}
E:显然三角形一定等腰且腰为最长边,瞎贪心,从小到大考虑边长,让其先尽量与之前的短边组成三角形,若有剩余则内部消化,最后留下的边留给更长的边匹配。
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
#define ll long long
#define N 300010
char getc(){char c=getchar();while ((c<'A'||c>'Z')&&(c<'a'||c>'z')&&(c<'0'||c>'9')) c=getchar();return c;}
int gcd(int n,int m){return m==0?n:gcd(m,n%m);}
int read()
{
int x=0,f=1;char c=getchar();
while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar();
return x*f;
}
int n,a[N];
ll ans,x=0;
signed main()
{
#ifndef ONLINE_JUDGE
freopen("a.in","r",stdin);
freopen("a.out","w",stdout);
#endif
n=read();
for (int i=1;i<=n;i++) a[i]=read();
for (int i=1;i<=n;i++)
{
if (a[i]>=x*2)
{
ans+=x,a[i]-=x*2,x=0;
ans+=a[i]/3,x=a[i]%3;
}
else
{
ans+=a[i]/2,x-=a[i]/2,x+=a[i]%2;
}
}
cout<<ans;
return 0;
//NOTICE LONG LONG!!!!!
}
F:考虑对于单次询问怎么做,显然有dpf[i][0/1]表示在i连向父亲的这条边是否切的情况下,i子树内所有点都满足条件的最小代价,转移将儿子按f[][0]-f[][1]排下序即可。注意到度数>=x的点的个数是O(n/x)的,所以对于所有询问,需要考虑的点的总数只有O(nlogn)个。但同时这些点还会与度数<x的点相连,这些边也可以提供贡献,这一部分可以用平衡树维护,dp时在平衡树上二分决定删除哪些边即可。
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define N 250010
#define inf 10000000000000000ll
#define lson tree[k].ch[0]
#define rson tree[k].ch[1]
mt19937 rnd(20020509);
char getc(){char c=getchar();while ((c<'A'||c>'Z')&&(c<'a'||c>'z')&&(c<'0'||c>'9')) c=getchar();return c;}
int gcd(int n,int m){return m==0?n:gcd(m,n%m);}
int read()
{
int x=0,f=1;char c=getchar();
while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar();
return x*f;
}
int _,n,p[N],degree[N],root[N],id[N],t,cnt;
bool vis[N];
ll f[N][2],S[N],a[N];
struct data{int to,nxt,len;
}edge[N<<1];
struct data2{int ch[2],p,size,x;ll sum;
}tree[N<<1];
void addedge(int x,int y,int z){t++;edge[t].to=y,edge[t].nxt=p[x],edge[t].len=z,p[x]=t;}
bool cmp(const int&a,const int&b)
{
return degree[a]<degree[b];
}
ll query(int k,int x,int s)
{
if (x==0) return 0;
if (k==0) return x>s?inf:S[x];
if (tree[lson].size>=x) return query(lson,x,s);
if (x-tree[lson].size>s) return query(rson,x-tree[lson].size-1,s)+tree[lson].sum+tree[k].x;
ll t=tree[lson].sum+tree[k].x+S[x-tree[lson].size-1];
if (tree[k].x<=a[x-tree[lson].size]) return t=min(t,tree[lson].sum+tree[k].x+query(rson,x-tree[lson].size-1,s));
else return t=min(t,query(lson,x,s));
return t;
}
void up(int k)
{
tree[k].size=tree[lson].size+tree[rson].size+1;
tree[k].sum=tree[lson].sum+tree[rson].sum+tree[k].x;
}
void move(int &k,int p)
{
int t=tree[k].ch[p];
tree[k].ch[p]=tree[t].ch[!p],tree[t].ch[!p]=k,up(k),up(t),k=t;
}
void ins(int &k,int x)
{
if (k==0) {k=++cnt;tree[k].p=rnd(),tree[k].size=1,tree[k].sum=tree[k].x=x;return;}
if (tree[k].x<x) {ins(rson,x);if (tree[rson].p>tree[k].p) move(k,1);}
else {ins(lson,x);if (tree[lson].p>tree[k].p) move(k,0);}
up(k);
}
void dfs(int k,int from)
{
vis[k]=1;int x=0;bool isroot=1;f[k][0]=f[k][1]=0;
int last=0;
for (int i=p[k];i;i=edge[i].nxt)
if (degree[edge[i].to]>_)
{
if (!vis[edge[i].to]) dfs(edge[i].to,k),f[k][0]+=f[edge[i].to][1],f[k][1]+=f[edge[i].to][1];
else f[k][0]+=edge[i].len,isroot=0;
last=i;
}
else if (last) edge[last].nxt=edge[i].nxt;else p[k]=edge[i].nxt;
for (int i=p[k];i;i=edge[i].nxt)
if (degree[edge[i].to]>_&&edge[i].to!=from) a[++x]=f[edge[i].to][0]-f[edge[i].to][1];
sort(a+1,a+x+1);
for (int i=1;i<=x;i++) S[i]=S[i-1]+a[i];
for (int i=x;i>=0;i--)
if (a[i]<=0)
{
if (degree[k]-i<=_) f[k][1]+=S[i];
else f[k][1]+=query(root[k],degree[k]-_,x);
break;
}
if (!isroot)
{
for (int i=x;i>=0;i--)
if (a[i]<=0)
{
if (degree[k]-1-i<=_) f[k][0]+=S[i];
else f[k][0]+=query(root[k],degree[k]-1-_,x);
break;
}
}
}
signed main()
{
#ifndef ONLINE_JUDGE
freopen("a.in","r",stdin);
freopen("a.out","w",stdout);
#endif
n=read();
for (int i=1;i<n;i++)
{
int x=read(),y=read(),z=read();
addedge(x,y,z),addedge(y,x,z);
degree[x]++,degree[y]++;
}
for (int i=1;i<=n;i++) id[i]=i;
sort(id+1,id+n+1,cmp);
int x=1;
for (_=0;_<n;_++)
{
while (x<=n&°ree[id[x]]<=_)
{
for (int j=p[id[x]];j;j=edge[j].nxt)
ins(root[edge[j].to],edge[j].len);
x++;
}
for (int j=x;j<=n;j++) vis[id[j]]=0;
ll ans=0;
for (int j=x;j<=n;j++)
if (!vis[id[j]])
{
dfs(id[j],id[j]);
ans+=f[id[j]][1];
}
//for (int i=1;i<=n;i++) cout<<f[i][0]<<' '<<f[i][1]<<endl;
printf("%I64d ",ans);
}
return 0;
//NOTICE LONG LONG!!!!!
}
G、H:咕咕咕
小号打的。这种场真是不管打成什么样都能涨分。result:rank 310 rating +7
Codeforces Global Round 2的更多相关文章
- CodeForces Global Round 1
CodeForces Global Round 1 CF新的比赛呢(虽然没啥区别)!这种报名的人多的比赛涨分是真的快.... 所以就写下题解吧. A. Parity 太简单了,随便模拟一下就完了. B ...
- Codeforces Global Round 1 - D. Jongmah(动态规划)
Problem Codeforces Global Round 1 - D. Jongmah Time Limit: 3000 mSec Problem Description Input Out ...
- Codeforces Global Round 2 题解
Codeforces Global Round 2 题目链接:https://codeforces.com/contest/1119 A. Ilya and a Colorful Walk 题意: 给 ...
- Codeforces Global Round 1 (A-E题解)
Codeforces Global Round 1 题目链接:https://codeforces.com/contest/1110 A. Parity 题意: 给出{ak},b,k,判断a1*b^( ...
- Codeforces Global Round 3
Codeforces Global Round 3 A. Another One Bites The Dust 有若干个a,有若干个b,有若干个ab.你现在要把这些串拼成一个串,使得任意两个相邻的位置 ...
- Codeforces Global Round 1 (CF1110) (未完结,只有 A-F)
Codeforces Global Round 1 (CF1110) 继续补题.因为看见同学打了这场,而且涨分还不错,所以觉得这套题目可能会比较有意思. 因为下午要开学了,所以恐怕暂时不能把这套题目补 ...
- 【手抖康复训练1 】Codeforces Global Round 6
[手抖康复训练1 ]Codeforces Global Round 6 总结:不想复习随意打的一场,比赛开始就是熟悉的N分钟进不去时间,2333,太久没写题的后果就是:A 题手抖过不了样例 B题秒出思 ...
- Codeforces Global Round 11 个人题解(B题)
Codeforces Global Round 11 1427A. Avoiding Zero 题目链接:click here 待补 1427B. Chess Cheater 题目链接:click h ...
- 【Codeforces Round 1110】Codeforces Global Round 1
Codeforces Round 1110 这场比赛只做了\(A\).\(B\).\(C\),排名\(905\),不好. 主要的问题在\(D\)题上,有\(505\)人做出,但我没做出来. 考虑的时候 ...
- 树形DP ---- Codeforces Global Round 2 F. Niyaz and Small Degrees引发的一场血案
Aspirations:没有结果,没有成绩,acm是否有意义?它最大的意义就是让我培养快速理解和应用一个个未知知识点的能力. ————————————————————————————————————— ...
随机推荐
- Timeline Style
from: https://freefrontend.com/css-timelines/ https://bootstrapthemes.co/items/resources/timeline/ h ...
- jquery获取内容和属性的方法
通过jquery如何捕获文本内容和属性? text(),html(),val()及attr(). attr()更具有普遍性,元素text属性和表单value属性,可以通过attr()操作. <! ...
- OPC协议解析-关于OPC协议的几个问题
1 什么是OPC协议? 为了便于自动化行业不同厂家的设备和应用程序能相互交换数据,定义了一个统一的接口函数,就是OPC协议规范.有了OPC就可以使用统一的方式去访问不同设备厂商的产品数据. OP ...
- Monkey测试记录
配置环境变量,不然用不了adb命令 path这里也一样配置一下 命令的各种意思百度一下看看也就知道了 看到一篇博客推荐的一种测试命令,我也直接拿来用了 adb shell monkey -p 你的包名 ...
- 18-10-31 Scrum Meeting 3
1.会议照片 2.每人的工作 昨天完成的工作 1 制定配置 修改配置 查询配置这三个接口 2 3 获取单词对应的中文释义 4 完成测验的部分接口 5 后端对接计划的接口 6 剩余的 ...
- MySQL 基础知识梳理学习(一)----系统数据库
information_schema 此数据库是MySQL数据库自带的,主要存储数据库的元数据,保存了关于MySQL服务器维护的所有其他数据库的信息,如数据库名.数据库表.表列的数据类型及访问权限等. ...
- Linux中Apache服务器的简单配置
配置Apache服务器: 1.安装Apache,可以参考Linux 中yum的配置来安装: yum install http* -y 2.修改SELinux: setenforce 0 查看: 3.防 ...
- 【Teradata】变更viewpoint web登录地址
1.使用root用户登录原viewpoint地址 ssh root@192.168.253.133 2.查看使用网卡(示例中为eth0) route Kernel IP routing table D ...
- Python开发【第四篇】函数
函数的作用 函数可以让编程逻辑结构化以及模块化 无论是C.C++,Java还是Python,函数是必不可少的知识点,也是很重要的知识点,函数是完成一个功能的代码块,使用函数可以使逻辑结构变得更加清晰以 ...
- Spark-RDD之Partition源码分析
概要 Spark RDD主要由Dependency.Partition.Partitioner组成,Partition是其中之一.一份待处理的原始数据会被按照相应的逻辑(例如jdbc和hdfs的spl ...