1. 程式人生 > >2017 ACM/ICPC Asia Regional Beijing Online

2017 ACM/ICPC Asia Regional Beijing Online

nic matrix div poll col gcd code 預處理 spa

A Visiting Peking University

直接模擬

B Reverse Suffix Array

unsolved

C Matrix

預處理每列前綴和以及每個每個行區間的每列的最小值,枚舉行的所有區間,求最長連續字段和。

D Agent Communication

unsolved

E Territorial Dispute

n=1,2時不行

n=3時必須三點共線

n>3一定可以,任意找四個點判斷即可。

好像也可以跑凸包,不在凸包上的點單獨分開,如果全部在凸包上,找兩個不相鄰的點即可。

技術分享
  1 #include <bits/stdc++.h>
  2 #define
lson (id*2) 3 #define rson (id*2+1) 4 #define mid ((l+r)/2) 5 using namespace std; 6 7 typedef long long LL; 8 const LL MAXN=1e3+5; 9 10 LL n; 11 struct PoLL{ 12 LL x,y,id; 13 } p[MAXN]; 14 bool cmp(PoLL a,PoLL b){ 15 if(a.x==b.x) return a.y<b.y; 16 return a.x<b.x;
17 } 18 LL vis[MAXN]; 19 20 LL cross(PoLL a,PoLL b){ 21 return a.x*b.y-a.y*b.x; 22 } 23 24 int main(){ 25 LL t; 26 scanf("%lld",&t); 27 while(t--){ 28 memset(vis,0,sizeof(vis)); 29 scanf("%lld",&n); 30 for(LL i=1;i<=n;i++){ 31 scanf("
%lld%lld",&p[i].x,&p[i].y); 32 p[i].id=i; 33 } 34 sort(p+1,p+1+n,cmp); 35 if(n<=2){ 36 printf("NO\n"); 37 continue; 38 } 39 else if(n==3){ 40 PoLL a=(PoLL){p[2].x-p[1].x,p[2].y-p[1].y}; 41 PoLL b=(PoLL){p[3].x-p[2].x,p[3].y-p[2].y}; 42 if(cross(a,b)==0) vis[p[2].id]=1; 43 else{ 44 printf("NO\n"); 45 continue; 46 } 47 } 48 else{ 49 LL flag=0; 50 for(int tt=1;tt<=4;tt++){ 51 int i,j,k; 52 if(tt==1) j=1,i=2,k=3; 53 if(tt==2) j=1,i=2,k=4; 54 if(tt==3) j=1,i=3,k=4; 55 if(tt==4) j=2,i=3,k=4; 56 PoLL ak=(PoLL){p[i].x-p[j].x,p[i].y-p[j].y}; 57 PoLL bk=(PoLL){p[k].x-p[i].x,p[k].y-p[i].y}; 58 if(cross(ak,bk)==0){ 59 vis[p[i].id]=1; 60 flag=1; 61 break; 62 } 63 } 64 65 if(flag==0) 66 for(LL i=1;i<=4;i++) 67 for(LL j=1;j<=4;j++) 68 for(LL k=1;k<=4;k++) 69 for(LL q=1;q<=4;q++){ 70 if(flag) break; 71 if(i==j||i==k||i==q) continue; 72 if(j==k||j==q||k==q) continue; 73 PoLL aa=(PoLL){p[i].x-p[j].x,p[i].y-p[j].y}; 74 PoLL bb=(PoLL){p[i].x-p[k].x,p[i].y-p[k].y}; 75 PoLL cc=(PoLL){p[i].x-p[q].x,p[i].y-p[q].y}; 76 LL s1=abs(cross(aa,bb))+abs(cross(aa,cc))+abs(cross(bb,cc)); 77 78 PoLL dd=(PoLL){p[j].x-p[k].x,p[j].y-p[k].y}; 79 PoLL ee=(PoLL){p[q].x-p[k].x,p[q].y-p[k].y}; 80 LL s2=abs(cross(dd,ee)); 81 if(s1==s2){ 82 flag=1; 83 vis[p[i].id]=1; 84 break; 85 } 86 } 87 88 if(flag==0) 89 for(LL i=1;i<=4;i++) 90 for(LL j=1;j<=4;j++) 91 for(LL k=1;k<=4;k++) 92 for(LL q=1;q<=4;q++){ 93 if(flag) break; 94 if(i==j||i==k||i==q) continue; 95 if(j==k||j==q||k==q) continue; 96 PoLL aa=(PoLL){p[i].x-p[j].x,p[i].y-p[j].y}; 97 PoLL bb=(PoLL){p[i].x-p[k].x,p[i].y-p[k].y}; 98 PoLL cc=(PoLL){p[i].x-p[q].x,p[i].y-p[q].y}; 99 if(cross(aa,bb)*cross(aa,cc)<0){ 100 vis[p[i].id]=vis[p[j].id]=1; 101 flag=1; 102 break; 103 } 104 } 105 if(!flag){ 106 printf("NO\n"); 107 continue; 108 } 109 } 110 printf("YES\n"); 111 for(LL i=1;i<=n;i++){ 112 if(vis[i]) printf("A"); 113 else printf("B"); 114 } 115 printf("\n"); 116 } 117 118 return 0; 119 }
Psong

F Cake

unsolved

G Bounce

經過的總數:res=(n-1)*(m-1)/gcd(n-1,m-1)+1

交點與拐點:(res-1)/gcd(n-1,m-1)

拐點個數:(n-1)/gcd(n-1,m-1)+(m-1)/gcd(n-1,m-1)-1

技術分享
 1 #include <bits/stdc++.h>
 2 #define lson (id*2)
 3 #define rson (id*2+1)
 4 #define mid ((l+r)/2)
 5 using namespace std;
 6 
 7 typedef long long LL;
 8 
 9 LL gcd(LL x,LL y){
10     return y==0?x:gcd(y,x%y);
11 }
12 
13 int main(){
14     LL n,m,k;
15     while(cin>>n>>m){
16         LL G=gcd(n-1,m-1);
17         LL res=(n-1)*(m-1)/G+1;
18         res=res-(res-1)/G+(n-1)/G+(m-1)/G-1;
19         cout<<res<<endl;
20     }
21 
22     return 0;
23 }
Psong

H Polynomial Product

unsolved

I Minimal

線段樹維護最大最小正負四個值即可

技術分享
  1 #include <bits/stdc++.h>
  2 #define lson (id*2)
  3 #define rson (id*2+1)
  4 #define mid ((l+r)/2)
  5 using namespace std;
  6 
  7 typedef long long LL;
  8 const int MAXN=2e5+5;
  9 const int inf=0x3f3f3f3f;
 10 
 11 int n,q;
 12 int a[MAXN];
 13 
 14 struct node{
 15     int val;
 16     int mizhen,mafu;
 17     int mi,ma;
 18 } tre[MAXN*4],ans;
 19 
 20 void push_up(int id,int ls,int rs){
 21     tre[id].mizhen=min(tre[ls].mizhen,tre[rs].mizhen);
 22     tre[id].mafu=max(tre[ls].mafu,tre[rs].mafu);
 23     tre[id].mi=min(tre[ls].mi,tre[rs].mi);
 24     tre[id].ma=max(tre[ls].ma,tre[rs].ma);
 25 }
 26 void build(int id,int l,int r){
 27     if(l==r){
 28         tre[id].mi=tre[id].ma=tre[id].val=a[l];
 29         if(a[l]>0){
 30             tre[id].mizhen=a[l];
 31             tre[id].mafu=-inf;
 32         }
 33         if(a[l]<0){
 34             tre[id].mizhen=inf;
 35             tre[id].mafu=a[l];
 36         }
 37         if(a[l]==0){
 38             tre[id].mizhen=a[l];
 39             tre[id].mafu=a[l];
 40         }
 41         return;
 42     }
 43     build(lson,l,mid);
 44     build(rson,mid+1,r);
 45     push_up(id,lson,rson);
 46 }
 47 
 48 void update(int id,int l,int r,int pos,int tt){
 49     if(l==r){
 50         tre[id].mi=tre[id].ma=tre[id].val=tt;
 51         if(tt>=0){
 52             tre[id].mizhen=tt;
 53             tre[id].mafu=-inf;
 54         }
 55         if(tt<=0){
 56             tre[id].mizhen=inf;
 57             tre[id].mafu=tt;
 58         }
 59         return;
 60     }
 61     if(pos<=mid)
 62         update(lson,l,mid,pos,tt);
 63     else
 64         update(rson,mid+1,r,pos,tt);
 65     push_up(id,lson,rson);
 66 }
 67 
 68 void query(int id,int l,int r,int ql,int qr){
 69     if(ql<=l&&r<=qr){
 70         ans.mizhen=min(ans.mizhen,tre[id].mizhen);
 71         ans.mafu=max(ans.mafu,tre[id].mafu);
 72         ans.ma=max(ans.ma,tre[id].ma);
 73         ans.mi=min(ans.mi,tre[id].mi);
 74         return;
 75     }
 76     if(ql<=mid)
 77         query(lson,l,mid,ql,qr);
 78     if(mid+1<=qr)
 79         query(rson,mid+1,r,ql,qr);
 80 }
 81 
 82 void solve(){
 83     LL res=(LL)ans.mi*ans.ma;
 84     res=min(res,(LL)ans.mi*ans.mi);
 85     res=min(res,(LL)ans.ma*ans.ma);
 86     if(ans.mafu!=-inf){
 87         res=min(res,(LL)ans.mafu*ans.mafu);
 88         res=min(res,(LL)ans.mafu*ans.mi);
 89         res=min(res,(LL)ans.mafu*ans.ma);
 90     }
 91     if(ans.mizhen!=inf){
 92         res=min(res,(LL)ans.mizhen*ans.mizhen);
 93         res=min(res,(LL)ans.mizhen*ans.mi);
 94         res=min(res,(LL)ans.mizhen*ans.ma);
 95     }
 96     if(ans.mafu!=-inf&&ans.mizhen!=inf) res=min(res,(LL)ans.mafu*ans.mizhen);
 97     printf("%lld\n",res);
 98 }
 99 
100 int main(){
101     int t;
102     scanf("%d",&t);
103     while(t--){
104         scanf("%d",&n);
105         n=(1<<n);
106         for(int i=1;i<=n;i++)
107             scanf("%d",&a[i]);
108         build(1,1,n);
109         scanf("%d",&q);
110         while(q--){
111             int op,x,y;
112             scanf("%d%d%d",&op,&x,&y);
113             if(op==1){
114                 x++; y++;
115                 ans.mi=ans.mizhen=inf;
116                 ans.ma=ans.mafu=-inf;
117                 query(1,1,n,x,y);
118                 solve();
119             }
120             else{
121                 x++;
122                 update(1,1,n,x,y);
123             }
124         }
125     }
126 
127     return 0;
128 }
Psong

J Typist‘s Problem

unsolved

2017 ACM/ICPC Asia Regional Beijing Online