B.和数检测
离散化
#include <iostream> #include<cstdio> #include<cstring> #include<algorithm> typedef long long ll; using namespace std; ll a[1000005],b[1000005]; ll cnt=0; ll getid(ll x){ return lower_bound(b+1,b+1+cnt,x)-b; } int main() { ll t;scanf("%lld",&t); while(t--){ ll n,m;scanf("%lld%lld",&n,&m); for(ll i=1;i<=n;i++){ scanf("%lld",&a[i]); b[i]=a[i]; } sort(b+1,b+1+n); cnt=unique(b+1,b+1+n)-(b+1); ll flag=0; for(ll i=1;i<=n;i++){ ll x=m-a[i]; ll p=getid(x); //printf("x=%lld p=%lld ",x,p); if(b[p]==x) {flag=1;break;} } if(flag) puts("1"); else puts("0"); } return 0; }
A.括号序列
栈
#include <iostream> #include<cstdio> #include<cmath> #include<cstring> #include<algorithm> #include<stack> typedef long long ll; using namespace std; char s[1000005]; struct Node{ char c; ll t; }; stack<Node> stk; ll C(ll n){ if(n<2) return 0; else return n*(n-1)/2; } int main() { scanf("%s",s+1); s[0]=')'; ll len=strlen(s+1);len++; ll ans=0; for(ll i=0;i<len;i++){ if(stk.empty()) {stk.push(Node{s[i],0});continue;} if(s[i]=='('){ stk.top().t++; stk.push(Node{s[i],0}); } else{ if(!stk.empty()){ Node top=stk.top(); if(top.c=='('){ ans+=C(top.t)+1; stk.pop(); } else { stk.push(Node{s[i],0}); } } } } while(!stk.empty()){ ans+=C(stk.top().t); char c=stk.top().c; stk.pop(); if(c=='('&&!stk.empty()) stk.top().t--; } printf("%lld ",ans); return 0; } /* )()())()() 6 ((())()((())() 8 */
枚举
#include <iostream> #include<cstdio> #include<cmath> #include<algorithm> using namespace std; char Map[5][5]; bool check1(char c){ for(int i=1;i<=3;i++){ int flag=1; for(int j=1;j<=3;j++){ if(Map[i][j]!=c) flag=0; } if(flag) return 1; } for(int j=1;j<=3;j++){ int flag=1; for(int i=1;i<=3;i++){ if(Map[i][j]!=c) flag=0; } if(flag) return 1; } if(Map[1][1]==c&&Map[2][2]==c&&Map[3][3]==c) return 1; if(Map[1][3]==c&&Map[2][2]==c&&Map[3][1]==c) return 1; return 0; } bool check2(char c1,char c2){ for(int i=1;i<=3;i++){ int flag=1; for(int j=1;j<=3;j++){ if(Map[i][j]!=c1&&Map[i][j]!=c2) flag=0; } if(flag){ int cnt1=0,cnt2=0; for(int j=1;j<=3;j++){ if(Map[i][j]==c1) cnt1++; else cnt2++; } if(cnt1&&cnt2) return 1; } } for(int j=1;j<=3;j++){ int flag=1; for(int i=1;i<=3;i++){ if(Map[i][j]!=c1&&Map[i][j]!=c2) flag=0; } if(flag){ int cnt1=0,cnt2=0; for(int i=1;i<=3;i++){ if(Map[i][j]==c1) cnt1++; else cnt2++; } if(cnt1&&cnt2) return 1; } } if((Map[1][1]==c1||Map[1][1]==c2)&&(Map[2][2]==c1||Map[2][2]==c2)&&(Map[3][3]==c1||Map[3][3]==c2)){ int cnt1=0,cnt2=0; if(Map[1][1]==c1) cnt1++;if(Map[1][1]==c2) cnt2++; if(Map[2][2]==c1) cnt1++;if(Map[2][2]==c2) cnt2++; if(Map[3][3]==c1) cnt1++;if(Map[3][3]==c2) cnt2++; if(cnt1&&cnt2) return 1; } if((Map[1][3]==c1||Map[1][3]==c2)&&(Map[2][2]==c1||Map[2][2]==c2)&&(Map[3][1]==c1||Map[3][1]==c2)){ int cnt1=0,cnt2=0; if(Map[1][3]==c1) cnt1++;if(Map[1][3]==c2) cnt2++; if(Map[2][2]==c1) cnt1++;if(Map[2][2]==c2) cnt2++; if(Map[3][1]==c1) cnt1++;if(Map[3][1]==c2) cnt2++; if(cnt1&&cnt2) return 1; } return 0; } int main() { for(int i=1;i<=3;i++){ for(int j=1;j<=3;j++){ scanf(" %c",&Map[i][j]); } } int ans1=0; for(int i=0;i<26;i++){ char c='A'+i; if(check1(c)) ans1++; } int ans2=0; for(int i=0;i<26;i++){ char c1='A'+i; for(int j=0;j<i;j++){ char c2='A'+j; if(check2(c1,c2)) ans2++; } } printf("%d %d ",ans1,ans2); return 0; }
B.求和VII
公式
import java.math.BigInteger; import java.util.Scanner; public class Main { public static void main(String[] args) { BigInteger x1,y1,x2,y2,m; String a,b,c,d,e; Scanner cin=new Scanner(System.in); a=cin.next();b=cin.next();c=cin.next();d=cin.next(); e=cin.next(); x1=new BigInteger(a); y1=new BigInteger(b); x2=new BigInteger(c); y2=new BigInteger(d); m=new BigInteger(e); BigInteger ans=BigInteger.valueOf(1); ans=x2.subtract(x1).add(BigInteger.ONE).multiply(x1.add(x2)).multiply(y2.subtract(y1).add(BigInteger.ONE)).divide(BigInteger.valueOf(2)); ans=ans.add(y2.subtract(y1).add(BigInteger.ONE).multiply(y1.add(y2)).multiply(x2.subtract(x1).add(BigInteger.ONE)).divide(BigInteger.valueOf(2))); ans=ans.subtract(x2.subtract(x1).add(BigInteger.ONE).multiply(y2.subtract(y1).add(BigInteger.ONE))); System.out.println(ans.mod(m)); } }
枚举
#include <iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; int n,m,k; int b[205],a[205],copa[205]; int v[205]; int lev[205]; bool check(){ memset(copa,0,sizeof(copa)); for(int i=1;i<=n;i++) copa[i]=a[i]; int i,p; for(i=1,p=1;i<=m&&p<=n;i++){ while(p<=n){ //printf("i=%d p=%d ",i,p); if(copa[p]){ if(copa[p]==b[i]) {p++;break;} else if(lev[copa[p]]>lev[b[i]]) return 0; else p++; } else { if(v[b[i]]) {p++;continue;} else copa[p]=b[i],p++;break; } } } if(i>m) return 1; return 0; } int main() { scanf("%d%d%d",&n,&m,&k); for(int i=1;i<=m;i++){ scanf("%d",&b[i]); lev[b[i]]=i; } int flag=0; for(int i=1;i<=k;i++){ int c,p;scanf("%d%d",&c,&p); v[c]=1; a[p]=c; if(c==1) flag=p; } if(flag) {printf("%d ",flag); return 0;} int i; for(i=1;i<=n;i++){ if(a[i]!=0) continue; a[i]=1; if(check()) break; else a[i]=0; } printf("%d ",i); return 0; } /* 6 4 1 2 3 4 5 2 1 */
D.Dove 打扑克
并查集+set+单调队列
#include <iostream> #include<cstdio> #include<set> #include<algorithm> typedef long long ll; using namespace std; set<ll> s;set<ll>::iterator it; ll n,m; ll fa[100005]; ll cnt[100005]; ll tot[100005],q[100005]; ll findd(ll x){ if(x==fa[x]) return x; else return fa[x]=findd(fa[x]); } void join(ll x,ll y){ ll fax=findd(x); ll fay=findd(y); if(fax==fay) return; tot[cnt[fay]]--;if(tot[cnt[fay]]==0) s.erase(cnt[fay]); cnt[fay]+=cnt[fax]; tot[cnt[fay]]++;if(tot[cnt[fay]]==1) s.insert(cnt[fay]); tot[cnt[fax]]--;if(tot[cnt[fax]]==0) s.erase(cnt[fax]); cnt[fax]=0; fa[fax]=fay; } int main() { scanf("%lld%lld",&n,&m); for(ll i=1;i<=n;i++) fa[i]=i,cnt[i]=1; tot[1]=n;s.insert(1); ll op,x,y,c; ll head=1,tail=0,tmp=0,ans=0,all=0; for(ll i=1;i<=m;i++){ scanf("%lld",&op); if(op==1){ scanf("%lld%lld",&x,&y); join(x,y); } else{ scanf("%lld",&c); it=s.begin(); head=1,tail=0,tmp=0,ans=0,all=0; while(it!=s.end()){ all+=tot[*it]; q[++tail]=*it; while(head<=tail&&(*it-q[head])>=c) tmp-=tot[q[head]],head++; if(head>tail) ans+=0,tmp=0; else{ ans+=tmp*tot[*it]+tot[*it]*(tot[*it]-1)/2;; tmp+=tot[*it]; } it++; } printf("%lld ",all*(all-1)/2-ans); } } return 0; }