Codeforces Round #436 (Div. 2)

Codeforces Round #436 (Div. 2)

A. Fair Game

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<cmath>
 6 #include<cstdlib>
 7 #include<vector>
 8 using namespace std;
 9 typedef long long ll;
10 typedef long double ld;
11 typedef pair<int,int> pr;
12 const double pi=acos(-1);
13 #define rep(i,a,n) for(int i=a;i<=n;i++)
14 #define per(i,n,a) for(int i=n;i>=a;i--)
15 #define Rep(i,u) for(int i=head[u];i;i=Next[i])
16 #define clr(a) memset(a,0,sizeof(a))
17 #define pb push_back
18 #define mp make_pair
19 #define fi first
20 #define sc second
21 #define pq priority_queue
22 #define pqb priority_queue <int, vector<int>, less<int> >
23 #define pqs priority_queue <int, vector<int>, greater<int> >
24 #define vec vector
25 ld eps=1e-9;
26 ll pp=1000000007;
27 ll mo(ll a,ll pp){if(a>=0 && a<pp)return a;a%=pp;if(a<0)a+=pp;return a;}
28 ll powmod(ll a,ll b,ll pp){ll ans=1;for(;b;b>>=1,a=mo(a*a,pp))if(b&1)ans=mo(ans*a,pp);return ans;}
29 void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
30 //void add(int x,int y,int z){ v[++e]=y; next[e]=head[x]; head[x]=e; cost[e]=z; }
31 int dx[5]={0,-1,1,0,0},dy[5]={0,0,0,-1,1};
32 ll read(){ ll ans=0; char last=' ',ch=getchar();
33 while(ch<'0' || ch>'9')last=ch,ch=getchar();
34 while(ch>='0' && ch<='9')ans=ans*10+ch-'0',ch=getchar();
35 if(last=='-')ans=-ans; return ans;
36 }
37 int a[1000],f[1000];
38 int main(){
39     int n=read(),num=0,a1=0,a2=0;
40     for (int i=1;i<=n;i++) a[i]=read(),f[a[i]]++;
41     for (int i=1;i<=100;i++)
42         if (f[i]>0) {
43             if (!a1){
44                 a1=i;
45             } else a2=i;
46             num++;
47         }
48     if (num!=2 || f[a1]!=f[a2]) puts("NO");
49     else {
50         puts("YES");
51         printf("%d %d\n",a1,a2);
52     }
53     return 0;
54 } 
View Code

B. Polycarp and Letters

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<cmath>
 6 #include<cstdlib>
 7 #include<vector>
 8 using namespace std;
 9 typedef long long ll;
10 typedef long double ld;
11 typedef pair<int,int> pr;
12 const double pi=acos(-1);
13 #define rep(i,a,n) for(int i=a;i<=n;i++)
14 #define per(i,n,a) for(int i=n;i>=a;i--)
15 #define Rep(i,u) for(int i=head[u];i;i=Next[i])
16 #define clr(a) memset(a,0,sizeof(a))
17 #define pb push_back
18 #define mp make_pair
19 #define fi first
20 #define sc second
21 #define pq priority_queue
22 #define pqb priority_queue <int, vector<int>, less<int> >
23 #define pqs priority_queue <int, vector<int>, greater<int> >
24 #define vec vector
25 ld eps=1e-9;
26 ll pp=1000000007;
27 ll mo(ll a,ll pp){if(a>=0 && a<pp)return a;a%=pp;if(a<0)a+=pp;return a;}
28 ll powmod(ll a,ll b,ll pp){ll ans=1;for(;b;b>>=1,a=mo(a*a,pp))if(b&1)ans=mo(ans*a,pp);return ans;}
29 void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
30 //void add(int x,int y,int z){ v[++e]=y; next[e]=head[x]; head[x]=e; cost[e]=z; }
31 int dx[5]={0,-1,1,0,0},dy[5]={0,0,0,-1,1};
32 ll read(){ ll ans=0; char last=' ',ch=getchar();
33 while(ch<'0' || ch>'9')last=ch,ch=getchar();
34 while(ch>='0' && ch<='9')ans=ans*10+ch-'0',ch=getchar();
35 if(last=='-')ans=-ans; return ans;
36 }
37 char ch[500];
38 int f[100],num,ans;
39 int main(){
40     int n=read();
41     scanf("%s",ch+1);
42     for (int i=1;i<=n;i++){
43         if (ch[i]>='a' && ch[i]<='z'){
44             if (!f[ch[i]-'a']) num++;
45             f[ch[i]-'a']=1;
46             ans=max(ans,num);
47         } else {
48             num=0; memset(f,0,sizeof(f));
49         }
50     }
51     printf("%d",ans);
52     return 0;
53 } 
View Code

