Codeforces Round #503 (by SIS, Div. 2)

Codeforces Round #503 (by SIS, Div. 2)

https://codeforces.com/contest/1020

A

 1 #include <iostream>
 2 #include <cstring>
 3 #include <cmath>
 4 #include <algorithm>
 5 #include <cstdio>
 6 #include <cstdlib>
 7 #include <string.h>
 8 using namespace std;
 9 int n,h,a,b,k;
10 int main()
11 {
12     scanf("%d%d%d%d%d",&n,&h,&a,&b,&k);
13     while(k--)
14     {
15         int ta,fa,tb,fb;long long ans=0;
16         scanf("%d%d%d%d",&ta,&fa,&tb,&fb);
17         if(ta==tb)
18             ans+=abs(fb-fa);
19         if(ta!=tb)
20         {
21             ans+=abs(tb-ta);
22             if(fa>=a and fa<=b)
23             {
24                 ans+=abs(fb-fa);
25             }
26             else
27             {
28                 if(abs(fa-a)<abs(fa-b))
29                 {
30                     ans+=abs(fa-a);
31                     ans+=abs(a-fb);
32                 }
33                 else
34                 {
35                     ans+=abs(fa-b);
36                     ans+=abs(b-fb);
37                 }
38             }
39         }
40         cout<<ans<<endl;
41     }
42     return 0;
43 }

B

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<string>
 6 #include<queue>
 7 #include<cmath>
 8 using namespace std;
 9
10 int a[1005];
11 bool book[1005];
12
13 int main(){
14
15     int n;
16     cin>>n;
17     memset(book,0,sizeof(book));
18     for(int i=1;i<=n;i++){
19         cin>>a[i];
20     }
21     for(int i=1;i<=n;i++){
22         memset(book,0,sizeof(book));
23         int j=i;
24         while(!book[j]){
25             book[j]=true;
26             j=a[j];
27         }
28         cout<<j<<" ";
29     }
30     cout<<endl;
31
32 }

C

贪心

题意:给定n和m,分别为选民和政党的数量,你事先知道每一个选民将要挑选的政党,但是你可以花钱收买他,求你最少花费多少钱可以赢得选举

思路:从小到大枚举自己最终的选票,对于选票比自己多的政党,就从小到大收买,直到比自己小为止,全部买完还没达到自己所枚举的票数,就从小到大收买其他人的

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<string>
 6 #include<cmath>
 7 #include<queue>
 8 #include<map>
 9 #include<vector>
10 using namespace std;
11
12 struct sair{
13     int pos;
14     long long b;
15 }a[3005];
16 int vis[3005];
17 int num[3005];
18
19 bool cmp(sair a,sair b){
20     return a.b<b.b;
21 }
22
23 int main(){
24     std::ios::sync_with_stdio(false);
25     int n,m;
26     cin>>n>>m;
27     for(int i=1;i<=n;i++){
28         cin>>a[i].pos>>a[i].b;
29     }
30     long long ans=0x3f3f3f3f3f3f3f3f;
31     sort(a+1,a+n+1,cmp);
32     for(int i=0;i<=n;i++){
33         long long sum=0;
34         memset(vis,0,sizeof(vis));
35         memset(num,0,sizeof(num));
36         for(int j=n;j>=1;j--){
37             if(num[a[j].pos]<i||a[j].pos==1){
38                 num[a[j].pos]++;
39             }
40             else{
41                 sum+=a[j].b;
42                 vis[j]=1;
43                 num[1]++;
44             }
45         }
46         for(int j=1;j<=n;j++){
47             if(num[1]<=i&&a[j].pos!=1&&!vis[j]){
48                 sum+=a[j].b;
49                 num[1]++;
50             }
51         }
52         if(num[1]>i)
53             ans=min(ans,sum);
54     }
55     cout<<ans<<endl;
56
57 }

D

交互题(感觉这类都是二分题?)

题意:n个人围成一圈(n为偶数),每个人手上有一个数字,相邻的两个人相差1或-1。问是否存在一个人与他对面的人手上的数字相同

