1. 程式人生 > >Educational Codeforces Round 37 (Rated for Div. 2)A,B,C,F

Educational Codeforces Round 37 (Rated for Div. 2)A,B,C,F

fine 暴力模擬 需要 lose sort codeforce 結構 分享圖片 線段樹

A Water The Garden

數據不大,暴力模擬下直至把每個花床都遍歷過的過程即可

技術分享圖片
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 #define PI acos(-1.0)
 5 #define INF 1e18
 6 #define inf 0x3f3f3f3f
 7 #define FAST_IO ios::sync_with_stdio(false)
 8 
 9 const int N=456;
10 typedef long long LL;
11 int x[N],vis[N];
12 13 int main(){ 14 FAST_IO; 15 int t,n,k; 16 cin>>t; 17 18 while(t--){ 19 int time=0,cnt=0; 20 memset(vis,0,sizeof(vis)); 21 cin>>n>>k; 22 for(int i=1;i<=k;i++) cin>>x[i]; 23 while(1){ 24 time++;
25 for(int i=1;i<=k;i++){ 26 if((x[i]+time-1)>=1&&(x[i]+time-1)<=n){ 27 if(!vis[x[i]+time-1]) vis[x[i]+time-1]=1,cnt++; 28 } 29 if((x[i]-time+1)>=1&&(x[i]-time+1)<=n){ 30 if
(!vis[x[i]-time+1]) vis[x[i]-time+1]=1,cnt++; 31 } 32 } 33 if(cnt>=n) break; 34 } 35 cout<<time<<endl; 36 } 37 38 return 0; 39 }
View Code

B Tea Queue

模擬下過程。如果跑的時間比這個人結束時間還大,那麽這個人不能喝茶輸出,如果開始時間已經超過設定跑的時間,更新下跑的時間。 技術分享圖片
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 #define PI acos(-1.0)
 5 #define INF 1e18
 6 #define inf 0x3f3f3f3f
 7 #define FAST_IO ios::sync_with_stdio(false)
 8 
 9 const int N=1234;
10 typedef long long LL;
11 struct node{
12     int l,r,idx;
13 }T[N];
14 
15 bool cmp(node x,node y){
16     if(x.l==y.l) return x.idx<y.idx;
17     return x.l<y.l;
18 }
19 
20 int main(){
21     FAST_IO;
22     int t,n;
23     cin>>t;
24 
25     while(t--){
26         int time=-1;
27         cin>>n;
28         for(int i=1;i<=n;i++){
29             cin>>T[i].l>>T[i].r;
30             T[i].idx=i;
31         }
32         sort(T+1,T+1+n,cmp);
33         for(int i=1;i<=n;i++){
34             if(time>T[i].r) {cout<<0<<" ";continue;}
35             if(T[i].l>time) time=T[i].l;
36             cout<<time<<" ";time++;
37         }
38         cout<<endl;
39     }
40 
41     return 0;
42 }
View Code C Swap Adjacent Elements 發現自己學的sort一直都是錯的... sort(),裏面的前兩個參數分別是要排序的首地址與尾地址的下一個地址, 第二個也可以認為是首地址+需要排序的區間長度。 這道題目只要找到連續的1,然後那個區間排序即可。 技術分享圖片
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 #define PI acos(-1.0)
 5 #define INF 1e18
 6 #define inf 0x3f3f3f3f
 7 #define FAST_IO ios::sync_with_stdio(false)
 8 
 9 const int N=200000+100;