C. Bus

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<cmath>
 6 #include<cstdlib>
 7 #include<vector>
 8 using namespace std;
 9 typedef long long ll;
10 typedef long double ld;
11 typedef pair<int,int> pr;
12 const double pi=acos(-1);
13 #define rep(i,a,n) for(int i=a;i<=n;i++)
14 #define per(i,n,a) for(int i=n;i>=a;i--)
15 #define Rep(i,u) for(int i=head[u];i;i=Next[i])
16 #define clr(a) memset(a,0,sizeof(a))
17 #define pb push_back
18 #define mp make_pair
19 #define fi first
20 #define sc second
21 #define pq priority_queue
22 #define pqb priority_queue <int, vector<int>, less<int> >
23 #define pqs priority_queue <int, vector<int>, greater<int> >
24 #define vec vector
25 ld eps=1e-9;
26 ll pp=1000000007;
27 ll mo(ll a,ll pp){if(a>=0 && a<pp)return a;a%=pp;if(a<0)a+=pp;return a;}
28 ll powmod(ll a,ll b,ll pp){ll ans=1;for(;b;b>>=1,a=mo(a*a,pp))if(b&1)ans=mo(ans*a,pp);return ans;}
29 void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
30 //void add(int x,int y,int z){ v[++e]=y; next[e]=head[x]; head[x]=e; cost[e]=z; }
31 int dx[5]={0,-1,1,0,0},dy[5]={0,0,0,-1,1};
32 ll read(){ ll ans=0; char last=' ',ch=getchar();
33 while(ch<'0' || ch>'9')last=ch,ch=getchar();
34 while(ch>='0' && ch<='9')ans=ans*10+ch-'0',ch=getchar();
35 if(last=='-')ans=-ans; return ans;
36 }
37 int main(){
38     int a=read(),b=read(),f=read(),k=read(),now=b,le=f,a_=(a-f)*2,o_=f*2,flag=1,num=0;
39     if (k==1){
40         if (f>b || a-f>b) {puts("-1"); return 0;}
41         if (a>b) {
42             puts("1"); return 0;
43         } else {
44             puts("0"); return 0;
45         }
46     }
47     now-=f;
48     if (now<0){
49         puts("-1");
50         return 0;
51     }
52     if (a_>now) {now=b; num++;}
53     for (int i=1;i<=k-1;i++){
54         if (now<0){
55             puts("-1");
56             return 0;
57         }
58         if (i&1){
59             now-=a_;
60             if (now<0){
61                 puts("-1");
62                 return 0;
63             }
64             if (i<k-1 && o_>now) {now=b; num++;}
65         } else {
66             now-=o_;
67             if (now<0){
68                 puts("-1");
69                 return 0;
70             }
71             if (i<k-1 && a_>now) {now=b; num++;}
72         }
73     }
74     if (k%2==0){
75     if (now<f) {now=b; num++;}
76     now-=f;
77     if (now<0){
78         puts("-1");
79         return 0;
80     }
81     printf("%d\n",num);
82     } else {
83     if (now<(a-f)) {now=b; num++;}
84     now-=(a-f);
85     if (now<0){
86         puts("-1");
87         return 0;
88     }
89     printf("%d\n",num);
90     }
91     return 0;
92 } 
View Code

D. Make a Permutation!

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<cmath>
 6 #include<cstdlib>
 7 #include<vector>
 8 using namespace std;
 9 typedef long long ll;
