Codeforces Round #576 (Div. 2)(A、B、C、D)

http://codeforces.com/contest/1199

1、City Day

水题,简单循环判断

 1 #include<iostream>
 2 #include<sstream>
 3 #include<fstream>
 4 #include<algorithm>
 5 #include<cstring>
 6 #include<iomanip>
 7 #include<cstdlib>
 8 #include<cctype>
 9 #include<vector>
10 #include<string>
11 #include<cmath>
12 #include<ctime>
13 #include<stack>
14 #include<queue>
15 #include<map>
16 #include<set>
17 #define mem(a,b) memset(a,b,sizeof(a))
18 #define random(a,b) (rand()%(b-a+1)+a)
19 #define ll long long
20 #define ull unsigned long long
21 #define e 2.71828182
22 #define Pi acos(-1.0)
23 using namespace std;
24 const int MAXN=1e5+5;
25 int n,x,y;
26 int a[MAXN];
27 int read()
28 {
29     int s=1,x=0;
30     char ch=getchar();
31     while(!isdigit(ch)) {if(ch=='-') s=-1;ch=getchar();}
32     while(isdigit(ch)) {x=10*x+ch-'0';ch=getchar();}
33     return x*s;
34 }
35 int main()
36 {
37     n=read(),x=read(),y=read();
38     for(int i=1;i<=n;++i) a[i]=read();
39     for(int i=1;i<=n;++i)
40     {
41         bool flag=true;
42         for(int j=max(1,i-x);j<=i-1&&flag;++j) if(a[j]<a[i]) flag=false;
43         for(int j=i+1;j<=min(i+y,n)&&flag;++j) if(a[j]<a[i]) flag=false;
44         if(flag)
45         {
46             cout<<i<<endl;
47             return 0;
48         } 
49     }
50 }
View Code

 

2、Water Lily

水题,简单勾股定理、解一次方程

 1 #include<iostream>
 2 #include<sstream>
 3 #include<fstream>
 4 #include<algorithm>
 5 #include<cstring>
 6 #include<iomanip>
 7 #include<cstdlib>
 8 #include<cctype>
 9 #include<vector>
10 #include<string>
11 #include<cmath>
12 #include<ctime>
13 #include<stack>
14 #include<queue>
15 #include<map>
16 #include<set>
17 #define mem(a,b) memset(a,b,sizeof(a))
18 #define random(a,b) (rand()%(b-a+1)+a)
19 #define ll long long
20 #define ull unsigned long long
21 #define e 2.71828182
22 #define Pi acos(-1.0)
23 using namespace std;
24 const int eps=1e-6;
25 int read()
26 {
27     int s=1,x=0;
28     char ch=getchar();
29     while(!isdigit(ch)) {if(ch=='-') s=-1;ch=getchar();}
30     while(isdigit(ch)) {x=10*x+ch-'0';ch=getchar();}
31     return x*s;
32 }
33 int main()
34 {
35     double H,L;
36     cin>>H>>L;
37     double ans=(L*L-H*H)/(H+H);
38     cout<<setiosflags(ios::fixed)<<setprecision(13)<<ans<<"\n";
39 }
View Code

 

3、MP3

详见代码

 1 #include<iostream>
 2 #include<sstream>
 3 #include<fstream>
 4 #include<algorithm>
 5 #include<cstring>
 6 #include<iomanip>
 7 #include<cstdlib>
 8 #include<cctype>
 9 #include<vector>
