首页 > 其他分享 >ACM ICPC 2017 Warmup Contest 1 (NCPC 2016)

ACM ICPC 2017 Warmup Contest 1 (NCPC 2016)

时间:2022-10-24 18:04:29浏览次数:62  
标签:ch return Warmup NCPC Contest int ll se define


A Artwork

倒跑并查集

#include<bits/stdc++.h> 
using namespace std;
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
For(j,m-1) cout<<a[i][j]<<' ';\
cout<<a[i][m]<<endl; \
}
#pragma
#define
#define
#define
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
inline int read()
{
int x=0,f=1; char ch=getchar();
while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
#define
#define
int n,m,q;
class bingchaji
{
public:
int father[MAXN*MAXN],n,cnt;
void mem(int _n)
{
n=cnt=_n;
For(i,n) father[i]=i;
}
int getfather(int x)
{
if (father[x]==x) return x;

return father[x]=getfather(father[x]);
}
void unite(int x,int y)
{
x=getfather(x);
y=getfather(y);
if (x^y) {
--cnt;
father[x]=y;
}
}
bool same(int x,int y)
{
return getfather(x)==getfather(y);
}
}S;
pair< pi ,pi > q1[MAXQ];
int a[MAXN][MAXN];
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
bool inside(int x,int y){return 1<=x&&x<=n&&1<=y&&y<=m;}
int an[MAXQ];
int id(int x,int y) {return (x-1)*m+y;}
int las[MAXN][MAXN];
int main()
{
// freopen("A.in","r",stdin);
// freopen(".out","w",stdout);

cin>>n>>m>>q;
S.mem(n*m);
MEM(las)
MEM(a)
For(i,q) {
q1[i].fi.fi=read();
q1[i].fi.se=read();
q1[i].se.fi=read();
q1[i].se.se=read();
Fork(x,q1[i].fi.fi,q1[i].se.fi) Fork(y,q1[i].fi.se,q1[i].se.se) {
a[x][y]=1;
if (!las[x][y])las[x][y]=i;
}
}
For(x,n) For(y,m) {
if (inside(x+1,y)&&!a[x][y]&&!a[x+1][y]) S.unite(id(x+1,y),id(x,y));
if (inside(x,y+1)&&!a[x][y]&&!a[x][y+1]) S.unite(id(x,y+1),id(x,y));
}
int ans=0;
For(x,n) For(y,m) if (S.getfather(id(x,y))==id(x,y)&&!a[x][y] ) {
++ans;
}
ForD(i,q) {
an[i]=ans;
Fork(x,q1[i].fi.fi,q1[i].se.fi) Fork(y,q1[i].fi.se,q1[i].se.se) if (a[x][y]&&las[x][y]==i){
a[x][y]=0;
ans++;
Rep(di,4) {
int x2=x+dx[di],y2=y+dy[di];
if (inside(x2,y2)&&!a[x][y]&&!a[x2][y2]) {
if (!S.same(id(x,y),id(x2,y2))) {
S.unite(id(x2,y2),id(x,y));
ans--;
}
}
}
// cout<<ans<<' ';
}
// cout<<endl;
}

For(i,q) printf("%d\n",an[i]);
return 0;
}

Bless You Autocorrect!

建trie树,二分hashLcp

Card Hand Sorting

暴力

#include<bits/stdc++.h> 
using namespace std;
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
For(j,m-1) cout<<a[i][j]<<' ';\
cout<<a[i][m]<<endl; \
}
#pragma
#define
#define
#define
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
inline int read()
{
int x=0,f=1; char ch=getchar();
while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
char l[100]="23456789TJQKA";
char s[100]="shdc";
int h1[10000],h2[10000];
namespace LIS{
#define
int n,a[MAXN],d[MAXN],f[MAXN],len=0;
void LIS()
{
memset(d,128,sizeof(d));
d[len=0]=INF;
ForD(i,n)
{
int l=0,r=len,ans=0;
while (l<=r)
{
int m=l+r>>1;
if (a[i]<d[m]) ans=m,l=m+1;
else r=m-1;
}
f[i]=ans+1;len=max(len,f[i]);
d[f[i]]=max(d[f[i]],a[i]);
}
}
}
pi p[MAXN];
int pe[4]={0,1,2,3};
int main()
{
// freopen("C.in","r",stdin);
// freopen(".out","w",stdout);
int n=read();
Rep(i,13) h1[l[i]]=i;
Rep(i,4) h2[s[i]]=i;
For(i,n) {
char s[10];
scanf("%s",s);
p[i]=mp(s[0],s[1]);
}
int ans=0;
LIS::n=n;
do{
Rep(st,1<<4) {
For(i,n) {
int sp=h2[p[i].se];
if ((st>>sp)&1)
LIS::a[i] = h1[p[i].fi] + pe[h2[p[i].se]]*13;
else {
LIS::a[i] = 12 - h1[p[i].fi] + pe[h2[p[i].se]]*13;
}
}
LIS::LIS();
gmax(ans,LIS::len);
}
}while(next_permutation(pe,pe+4));
cout<<n-ans<<endl;


return 0;
}

Daydreaming Stockbroker

E Exponial

扩展欧拉公式
广义欧拉定理
于同余式a^b≡x(mod m),如何求出x?(1<=a,m<=1000000000,1<=b<=10^1000000)
注意到b很大,我们可以先采取一些方法降幂。
若gcd(a,m)=1,那么使用欧拉定理即可:a^b≡a^(b%φ(m))(mod m)
若gcd(a,m)>1,且b>φ(m),则有“求幂大法”——a^b≡a^(b%φ(m)+φ(m))(mod m)
(当b<=φ(m)时直接用快速幂即可)

Keeping the Dogs Apart

2只dog分别在2折线上以相同速度跑,直到其中1只跑到终点(包括端点)的这段时间里2只dog最远距离?

#include<bits/stdc++.h>
#include<quadmath.h>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=Pre[x];p;p=Next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=Next[p])
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int>
#define pi pair<int,int>
#define SI(a) ((a).size())
#define ALL(x) (x).begin(),(x).end()
typedef long long ll;
typedef __float128 ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return (a-b+llabs(a-b)/F*F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int read()
{
int x=0,f=1; char ch=getchar();
while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
ll sqr(ll a){return a*a;}
ld sqr(ld a){return a*a;}
const __float128 eps=1e-10;
#define fabsq(x) max(x,-x)
int dcmp(__float128 x) {
if (fabsq(x)<eps) return 0; else return x<0 ? -1 : 1;
}
ld PI = 3.141592653589793238462643383;
class P{
public:
__float128 x,y;
P(__float128 x=0,__float128 y=0):x(x),y(y){}
friend ld dis2(P A,P B){return sqr(A.x-B.x)+sqr(A.y-B.y); }
friend ld Dot(P A,P B) {return A.x*B.x+A.y*B.y; }
friend ld Length(P A) {return (__float128)sqrt((double)Dot(A,A)); }

friend P operator- (P A,P B) { return P(A.x-B.x,A.y-B.y); }
friend P operator+ (P A,P B) { return P(A.x+B.x,A.y+B.y); }
friend P operator* (P A,__float128 p) { return P(A.x*p,A.y*p); }
friend P operator/ (P A,__float128 p) { return P(A.x/p,A.y/p); }
friend bool operator< (const P& a,const P& b) {return dcmp(a.x-b.x)<0 ||(dcmp(a.x-b.x)==0&& dcmp(a.y-b.y)<0 );}

};
P read_point() {
double ax,ay;
scanf("%lf%lf",&ax,&ay);
P a=P(ax,ay);
return a;
}
bool operator==(const P& a,const P& b) {
return dcmp(a.x-b.x)==0 && dcmp(a.y-b.y) == 0;
}
typedef P V;

__float128 Cross(V A,V B) {return A.x*B.y - A.y*B.x;}
__float128 DistanceToSegment(P p,P A,P B) {
if (A==B) return Length(p-A);
V v1 = B-A, v2 = p-A, v3 = p - B;
if (dcmp(Dot(v1,v2))<0) return Length(v2);
else if (dcmp(Dot(v1,v3))>0 ) return Length(v3);
else return fabsq(Cross(v1,v2) ) / Length(v1);
}
int n,m;
#define MAXN (120000)
P a[MAXN],b[MAXN];
__float128 t1[MAXN]={},t2[MAXN]={};
int main()
{
// freopen("K.in","r",stdin);
// freopen(".out","w",stdout);
n=read();
For(i,n) {
a[i]=read_point();
}
Fork(i,2,n) {
t1[i]=Length(a[i]-a[i-1])+t1[i-1];
}
m=read();
For(i,m) {
b[i]=read_point();
}
Fork(i,2,m) {
t2[i]=Length(b[i]-b[i-1])+t2[i-1];
}
int l1=1,l2=1;
__float128 nowt=0;
P A(b[1]-a[1]);
__float128 ans=Length(A),t;
while(l1<n&&l2<m) {
if (t1[l1+1]<t2[l2+1]) {
t=t1[l1+1]-nowt;
P B=A+ (b[l2+1]-b[l2])*t/Length(b[l2+1]-b[l2])- (a[l1+1]-a[l1])*t/Length(a[l1+1]-a[l1]);
ans = min(ans ,DistanceToSegment(P(0,0), A,B ) );
nowt=t1[++l1];
A=B;
} else {
t=t2[l2+1]-nowt;
P B=A+ (b[l2+1]-b[l2])*t/Length(b[l2+1]-b[l2])- (a[l1+1]-a[l1])*t/Length(a[l1+1]-a[l1]);
ans = min(ans ,DistanceToSegment(P(0,0), A,B ) );
nowt=t2[++l2];
A=B;
}
}
printf("%.12lf\n",(double)ans);


return 0;
}


标签:ch,return,Warmup,NCPC,Contest,int,ll,se,define
From: https://blog.51cto.com/u_15724837/5790752

相关文章