10 typedef long double ld;
11 typedef pair<int,int> pr;
12 const double pi=acos(-1);
13 #define rep(i,a,n) for(int i=a;i<=n;i++)
14 #define per(i,n,a) for(int i=n;i>=a;i--)
15 #define Rep(i,u) for(int i=head[u];i;i=Next[i])
16 #define clr(a) memset(a,0,sizeof(a))
17 #define pb push_back
18 #define mp make_pair
19 #define fi first
20 #define sc second
21 #define pq priority_queue
22 #define pqb priority_queue <int, vector<int>, less<int> >
23 #define pqs priority_queue <int, vector<int>, greater<int> >
24 #define vec vector
25 ld eps=1e-9;
26 ll pp=1000000007;
27 ll mo(ll a,ll pp){if(a>=0 && a<pp)return a;a%=pp;if(a<0)a+=pp;return a;}
28 ll powmod(ll a,ll b,ll pp){ll ans=1;for(;b;b>>=1,a=mo(a*a,pp))if(b&1)ans=mo(ans*a,pp);return ans;}
29 void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
30 //void add(int x,int y,int z){ v[++e]=y; next[e]=head[x]; head[x]=e; cost[e]=z; }
31 int dx[5]={0,-1,1,0,0},dy[5]={0,0,0,-1,1};
32 ll read(){ ll ans=0; char last=' ',ch=getchar();
33 while(ch<'0' || ch>'9')last=ch,ch=getchar();
34 while(ch>='0' && ch<='9')ans=ans*10+ch-'0',ch=getchar();
35 if(last=='-')ans=-ans; return ans;
36 }
37 const int N=200005;
38 int f[N],flag[N],a[N],q[N],nu[N];
39 int main(){
40     int n=read(),nuu=0,h=0,che=0;
41     for (int i=1;i<=n;i++) a[i]=read(),f[a[i]]++;
42     for (int i=1;i<=n;i++)
43         if (f[i]==0){
44             q[++nuu]=i; ++che;
45         }
46     for (int i=1;i<=n;i++){
47         if (f[a[i]]>1){
48              if (flag[a[i]] || q[h+1]<a[i])
49                         {
50                             f[a[i]]--;
51                             a[i]=q[++h];
52                          } else flag[a[i]]=1;
53         } else flag[a[i]]=1;
54     }
55     printf("%d\n",che);
56     for (int i=1;i<=n;i++)
57         printf("%d ",a[i]);
58     return 0;
59 } 
View Code

E. Fire

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<cmath>
 6 #include<cstdlib>
 7 #include<vector>
 8 using namespace std;
 9 typedef long long ll;
10 typedef long double ld;
11 typedef pair<int,int> pr;
12 const double pi=acos(-1);
13 #define rep(i,a,n) for(int i=a;i<=n;i++)
14 #define per(i,n,a) for(int i=n;i>=a;i--)
15 #define Rep(i,u) for(int i=head[u];i;i=Next[i])
16 #define clr(a) memset(a,0,sizeof(a))
17 #define pb push_back
18 #define mp make_pair
19 #define fi first
20 #define sc second
21 #define pq priority_queue
22 #define pqb priority_queue <int, vector<int>, less<int> >
23 #define pqs priority_queue <int, vector<int>, greater<int> >
24 #define vec vector
25 ld eps=1e-9;
26 ll pp=1000000007;
27 ll mo(ll a,ll pp){if(a>=0 && a<pp)return a;a%=pp;if(a<0)a+=pp;return a;}
28 ll powmod(ll a,ll b,ll pp){ll ans=1;for(;b;b>>=1,a=mo(a*a,pp))if(b&1)ans=mo(ans*a,pp);return ans;}
29 void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
30 //void add(int x,int y,int z){ v[++e]=y; next[e]=head[x]; head[x]=e; cost[e]=z; }
31 int dx[5]={0,-1,1,0,0},dy[5]={0,0,0,-1,1};
32 ll read(){ ll ans=0; char last=' ',ch=getchar();
33 while(ch<'0' || ch>'9')last=ch,ch=getchar();
34 while(ch>='0' && ch<='9')ans=ans*10+ch-'0',ch=getchar();
35 if(last=='-')ans=-ans; return ans;
36 }
37 struct node{
38     int t,d,p,id;
39     friend bool operator <(node a,node b){
40         return a.d<b.d;
41     }
42 }f[200];
43 int dp[200][3000],a[200][3000],q[200],nu,t[200][3000];
44 void dfs(int i,int x){
45     if (i==0) return ;
46     if (a[i][x]) {
47         q[++nu]=f[i].id; dfs(i-1,t[i][x]);
48     }
49     else dfs(i-1,x);
50 }
51 int main(){
52     int n=read();
53     for (int i=1;i<=n;i++) f[i].t=read(),f[i].d=read(),f[i].p=read(),f[i].id=i,f[i].d--;
54     sort(f+1,f+n+1);
55     for (int i=1;i<=n;i++) {
56         for (int j=0;j<=f[i].d;j++)    {
57             if (j>=f[i].t && dp[i-1][j]<dp[i-1][j-f[i].t]+f[i].p) {
58                 dp[i][j]=dp[i-1][j-f[i].t]+f[i].p,a[i][j]=1; t[i][j]=j-f[i].t;
59             } else dp[i][j]=dp[i-1][j],a[i][j]=0,t[i][j]=j;
60             //if (i<6) cout<<i<<" "<<j<<" "<<dp[i][j]<<" "<<a[i][j]<<endl;
61         }
62         for (int j=f[i].d+1;j<=f[n].d;j++) dp[i][j]=dp[i][j-1],a[i][j]=a[i][j-1],t[i][j]=t[i][j-1];
63     } 
64     dfs(n,f[n].d);
65     printf("%d\n%d\n",dp[n][f[n].d],nu);
66     while (nu){
67         printf("%d ",q[nu]);
68         nu--;
69     }
70     return 0;
71 } 
View Code