思路:模拟了半天才发现,当n%4==2时,无解,n%4==0时,设i位置与对面的差值为a[i],可以发现a[i]和a[(mid+n/2-1)%n+1]要么是相反数要么为0,当a[i]和a[(mid+n/2-1)%n+1]是相反数时,它们之间一定存在a[i]=0,那就是答案

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define lson l,mid,rt<<1
 4 #define rson mid+1,r,rt<<1|1
 5 #define sqr(x) ((x)*(x))
 6 #define pb push_back
 7 #define eb emplace_back
 8 #define maxn 1005
 9 #define eps 1e-8
10 #define pi acos(-1.0)
11 #define rep(k,i,j) for(int k=i;k<j;k++)
12 typedef long long ll;
13 typedef pair<int,int> pii;
14 typedef pair<double,double>pdd;
15 typedef pair<int,char> pic;
16 typedef pair<pair<int,string>,pii> ppp;
17 typedef unsigned long long ull;
18 const long long MOD=998244353;
19 const double oula=0.57721566490153286060651209;
20 using namespace std;
21
22 int n;
23 vector<int>ve;
24 int x;
25
26 int query(int x){
27     cout<<"? "<<x<<endl;
28     cin>>x;
29     return x;
30 }
31
32 int main(){
33     std::ios::sync_with_stdio(false);
34     cin>>n;
35     if(n%4==0){
36         int L=1,R=n/2,mid;
37         int tmp1=query(1);
38         int tmp2=query(1+n/2);
39         if(tmp1==tmp2){
40             cout<<"! "<<1<<endl;
41             return 0;
42         }
43         int flag1=tmp1>tmp2?1:-1;
44         int flag2=-flag1;
45         while(L<=R){
46             mid=L+R>>1;
47             tmp1=query(mid);
48             tmp2=query((mid+n/2-1)%n+1);
49             if(tmp1==tmp2){
50                 cout<<"! "<<mid<<endl;
51                 return 0;
52             }
53             if(flag1<0){
54                 if(tmp1<tmp2) L=mid+1;
55                 else R=mid-1;
56             }
57             else{
58                 if(tmp1<tmp2) R=mid-1;
59                 else L=mid+1;
60             }
61         }
62         tmp1=query(R);
63         tmp2=query((R+n/2-1)%n+1);
64         if(tmp1==tmp2){
65             cout<<"! "<<R<<endl;
66         }
67         tmp1=query(L);
68         tmp2=query((L+n/2-1)%n+1);
69         if(tmp1==tmp2){
70             cout<<"! "<<L<<endl;
71         }
72     }
73     else{
74         cout<<"! -1"<<endl;
75         return 0;
76     }
77
78 }

E

题意:有向图中找到一个点集,使得点集内的点满足dist(x,y)>=2,存在一个点集内的点到点集外的点dist(x,y)<=2

思路:遍历每个点,把他相邻的点删去。然后再判断留下来的点是否相邻(看到这题的第一个想法,但是正确性不会证明。。。)

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define lson l,mid,rt<<1
 4 #define rson mid+1,r,rt<<1|1
 5 #define sqr(x) ((x)*(x))
 6 #define pb push_back
 7 #define eb emplace_back
 8 #define maxn 1005
 9 #define eps 1e-8
10 #define pi acos(-1.0)
11 #define rep(k,i,j) for(int k=i;k<j;k++)
12 typedef long long ll;
13 typedef pair<int,int> pii;
14 typedef pair<double,double>pdd;
15 typedef pair<int,char> pic;
16 typedef pair<pair<int,string>,pii> ppp;
17 typedef unsigned long long ull;
18 const long long MOD=998244353;
19 const double oula=0.57721566490153286060651209;
20 using namespace std;
21
22 vector<int>ve[1000005];
23 bool book[1000005];
24 bool used[1000005];
25
26 int main(){
27     std::ios::sync_with_stdio(false);
28     int n,m;
29     cin>>n>>m;
30     int u,v;
31     for(int i=1;i<=m;i++){
32         cin>>u>>v;
33         ve[u].pb(v);
34     }
35     for(int i=1;i<=n;i++){
36         if(!book[i]){
37             book[i]=1;
38             used[i]=1;
39             for(int j=0;j<ve[i].size();j++){
40                 book[ve[i][j]]=1;
41             }
42         }
43     }
44     int ans=0;
45     for(int i=n;i;i--){
46         if(used[i]){
47             ans++;
48             for(int j=0;j<ve[i].size();j++){
49                 used[ve[i][j]]=0;
50             }
51         }
52     }
53     cout<<ans<<endl;
54     for(int i=1;i<=n;i++){
55         if(used[i]){
56             cout<<i<<" ";
57         }
58     }
59 }

