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

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 }

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 }

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 }

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 }

原文地址:https://www.cnblogs.com/Leonard-/p/8408461.html

时间: 2024-11-08 03:23:48

Educational Codeforces Round 37 (Rated for Div. 2)A,B,C,F的相关文章

[Codeforces]Educational Codeforces Round 37 (Rated for Div. 2)

Water The Garden #pragma comment(linker, "/STACK:102400000,102400000") #include<stdio.h> #include<string.h> #include<stdlib.h> #include<vector> #include<algorithm> #include<iostream> #include<map> #inclu

Educational Codeforces Round 37 (Rated for Div. 2) E. Connected Components? 图论

E. Connected Components? You are given an undirected graph consisting of n vertices and edges. Instead of giving you the edges that exist in the graph, we give you m unordered pairs (x,?y) such that there is no edge between x and y, and if some pair

Educational Codeforces Round 80 (Rated for Div. 2)E(树状数组,模拟,思维)

1 #define HAVE_STRUCT_TIMESPEC 2 #include<bits/stdc++.h> 3 using namespace std; 4 int mn[600007],mx[600007],a[600007],pos[600007],sum[600007]; 5 int n,m; 6 int lowbit(int x){ 7 return x&(-x); 8 } 9 void add(int x,int val){//单点更新 10 while(x<60

Educational Codeforces Round 55 (Rated for Div. 2) C. Multi-Subject Competition (实现,贪心,排序)

C. Multi-Subject Competition time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output A multi-subject competition is coming! The competition has m different subjects participants can choose from. That'

Educational Codeforces Round 36 (Rated for Div. 2)

Educational Codeforces Round 36 (Rated for Div. 2) F. Imbalance Value of a Tree You are given a tree T consisting of n vertices. A number is written on each vertex; the number written on vertex i is ai. Let's denote the function I(x,?y) as the differ

Educational Codeforces Round 69 (Rated for Div. 2) B - Pillars

Educational Codeforces Round 69 (Rated for Div. 2) B - Pillars There are n pillars aligned in a row and numbered from 1 to n. Initially each pillar contains exactly one disk. The i-th pillar contains a disk having radius ai. You can move these disks

Educational Codeforces Round 71 (Rated for Div. 2) A - There Are Two Types Of Burgers

原文链接:https://www.cnblogs.com/xwl3109377858/p/11404050.html Educational Codeforces Round 71 (Rated for Div. 2) A - There Are Two Types Of Burgers There are two types of burgers in your restaurant — hamburgers and chicken burgers! To assemble a hamburg

Educational Codeforces Round 71 (Rated for Div. 2) D - Number Of Permutations

原文链接:https://www.cnblogs.com/xwl3109377858/p/11405773.html Educational Codeforces Round 71 (Rated for Div. 2) D - Number Of Permutations You are given a sequence of n pairs of integers: (a1,b1),(a2,b2),…,(an,bn). This sequence is called bad if it is

Educational Codeforces Round 72 (Rated for Div. 2)-D. Coloring Edges-拓扑排序

Educational Codeforces Round 72 (Rated for Div. 2)-D. Coloring Edges-拓扑排序 [Problem Description] ? 给你一个有向图,给用最少的颜色给每条边染色,要保证不存在一个环中的所有边都是同一个颜色. [Solution] ? 用拓扑排序判断图中是否存在环,若图中不存在环,则所有边都是同一种颜色.否则,同一个环中,只要用两种颜色就可以满足题目条件,所以总的颜色数就是两种,对于一个环,一定会存在两种边:1.节点号小