F. Cities Excursions

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<cmath>
 6 #include<cstdlib>
 7 #include<vector>
 8 using namespace std;
 9 typedef long long ll;
10 typedef long double ld;
11 typedef pair<int,int> pr;
12 const double pi=acos(-1);
13 #define rep(i,a,n) for(int i=a;i<=n;i++)
14 #define per(i,n,a) for(int i=n;i>=a;i--)
15 #define Rep(i,u) for(int i=head[u];i;i=Next[i])
16 #define clr(a) memset(a,0,sizeof(a))
17 #define pb push_back
18 #define mp make_pair
19 #define fi first
20 #define sc second
21 #define pq priority_queue
22 #define pqb priority_queue <int, vector<int>, less<int> >
23 #define pqs priority_queue <int, vector<int>, greater<int> >
24 ld eps=1e-9;
25 ll pp=1000000007;
26 ll mo(ll a,ll pp){if(a>=0 && a<pp)return a;a%=pp;if(a<0)a+=pp;return a;}
27 ll powmod(ll a,ll b,ll pp){ll ans=1;for(;b;b>>=1,a=mo(a*a,pp))if(b&1)ans=mo(ans*a,pp);return ans;}
28 void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
29 //void add(int x,int y,int z){ v[++e]=y; next[e]=head[x]; head[x]=e; cost[e]=z; }
30 int dx[5]={0,-1,1,0,0},dy[5]={0,0,0,-1,1};
31 ll read(){ ll ans=0; char last=' ',ch=getchar();
32 while(ch<'0' || ch>'9')last=ch,ch=getchar();
33 while(ch>='0' && ch<='9')ans=ans*10+ch-'0',ch=getchar();
34 if(last=='-')ans=-ans; return ans;
35 }
36 const int N=400005;
37 struct node{
38     int a,c,i;
39 };
40 vector<node> vec[N];
41 int v[N],head[N],Next[N],q[N],ans[N],f[16][N],g[N],vis[N],e;
42 void add(int x,int y){ v[++e]=y; Next[e]=head[x]; head[x]=e; }
43 void dfs(int u){
44     vis[u]=1;
45     for (int i=head[u];i;i=Next[i]){
46         int v_=v[i];
47         if (g[v_]!=0) g[v_]=min(g[v_],u);
48         if (!vis[v_]) g[v_]=u,dfs(v_);
49     }
50 }
51 int main(){
52     int n=read(),m=read(),q=read();
53     for (int i=1;i<=m;i++){
54         int a=read(),b=read();
55         add(b,a);
56     }
57     for (int i=1;i<=q;i++){
58         int a=read(),b=read(),c=read();
59         vec[b].push_back((node){a,c,i});
60     }
61     for (int t=1;t<=n;t++){
62         int size=vec[t].size();
63         if (size==0) continue;
64         memset(g,0,sizeof(g));
65         memset(vis,0,sizeof(vis));
66         dfs(t);
67         for (int i=1;i<=n;i++) f[0][i]=g[i];
68         for (int i=1;i<=15;i++)
69             for (int j=1;j<=n;j++)
70                 f[i][j]=f[i-1][f[i-1][j]];
71         for (int i=0;i<size;i++){ 
72             if (!g[vec[t][i].a]||f[15][vec[t][i].a]) {
73                 ans[vec[t][i].i]=-1;
74                 continue;
75             }
76             int k=vec[t][i].c,p=0,no=vec[t][i].a; k--;
77             for (;k;k>>=1,p++)
78             if (k&1){
79                 no=f[p][no];
80             }
81             if (no==0) ans[vec[t][i].i]=-1;
82             else ans[vec[t][i].i]=no;
83         }
84     }
85     for (int i=1;i<=q;i++){
86         printf("%d\n",ans[i]);
87     }    
88     return 0;
89 } 
View Code

 

转载于:https://www.cnblogs.com/SXia/p/7600448.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值