T1:对于每个字母存储每次移动后x坐标与y坐标与所有操作开始前的的变化量,将(T/len)乘变化量再加第(T%len)个变化量即可。
Code:
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #define ll long long 5 #define MN 5005 6 using namespace std; 7 inline ll in(){ 8 ll x=0;bool f=0; char c; 9 for (;(c=getchar())<‘0‘||c>‘9‘;f=c==‘-‘); 10 for (x=c-‘0‘;(c=getchar())>=‘0‘&&c<=‘9‘;x=(x<<3)+(x<<1)+c-‘0‘); 11 return f?-x:x; 12 } 13 char ch[MN]; 14 ll dx[MN],dy[MN],ax,ay,t; 15 int main() 16 { 17 scanf("%s",ch);t=in(); 18 int len=strlen(ch);dx[0]=dy[0]=0; 19 for (int i=0;i<len;++i){ 20 if (i) dx[i]=dx[i-1];dy[i]=dy[i-1]; 21 switch (ch[i]){ 22 case ‘E‘:++dx[i];break; 23 case ‘S‘:--dy[i];break; 24 case ‘W‘:--dx[i];break; 25 case ‘N‘:++dy[i];break; 26 } 27 }int tr=t/len;ax=tr*dx[len-1];ay=tr*dy[len-1]; 28 t%=len;ax+=dx[t-1];ay+=dy[t-1]; 29 printf("%lld %lld",ax,ay);return 0; 30 }
T2:二分答案,注意需用到高精度计算。
Code:
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #define ll long long 5 #define MN 20000 6 using namespace std; 7 struct hpc{ 8 ll num[5005]; 9 int len; 10 }a,b; 11 char m[MN+5],n[MN+5]; 12 inline int max(ll a,ll b){return a>b?a:b;} 13 bool operator >(const hpc &a,const hpc &b){ 14 if (a.len!=b.len) return a.len>b.len; 15 for (int i=a.len-1;i>=0;--i) 16 if (a.num[i]!=b.num[i]) return a.num[i]>b.num[i]; 17 return 0; 18 } 19 hpc operator * (hpc &a,ll b){ 20 hpc ans;int &len=ans.len; 21 memset(ans.num,0,sizeof(ans.num)); 22 for (int i=0;i<a.len;++i){ 23 ans.num[i]+=a.num[i]*b; 24 ans.num[i+1]+=ans.num[i]/10000; 25 ans.num[i]%=10000; 26 }len=a.len; 27 for (;ans.num[len]>0;++len){ 28 ans.num[len+1]=ans.num[len]/10000; 29 ans.num[len]%=10000; 30 }return ans; 31 } 32 33 inline ll div(hpc &a,hpc &b){ 34 ll l=0ll,r=2000000000ll; 35 while (l+1<r){ 36 ll mid=(l+r)>>1; 37 if (b*mid>a) r=mid;else l=mid; 38 }if (b*r>a) return l;else return r; 39 } 40 hpc change(char *ch){ 41 int len=0,lc=strlen(ch);hpc ans; 42 memset(ans.num,0,sizeof(ans.num)); 43 for (int i=lc-1;i>=0;i-=4){ 44 int x=0,k=max(0,i-3); 45 for (int j=k;j<=i;++j) 46 x=(x<<3)+(x<<1)+(ch[j]-‘0‘); 47 ans.num[len++]=x; 48 }ans.len=len;return ans; 49 } 50 int main() 51 { 52 scanf("%s",m);scanf("%s",n); 53 a=change(m);b=change(n); 54 printf("%lld\n",div(a,b));return 0; 55 }
T3:令f[i]表示在第i个点能拿到的最大值.
对于第i个点,考虑贪心选取第[i-r,i-l]个点中的最大fi再加上ai即可。
考虑用堆维护[i-r,i-l]中f的最大值。时间复杂度O(n log n)。
Code:
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<queue> 5 #define ll long long 6 #define inf 0x7fffffff 7 #define MN 200005 8 using namespace std; 9 typedef pair<int,int> P;//f[pos],pos 10 priority_queue<P,vector<P> >q; 11 inline int in(){ 12 int x=0;bool f=0; char c; 13 for (;(c=getchar())<‘0‘||c>‘9‘;f=c==‘-‘); 14 for (x=c-‘0‘;(c=getchar())>=‘0‘&&c<=‘9‘;x=(x<<3)+(x<<1)+c-‘0‘); 15 return f?-x:x; 16 } 17 int f[MN],a[MN],pre[MN],way[MN],mx=-inf; 18 int n,l,r,lps,cnt=0; 19 int main() 20 { 21 n=in();l=in();r=in();int lps; 22 for (int i=0;i<=n;++i) a[i]=in(); 23 for (int i=1;i<=n;++i){ 24 if (i<l) f[i]=-inf; 25 else{ 26 int cur=q.top().second; 27 while (cur<i-r) q.pop(),cur=q.top().second; 28 pre[i]=cur;f[i]=f[cur]+a[i]; 29 }lps=i-l+1; 30 if (lps>=0&&f[lps]!=-inf)q.push(make_pair(f[lps],lps)); 31 }lps=n-r+1; 32 for (int i=lps;i<=n;++i) if (f[i]>mx) mx=f[i],way[0]=i; 33 printf("%d\n",mx); 34 for (int i=way[0];i;i=pre[i]) way[++cnt]=i;printf("0 "); 35 for (int i=cnt;i>0;--i) printf("%d ",way[i]);printf("-1"); 36 return 0; 37 }
T4:找到点数最多且字典序最先的强联通分量即可。
Code:
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #define MN 5005 5 #define ME 50005 6 #define inf 100005 7 using namespace std; 8 inline int in(){ 9 int x=0;bool f=0; char c; 10 for (;(c=getchar())<‘0‘||c>‘9‘;f=c==‘-‘); 11 for (x=c-‘0‘;(c=getchar())>=‘0‘&&c<=‘9‘;x=(x<<3)+(x<<1)+c-‘0‘); 12 return f?-x:x; 13 } 14 struct edge{ 15 int to,next; 16 }e[ME<<1],r[ME<<1]; 17 int hd[MN],rh[MN],t[MN],od[MN],ans[MN]; 18 bool vis[MN]; 19 int n,m,a,b,tp,mn,ct=0,siz,cnt=0,rct=0,amin=inf; 20 inline int min(int a,int b){return a<b?a:b;} 21 inline void ins (int x,int y){ 22 ++cnt;e[cnt].to=y;e[cnt].next=hd[x];hd[x]=cnt; 23 ++rct;r[rct].to=x;r[rct].next=rh[y];rh[y]=rct; 24 } 25 void dfs(int x){ 26 vis[x]=1; 27 for (int i=hd[x];i;i=e[i].next){ 28 int v=e[i].to; 29 if (!vis[v]) dfs(v); 30 } 31 od[++ct]=x; 32 } 33 void rdfs(int x){ 34 vis[x]=1;t[++ct]=x;mn=min(mn,x); 35 for (int i=rh[x];i;i=r[i].next){ 36 int v=r[i].to; 37 if (!vis[v]) rdfs(v); 38 } 39 } 40 int main() 41 { 42 n=in();m=in();memset(vis,0,sizeof(vis)); 43 for (int i=1;i<=m;++i){ 44 a=in();b=in();tp=in(); 45 if (tp==1) ins(a,b); 46 else ins(a,b),ins(b,a); 47 } 48 for (int i=1;i<=n;++i) if (!vis[i]) dfs(i); 49 memset(vis,0,sizeof(vis));ct=siz=0; 50 for (int i=n;i;--i){ 51 if (!vis[od[i]]) ct=0,mn=inf,rdfs(od[i]); 52 if (ct<siz||(ct==siz&&mn>amin)) continue; 53 siz=ct;amin=mn;memcpy(ans,t,sizeof(t)); 54 }sort(ans+1,ans+siz+1);printf("%d\n",siz); 55 for (int i=1;i<=siz;++i) printf("%d ",ans[i]);return 0; 56 }
时间: 2024-09-29 02:53:02