10 #include<string>
11 #include<cmath>
12 #include<ctime>
13 #include<stack>
14 #include<queue>
15 #include<map>
16 #include<set>
17 #define mem(a,b) memset(a,b,sizeof(a))
18 #define random(a,b) (rand()%(b-a+1)+a)
19 #define ll long long
20 #define ull unsigned long long
21 #define e 2.71828182
22 #define Pi acos(-1.0)
23 using namespace std;
24 const int MAXN=4e5+5;
25 int n,I;
26 int a[MAXN],num[MAXN];
27 int read()
28 {
29     int s=1,x=0;
30     char ch=getchar();
31     while(!isdigit(ch)) {if(ch=='-') s=-1;ch=getchar();}
32     while(isdigit(ch)) {x=10*x+ch-'0';ch=getchar();}
33     return x*s;
34 }
35 int main()
36 {
37     //freopen("1.in","r",stdin);
38     n=read(),I=read();
39     int maxk=8*I/n,maxK=pow(2,maxk);
40     if(maxk>=20)
41     {
42         cout<<"0\n";
43         return 0;
44     }
45     for(int i=1;i<=n;++i) a[i]=read();
46     sort(a+1,a+n+1);
47     int cnt=1;
48     num[cnt]=1;
49     for(int i=2;i<=n;++i)
50     {
51         if(a[i]==a[i-1]) num[cnt]++;
52         else num[++cnt]=num[cnt-1]+1;
53     }
54     /*for(int i=1;i<=cnt;++i)
55     cout<<num[i]<<' ';cout<<endl;*/
56     if(cnt<=maxK)
57     {
58         cout<<"0\n";
59         return 0;
60     }
61     //给的数据共cnt个不同的值,从小到大相当于离散化了
62     //num[i]表示小于等于i的共num[i]个(等于i相当于原数据的第i小)
63     //num[r]-num[l-1]表示范围为[l,r]的数的数目
64     //接下来求范围长度为K的所有区间中包含数字最多的 
65     //cout<<"max:"<<maxK<<endl;
66     int max_con=-1;//最长连续 
67     for(int i=maxK;i<=cnt;++i)//枚举右边界 
68     {
69         int tmp=num[i]-num[i-maxK];//范围为[i-K+1,i],长度为K
70         max_con=max(max_con,tmp);
71     }
72     cout<<n-max_con<<endl;
73 }
View Code

 

4、Welfare State

 a.思维,不必每一次操作都更新

 1 #include<iostream>
 2 #include<sstream>
 3 #include<fstream>
 4 #include<algorithm>
 5 #include<cstring>
 6 #include<iomanip>
 7 #include<cstdlib>
 8 #include<cctype>
 9 #include<vector>
10 #include<string>
11 #include<cmath>
12 #include<ctime>
13 #include<stack>
14 #include<queue>
15 #include<map>
16 #include<set>
17 #define mem(a,b) memset(a,b,sizeof(a))
18 #define random(a,b) (rand()%(b-a+1)+a)
19 #define ll long long
20 #define ull unsigned long long
21 #define e 2.71828182
22 #define Pi acos(-1.0)
23 #define P pair<int,int>
24 using namespace std;
25 const int MAXN=2e5+5; 
26 int a[MAXN],n,q,p,x;
27 map< int,P > opt1;
28 int opt2[MAXN];
29 int read()
30 {
31     int s=1,x=0;
32     char ch=getchar();
33     while(!isdigit(ch)) {if(ch=='-') s=-1;ch=getchar();}
34     while(isdigit(ch)) {x=10*x+ch-'0';ch=getchar();}
35     return x*s;
36 }
37 int main()
38 {
39     n=read();
40     for(int i=1;i<=n;++i) a[i]=read();
41     q=read();
42     int id;
43     for(int i=1;i<=q;++i)
44     {
45         id=read();
46         if(id==1)
47         {
48             p=read(),x=read();
49             if(opt1.count(p)) opt1[p]=make_pair(x,i);
50             else opt1.insert(make_pair(p,make_pair(x,i)));
51         }
52         else if(id==2)
53         {
54             x=read();
55             opt2[i]=x;
56         }
57     }
58     for(int i=q;i>=1;--i)
59     opt2[i]=max(opt2[i],opt2[i+1]);//opt2[i]表示大于等于第i次序时的第二类操作的最大x 
60     /*for(int i=1;i<=q;++i)
61     cout<<opt2[i]<<' ';cout<<endl;*/
62     for(int i=1;i<=n;++i)
63     {
64         if(opt1.count(i))//有第一类操作 
65             a[i]=max(opt1[i].first,opt2[opt1[i].second+1]);
66         else
67             a[i]=max(a[i],opt2[1]);
68         cout<<a[i]<<' ';
69     }
70     return 0;
71 }
View Code

