XVIII Open Cup named after E.V. Pankratiev. Grand Prix of SPb
A. Base $i - 1$ Notation
两个性质:
- $2=1100$
- $122=0$
利用这两条性质实现高精度加法即可。
时间复杂度$O(n)$。
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
const int N = 6e6 + 10;
const int inf = 1e9;
int casenum, casei;
char a[N], b[N];
int c[N];
int main()
{
freopen("base-i-1.in","r",stdin); freopen("base-i-1.out","w",stdout);
while(~scanf("%s%s", a, b))
{
int n = strlen(a); reverse(a, a + n);
int m = strlen(b); reverse(b, b + m);
//int n = 5e5; for(int i = 0; i < n; ++i)a[i] = '1'; a[n] = 0;
//int m = 5e5; for(int i = 0; i < m; ++i)b[i] = '1'; b[m] = 0;
int g = max(n, m);
for(int i = 0; i < g; ++i)
{
int p = i / 2; c[i] = 0;
if(i < n)c[i] += a[i] - 48;
if(i < m)c[i] += b[i] - 48;
}
for(int i = 0; i < g; ++i)
{
int t=min(c[i]/2,min(c[i+1]/2,c[i+2]));
c[i]-=t*2;
c[i+1]-=t*2;
c[i+2]-=t;
int w = c[i] / 2;
if(w)
{
c[i + 2] += w;
c[i + 3] += w;
g = max(g, i + 4);
}
c[i] %= 2;
}
while(g>1&&!c[g-1])g--;
for(int i = g - 1; i >= 0; --i)printf("%d", c[i]);
puts("");
}
return 0;
}
/* */
B. Squaring a Bit
直接暴力枚举平方数即可通过,需要手写popcount。
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
const int N = 3e5 + 10;
typedef long long LL;
LL n;
int v[65555];
int popcount(LL x){
return v[x&65535]+v[x>>16&65535]+v[x>>32&65535]+v[x>>48];
}
int main()
{
for(int i=1;i<65536;i++)v[i]=v[i>>1]+(i&1);
freopen("bit-squares.in","r",stdin); freopen("bit-squares.out","w",stdout);
while(~scanf("%lld", &n))
{
LL len = 0;
int one = 0;
while(n)
{
++len;
one += n & 1;
n /= 2;
} LL bott = 1ll << (len - 1);
int bot = sqrt(bott) + 0.99999999;
LL topp = (1ll << len) - 1;
int top = sqrt(min(topp, (LL)1e18));
//printf("%lld %lld\n", bott, topp);
//printf("%d %d\n", bot, top);
int ans = 0;
LL val = (LL)bot * bot, add = bot * 2 + 1;
int i=bot;
for(;i+4<=top;i+=4){
if(popcount(val) == one)++ans;
val += add, add += 2;
if(popcount(val) == one)++ans;
val += add, add += 2;
if(popcount(val) == one)++ans;
val += add, add += 2;
if(popcount(val) == one)++ans;
val += add, add += 2;
}
for(; i <= top; ++i, val += add, add += 2)
{
//printf("%d %lld\n", i, val);
//val = (LL)i * i;
if(popcount(val) == one)++ans;
}
printf("%d\n", ans);
}
return 0;
}
/* */
C. Chickens
状压DP求方案数。
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
const int N = 3e5 + 10;
const int inf = 1e9;
int casenum, casei;
int c[15], e[15];
int f[15][1 << 13];
int n;
int main()
{
freopen("chickens.in","r",stdin);
freopen("chickens.out","w",stdout);
while(~scanf("%d", &n))
{
for(int i = 0; i < n; ++i)scanf("%d", &c[i]);
for(int i = 0; i < n; ++i)scanf("%d", &e[i]);
memset(f, 0, sizeof(f)); f[0][0] = 1;
int top = (1 << n) - 1;
for(int i = 0; i < n; ++i)
{
for(int j = 0; j <= top; ++j)if(f[i][j])
{
for(int k = 0; k < n; ++k)if((~j >> k & 1) && c[i] <= e[k])
{
f[i + 1][j | 1 << k] += f[i][j];
}
}
}
printf("%d\n", f[n][top]);
}
return 0;
}
/* */
D. Lights at a Crossing
对于每个观察,枚举两盏红灯,由剩余时间大的向小的连权值为差值的有向边,则最小周期为这个有向图的最小环。
时间复杂度$O(n^3+mn^2)$。
#include<cstdio>
const int N=30,inf=10000000;
int n,m,i,j,k,f[N][N],a[N],ans=inf;char s[99];
inline void up(int&x,int y){x>y?(x=y):0;}
int main(){
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++)for(j=1;j<=n;j++)f[i][j]=inf;
while(m--){
for(i=1;i<=n;i++){
scanf("%s",s);
if(s[0]=='X')a[i]=-1;else sscanf(s,"%d",&a[i]);
}
for(i=1;i<=n;i++)if(~a[i])for(j=i+1;j<=n;j++)if(~a[j]){
if(a[i]<a[j])up(f[i][j],a[j]-a[i]);
if(a[i]>a[j])up(f[j][i],a[i]-a[j]);
}
}
for(k=1;k<=n;k++)for(i=1;i<=n;i++)for(j=1;j<=n;j++)up(f[i][j],f[i][k]+f[k][j]);
for(i=1;i<=n;i++)up(ans,f[i][i]);
if(ans==inf)ans=-1;
printf("%d",ans);
}
E. Decimal Form
法雷序列求两分数之间分母最小的分数。
时间复杂度$O(T\log w)$。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef __int128 lll;
typedef pair<lll,lll>P;
lll n,r,a,b,c,d,t;
int Case;
ld goal;
const int lim=1000000000;
const ld eps=1e-15;
lll gcd(lll a,lll b){return b?gcd(b,a%b):a;}
P cal(lll a,lll b,lll c,lll d){
lll x=a/b+1;
if(x*d<c)return P(x,1);
if(!a)return P(1,d/c+1);
if(a<=b&&c<=d){
P t=cal(d,c,b,a);
swap(t.first,t.second);
return t;
}
x=a/b;
P t=cal(a-b*x,b,c-d*x,d);
t.first+=t.second*x;
return t;
}
void write(lll x){
if(x>=10)write(x/10);
x%=10;
printf("%d",(int)x);
}
//0.666666666666666667
int main(){
freopen("decimal-form.in","r",stdin);
freopen("decimal-form.out","w",stdout);
scanf("%d",&Case);
while(Case--){
n=18;
static char s[100];
scanf("%s",s);
int len=strlen(s);
r=0;
for(int i=2;i<len;i++)r=r*10+s[i]-'0';
if(!r){
puts("0 1");
continue;
}
for(t=10;n--;t*=10);
a=r*10-5,b=t;
c=r*10+5,d=t;
lll o=gcd(a,b);
a/=o,b/=o;
o=gcd(c,d);
c/=o,d/=o;
P p=cal(a,b,c,d);
if(b<p.second)p=P(a,b);
write(p.first);
putchar(' ');
write(p.second);
puts("");
}
}
F. Martian Maze
留坑。
G. Wet Mole
Floodfill求出所有水能灌到的点即可。
时间复杂度$O(nm)$。
#include<cstdio>
const int N=1010;
int n,m,i,j,flag;char a[N][N];bool v[N][N];
inline bool check(int x,int y){
if(x<1||x>n||y<1||y>m)return 0;
if(a[x][y]=='#')return 0;
return 1;
}
void dfs(int x,int y){
if(v[x][y])return;
v[x][y]=1;
if(check(x+1,y))dfs(x+1,y);
else{
if(check(x,y-1))dfs(x,y-1);
if(check(x,y+1))dfs(x,y+1);
}
}
int main(){
freopen("mole.in","r",stdin);
freopen("mole.out","w",stdout);
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++)scanf("%s",a[i]+1);
for(i=1;i<=m;i++)if(a[1][i]=='.')dfs(1,i);
for(i=1;i<=n;i++)for(j=1;j<=m;j++)if(a[i][j]=='.'&&!v[i][j]){
if(!flag){
flag=1;
a[i][j]='X';
}
}
if(flag){
puts("Yes");
for(i=1;i<=n;i++)puts(a[i]+1);
}else puts("No");
}
H. Oddities
留坑。
I. Sorting on the Plane
将所有向量分成两类:在$1$号向量左侧和右侧的。然后分别排序即可。
时间复杂度$O(n\log n)$。
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
const int N=100010;
int n,i,f[N],m,t;
vector<int>q[2];
int ask(int x,int y){
printf("? %d %d\n",x,y);
fflush(stdout);
int t;
scanf("%d",&t);
return t;
}
bool cmp(int x,int y){return ask(x,y);}
int main(){
scanf("%d",&n);
if(n==1){
puts("! YES");
puts("1");
fflush(stdout);
return 0;
}
for(i=2;i<=n;i++){
q[ask(1,i)].push_back(i);
}
for(i=0;i<2;i++)sort(q[i].begin(),q[i].end(),cmp);
for(i=0;i<q[0].size();i++)f[++m]=q[0][i];
f[++m]=1;
for(i=0;i<q[1].size();i++)f[++m]=q[1][i];
if(t=ask(f[1],f[m]))puts("! YES");else puts("! NO");
if(!t)printf("%d ",m);
for(i=1;i<=m;i++)printf("%d ",f[i]);
fflush(stdout);
return 0;
}
J. Center of List of Sums
二分求出上下界以及上下界的排名,然后将中间的数暴力取出即可。
时间复杂度$O(n\log n)$。
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=2000010;
int n,m,i;ll a[N],b[N],q[N],L,R,down,up,cntdown,cntup;
inline ll cal(ll lim){//<=lim
int i=1,j=n;
ll t=0;
for(i=1;i<=n;i++){
while(j&&a[i]+b[j]>lim)j--;
t+=j;
}
return t;
}
inline ll getkth(ll k){
ll l=0,r=2100000000,mid,t;
while(l<=r){
mid=(l+r)>>1;
if(cal(mid)>=k)r=(t=mid)-1;else l=mid+1;
}
return t;
}
void push(ll A,ll B){
int i,j=n,k=n;
for(i=1;i<=n;i++){
while(j&&a[i]+b[j]>=A)j--;
while(k&&a[i]+b[k]>B)k--;
for(int o=j+1;o<=k;o++)q[++m]=a[i]+b[o];
}
}
int main(){
freopen("sums-center.in","r",stdin);
freopen("sums-center.out","w",stdout);
scanf("%d",&n);
for(i=1;i<=n;i++)scanf("%lld",&a[i]);
for(i=1;i<=n;i++)scanf("%lld",&b[i]);
sort(a+1,a+n+1);
sort(b+1,b+n+1);
L=1LL*n*(n-1)/2+1;
R=1LL*n*(n+1)/2;
down=getkth(L);
up=getkth(R);
//printf("L=%lld R=%lld down=%lld up=%lld\n",L,R,down,up);
if(down==up){
for(ll o=L;o<=R;o++)printf("%lld ",down);
return 0;
}
cntdown=cal(down);//<=down
cntup=cal(up-1);//<up
//printf("cnt=%lld %lld\n",cntdown,cntup);
push(down+1,up-1);
for(ll o=L;o<=cntdown&&o<=R;o++)q[++m]=down;
for(ll o=R;o>cntup&&o>=L;o--)q[++m]=up;
sort(q+1,q+m+1);
for(i=1;i<=m;i++)printf("%lld ",q[i]);
}
K. Cookies
将每个串排序,然后直接DP求出最长链即可。
#include<cstdio>
#include<string>
#include<algorithm>
#include<map>
#include<iostream>
using namespace std;
const int N=180000;
int n,m,i;
int len[N];
int f[N],g[N];
string name[N],show[N],need[N];
map<string,int>pos;
string q[N];
int dp(int x){
if(!x)return 0;
if(f[x])return f[x];
int A=f[x],B=0;
for(int i=0;i<name[x].size();i++){
string now=name[x];
now.erase(i,1);
int o=pos[now];
if(!o)continue;
int t=dp(o);
if(t>A)A=t,B=o;
}
f[x]=A+1,g[x]=B;
return f[x];
}
int main(){
freopen("word-chains.in","r",stdin); freopen("word-chains.out","w",stdout);
cin>>n;//n=10000
for(i=0;i<n;i++)cin>>need[i];
cin>>m;//173554
m++;
name[1]="";
show[1]=".";
for(i=2;i<=m;i++){
cin>>name[i];
show[i]=name[i];
}
for(i=1;i<=m;i++){
sort(name[i].begin(),name[i].end());
pos[name[i]]=i;//sorted
len[i]=name[i].size();
}
for(i=0;i<n;i++){
string now=need[i];
sort(now.begin(),now.end());
int x=pos[now];
dp(x);
int cnt=0;
while(x){
q[++cnt]=show[x];
x=g[x];
}
q[1]=need[i];
cout<<cnt<<endl;
for(int j=1;j<=cnt;j++){
cout<<q[j];
if(j<cnt)cout<<" -> ";else cout<<endl;
}
}
}
/*
4
university
open
cup
cookie
17
university
intrusive
neuritis
unities
seniti
nisei
sine
sei
e
es
one
ne
open
cup
up
p
cookie
*/
L. Xor-fair Division
若所有数异或和非$0$,则答案为$0$,否则答案为$2^n-2$。
#include<cstdio>
typedef long long ll;
int n;long long ans,sum,x;
int main(){
freopen("xorseq.in","r",stdin);
freopen("xorseq.out","w",stdout);
scanf("%d",&n);
ans=1LL<<n;
while(n--){
scanf("%lld",&x);
sum^=x;
}
if(sum)ans=0;
else if(n==1)ans=0;
else ans-=2;
printf("%lld",ans);
}
XVIII Open Cup named after E.V. Pankratiev. Grand Prix of SPb的更多相关文章
- XVIII Open Cup named after E.V. Pankratiev. Grand Prix of Siberia
1. GUI 按题意判断即可. #include<stdio.h> #include<iostream> #include<string.h> #include&l ...
- XVIII Open Cup named after E.V. Pankratiev. Grand Prix of Peterhof
A. City Wall 找规律. #include<stdio.h> #include<iostream> #include<string.h> #include ...
- XVIII Open Cup named after E.V. Pankratiev. Grand Prix of Khamovniki
A. Ability Draft 记忆化搜索. #include<stdio.h> #include<iostream> #include<string.h> #i ...
- XVIII Open Cup named after E.V. Pankratiev. Grand Prix of Korea
A. Donut 扫描线+线段树. #include<cstdio> #include<algorithm> using namespace std; typedef long ...
- XVIII Open Cup named after E.V. Pankratiev. Grand Prix of Saratov
A. Three Arrays 枚举每个$a_i$,双指针出$b$和$c$的范围,对于$b$中每个预先双指针出$c$的范围,那么对于每个$b$,在对应$c$的区间加$1$,在$a$处区间求和即可. 树 ...
- XVIII Open Cup named after E.V. Pankratiev. Grand Prix of Khamovniki Problem J Stairways解题报告(分块+维护凸壳)
首先ORZ一发Claris聚聚的题解:http://www.cnblogs.com/clrs97/p/8689215.html,不然我可能没机会补过这道神题了. 这里写一个更详细的题解吧(我还是太菜了 ...
- XVII Open Cup named after E.V. Pankratiev. Grand Prix of America (NAIPC-2017)
A. Pieces of Parentheses 将括号串排序,先处理会使左括号数增加的串,这里面先处理减少的值少的串:再处理会使左括号数减少的串,这里面先处理差值较大的串.确定顺序之后就可以DP了. ...
- XVII Open Cup named after E.V. Pankratiev Grand Prix of Moscow Workshops, Sunday, April 23, 2017 Problem D. Great Again
题目: Problem D. Great AgainInput file: standard inputOutput file: standard outputTime limit: 2 second ...
- XVII Open Cup named after E.V. Pankratiev Grand Prix of Moscow Workshops, Sunday, April 23, 2017 Problem K. Piecemaking
题目:Problem K. PiecemakingInput file: standard inputOutput file: standard outputTime limit: 1 secondM ...
随机推荐
- Ubuntu16.04 安装g++6
https://blog.csdn.net/qq_34877350/article/details/81182022 1.安装gcc-6: sudo apt-get update && ...
- Stanford Local 2016 E "Election of Evil"(搜索(正解)或并查集(划掉))
传送门 题意: 给出集合U,V,集合U有n个元素,集合V有m个元素: 有 m 个操作,mi : s1 s2 有一条s1指向s2的边(s1,s2可能属于第三个集合,暂且称之为K集合): 指向边具有传递性 ...
- CentOS配置防火墙操作实例
CentOS 配置防火墙操作实例(启.停.开.闭端口): 注:防火墙的基本操作命令: 查询防火墙状态: [root@localhost ~]# service iptables status<回 ...
- 1.1浅谈Spring(一个叫春的框架)
如今各种Spring框架甚嚣尘上,但是终归还是属于spring的东西.所以在这里,个人谈一谈对spring的认识,笔者觉得掌握spring原理以及spring所涉及到的设计模式对我们具有极大的帮助.我 ...
- sql 发送邮件
一.启用Database Mail XPs功能. 查看Database Mail XPs功能是否打开,从返回结果来看,value为0说明没有打开,注意SQL Mail XPs是SQL Server早期 ...
- java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or.....
1,<activity android:name=".DialogActivity" android:theme="@android:style/Theme.Dia ...
- ubuntu 16.04 安装 Mask_RCNN 遇到的问题集锦
源码网页(Mask R-CNN for object detection and instance segmentation on Keras and TensorFlow): https://git ...
- pythonのsimple_tag
当我们需要在页面种直接调用py文件中的某些方法时,我们就要用到simple_tag.具体步骤如下: 1.在某个app下创建templatetags文件夹,切记该名称是不可以改变的. 2.在该文件夹下创 ...
- Git入门——本地版本库操作
作为一个一直用SVN的家伙,深深地感到了自己在版本控制工具上的落伍.... 首先必须强调的是: Git与Github不是一回事. Git是目前世界上最先进的分布式版本控制系统,于2005年被linux ...
- C++设计模式——外观模式
前言 在实际开发时,面对一个大的系统,总是会将一个大的系统分成若干个子系统,等子系统完成之后,再分别调用对应的子系统来完成对应的整体功能,这样有利于降低系统的复杂性:最终进行实现某个具体的功能时,我们 ...