原文地址:https://www.cnblogs.com/Fighting-sh/p/10581985.html

时间: 2024-08-03 23:46:56

Codeforces Round #503 (by SIS, Div. 2)的相关文章

Codeforces Round #503 (by SIS, Div. 2) Partial Solution

从这里开始 题目列表 瞎扯 Problem A New Building for SIS Problem B Badge Problem C Elections Problem D The hat Problem E Sergey's problem 瞎扯 例行快速切A.B.C. 然后发现D是交互.E也不像是我能做的题,感觉完蛋了. 最后8分钟想出D.狂码代码,然后比赛结束后1分钟过样例. 第二天早上再花4分钟AC.我真是个大菜逼.. 于是这场cf比赛变成了真·手速场.几个friends手速比我

Codeforces Round #503 (by SIS, Div. 2) C. Elections

气死我了人生中第一次打cf就掉分了 A题大水题浪费太多时间囧明明都是A两题亮老师还上分了.. 表示C题打的时候就想到正解啊(而且还更加优秀,因为家里老爷机暴力跑的超龟以为不行 其实是没认真算复杂度),虽然不会证三分性,但是最后还是AC了,暴力1000ms+ 三分40ms+ 看着就很奇淫的题,猛然脑海里就想到二分政党的最后得到的选票,然后发现没有单调性,但好像满足三分 然后每次就贪心,for一遍把那些比1政党本来就有的选票+要多拿的选票还要多的拿到比这个值少1 假如还不够要多拿的,那就在剩下的拿最

Codeforces Round #503 (by SIS, Div. 2) D. The hat -交互题,二分

cf1020D 题意: 交互题目,在有限的询问中找到一个x,使得数列中的第x位和第(x+n/2)位的值大小相同.数列保证相邻的两个差值为1或-1: 思路: 构造函数f(x) = a[x] - a[x + n/2] ,由于a数列差值为1或-1,所以可以发现f(x)是连续的.然后就可以用二分了,这种二分的check方式是自己第一次见的.就是通过f(mid)和f(d)的正负来判断区间的移动.其中d是任选的. 代码: #include <iostream> #include <cstdio>

Codeforces Round #503 (by SIS, Div. 1)第四题 the hat

原题链接:B. The hat 题意:有n(偶数)个人围成一个圈,每个人身上有一个数字,保证相邻两个人的数字差为1, 现在要把第i个人和第i+n/2个人面对面站着,例如现在有8个人,站好后如下: 1 2 1 2 3 4 3 2 第1个人和第5个人面对面,第2个人和第6个人面对面,以此类推... 现在的问题是你可以询问q(q<=60)次,在n(2<=n<=100000)个人中 找出一对面对面站着且数字相同的人,输出这两个人任意一个人的位置,没找到输出-1: 首先当n%4!=0的时候我们直接

Codeforces Round #503 (by SIS, Div. 2) D. The hat

有图可以直观发现,如果一开始的pair(1,1+n/2)和pair(x, x+n/2)大小关系不同 那么中间必然存在一个答案 简单总结就是大小关系不同,中间就有答案 所以就可以使用二分 #include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <climits> #include <cstring> #include <s

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 36 (Rated for Div. 2) 题解

Educational Codeforces Round 36 (Rated for Div. 2) 题目的质量很不错(不看题解做不出来,笑 Codeforces 920C 题意 给定一个\(1\)到\(n\)组成的数组,只可以交换某些相邻的位置,问是否可以将数组调整为升序的 解题思路 首先如果每个数都能通过交换到它应该到的位置,那么就可以调整为升序的. 但实际上交换是对称的,如果应该在的位置在当前位置前方的数都交换完成,那么整体就是排好序的,因为不可能所有不在相应位置的数都在相应位置的后方.

Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes

Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes 题目连接: http://codeforces.com/contest/985/problem/E Description Mishka received a gift of multicolored pencils for his birthday! Unfortunately he lives in a monochrome w

Educational Codeforces Round 55 (Rated for Div. 2)

Educational Codeforces Round 55 (Rated for Div. 2) 链接 A Vasya and Book 傻逼题..注意判边界. #include<cstdio> #include<cstring> #include<algorithm> #include<queue> #include<set> #include<map> #include<vector> #include<cm