b、线段树

  1 #include<iostream>
  2 #include<sstream>
  3 #include<fstream>
  4 #include<algorithm>
  5 #include<cstring>
  6 #include<iomanip>
  7 #include<cstdlib>
  8 #include<cctype>
  9 #include<vector>
 10 #include<string>
 11 #include<cmath>
 12 #include<ctime>
 13 #include<stack>
 14 #include<queue>
 15 #include<map>
 16 #include<set>
 17 #define mem(a,b) memset(a,b,sizeof(a))
 18 #define random(a,b) (rand()%(b-a+1)+a)
 19 #define ll long long
 20 #define ull unsigned long long
 21 #define e 2.71828182
 22 #define Pi acos(-1.0)
 23 #define ls(rt) (rt<<1)
 24 #define rs(rt) (rt<<1|1)
 25 #define INF 0x7fffffff
 26 using namespace std;
 27 const int MAXN=2e5+5;
 28 int a[MAXN],n,q,p,x;
 29 int read()
 30 {
 31     int s=1,x=0;
 32     char ch=getchar();
 33     while(!isdigit(ch)) {if(ch=='-') s=-1;ch=getchar();}
 34     while(isdigit(ch)) {x=10*x+ch-'0';ch=getchar();}
 35     return x*s;
 36 }
 37 struct node
 38 {
 39     int Min,tag;
 40 }T[MAXN<<2];
 41 inline void push_up(int rt)
 42 {
 43     T[rt].Min=min(T[ls(rt)].Min,T[rs(rt)].Min);
 44 }
 45 inline void build(int rt,int l,int r)
 46 {
 47     if(l==r)
 48     {
 49         T[rt].Min=a[l];
 50         T[rt].tag=0;
 51         return ;
 52     }
 53     T[rt].tag=0,T[rt].Min=INF;
 54     int mid=(l+r)>>1;
 55     build(ls(rt),l,mid);
 56     build(rs(rt),mid+1,r);
 57     push_up(rt);
 58 }
 59 inline void push_down(int rt,int l,int r)
 60 {
 61     if(!T[rt].tag) return ;
 62     T[ls(rt)].tag=max(T[ls(rt)].tag,T[rt].tag);
 63     T[rs(rt)].tag=max(T[rs(rt)].tag,T[rt].tag);
 64     T[ls(rt)].Min=max(T[ls(rt)].Min,T[rt].tag);
 65     T[rs(rt)].Min=max(T[rs(rt)].Min,T[rt].tag);
 66     T[rt].tag=0;
 67 }
 68 inline void update_single(int rt,int l,int r,int nl,int nr,int k)
 69 {
 70     //nl==nr 
 71     if(l==r)
 72     {
 73         T[rt].Min=k;
 74         return;
 75     }
 76     push_down(rt,l,r);
 77     int mid=(l+r)>>1;
 78     if(nl<=mid) update_single(ls(rt),l,mid,nl,nr,k);
 79     else update_single(rs(rt),mid+1,r,nl,nr,k);
 80     push_up(rt); 
 81 }
 82 inline void update_interval(int rt,int k)
 83 {
 84     if(k<=T[rt].Min) return;
 85     T[rt].Min=k;
 86     T[rt].tag=max(T[rt].tag,k);
 87 }
 88 int query_single(int rt,int l,int r,int nl,int nr)
 89 {
 90     //nl==nr
 91     if(l==r) return T[rt].Min;
 92     push_down(rt,l,r);
 93     int mid=(l+r)>>1;
 94     if(nl<=mid) return query_single(ls(rt),l,mid,nl,nr);
 95     else return query_single(rs(rt),mid+1,r,nl,nr); 
 96 }
 97 int main()
 98 {
 99     n=read();
100     for(int i=1;i<=n;++i) a[i]=read();
101     build(1,1,n);
102     q=read();
103     int opt;
104     while(q--)
105     {
106         opt=read();
107         if(opt==1)
108         {
109             p=read(),x=read();
110             update_single(1,1,n,p,p,x);
111         }
112         else 
113         {
114             x=read();
115             update_interval(1,x);
116         }
117     } 
118     for(int i=1;i<=n;++i)
119     cout<<query_single(1,1,n,i,i)<<' '; 
120 }
View Code

 

转载于:https://www.cnblogs.com/wangzhebufangqi/p/11299849.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值