10 typedef long long LL;
11 int a[N];
12 
13 int main(){
14     FAST_IO;
15     string s;
16     int n,l=-1,r=-1;
17     cin>>n;
18     for(int i=0;i<n;i++) cin>>a[i];
19     cin>>s;
20     for(int i=0;i<n-1;i++){
21         if(s[i]==1){
22             if(l==-1) l=i;
23             r=i;
24         }
25         else if(s[i]==0&&l!=-1){
26             sort(a+l,a+r+2);
27             l=-1;
28         }
29     }
30     if(l!=-1) sort(a+l,a+r+2);
31     for(int i=0;i<n;i++){
32         if(a[i]!=(i+1)) {cout<<"NO"<<endl;return 0;}
33     }
34     cout<<"YES"<<endl;
35     return 0;
36 }
View Code F SUM and REPLACE 挺裸的線段樹。和上次那道線段樹一樣的套路。我們知道D(1)=1,D(2)=2。結構體裏放一個D(ai)當前區間的最大值,然後在更新的時候,如果當前區間的最大值mx<=2。那麽沒有繼續更新的必要,提前退出即可。(通過這個剪枝降低時間復雜度) 技術分享圖片
  1 #include <bits/stdc++.h>
  2 using namespace std;
  3 
  4 #define PI acos(-1.0)
  5 #define INF 1e18
  6 #define inf 0x3f3f3f3f
  7 #define FAST_IO ios::sync_with_stdio(false)
  8 
  9 const int N=1e6+10;
 10 typedef long long LL;
 11 LL ans;
 12 LL a[N],d[N];
 13 
 14 struct Tree
 15 {
 16     LL l,r;
 17     LL sum,mx;
 18 };
 19 Tree tree[2*N];
 20 
 21 void pushup(LL x)
 22 {
 23     LL tmp=x<<1;
 24     tree[x].sum=tree[tmp].sum+tree[tmp+1].sum;
 25     tree[x].mx=max(tree[tmp].mx,tree[tmp+1].mx);
 26 }
 27 
 28 void build(LL l,LL r,LL x)
 29 {
 30     tree[x].l=l;
 31     tree[x].r=r;
 32     if(l==r)
 33     {
 34         tree[x].mx=tree[x].sum=a[l];
 35         return ;
 36     }
 37     LL tmp=x<<1;
 38     LL mid=(l+r)>>1;
 39     build(l,mid,tmp);
 40     build(mid+1,r,tmp+1);
 41     pushup(x);
 42 }
 43 
 44 void update(LL l,LL r,LL x)
 45 {
 46     if(r<tree[x].l||l>tree[x].r||tree[x].mx<=2) return ;
 47     if(tree[x].l==tree[x].r)
 48     {
 49         tree[x].mx=tree[x].sum=d[tree[x].sum];
 50         return ;
 51     }
 52     LL tmp=x<<1;
 53     LL mid=(tree[x].l+tree[x].r)>>1;
 54     if(r<=mid) update(l,r,tmp);
 55     else if(l>mid) update(l,r,tmp+1);
 56     else
 57     {
 58         update(l,mid,tmp);
 59         update(mid+1,r,tmp+1);
 60     }
 61     pushup(x);
 62 }
 63 
 64 void query(LL l,LL r,LL x)
 65 {
 66     if(r<tree[x].l||l>tree[x].r) return ;
 67     if(l<=tree[x].l&&r>=tree[x].r)
 68     {
 69         ans+=tree[x].sum;
 70         return ;
 71     }
 72     LL tmp=x<<1;
 73     LL mid=(tree[x].l+tree[x].r)>>1;
 74     if(r<=mid) query(l,r,tmp);
 75     else if(l>mid) query(l,r,tmp+1);
 76     else
 77     {
 78         query(l,mid,tmp);
 79         query(mid+1,r,tmp+1);
 80     }
 81 }
 82 
 83 int main(){
 84     LL n,m,op,l,r;
 85     scanf("%lld %lld",&n,&m);
 86     for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
 87 
 88     for(int i=1;i<=1000000;i++)
 89     for(int j=i;j<=1000000;j+=i)
 90     d[j]++;
 91 
 92     build(1,n,1);
 93 
 94     for(int i=1;i<=m;i++){
 95         scanf("%lld %lld %lld",&op,&l,&r);
 96         if(op==1){
 97             update(l,r,1);
 98         }
 99         else{
100             ans=0;
101             query(l,r,1);
102             printf("%lld\n",ans);
103         }
104     }
105 
106     return 0;
107 }
View Code

Educational Codeforces Round 37 (Rated for Div. 2)A,B,C,F