1. 程式人生 > 實用技巧 >Codeforces Round #665 (Div. 2) Distance and Axis、

Codeforces Round #665 (Div. 2) Distance and Axis、

題目連結:Distance and Axis

題意:在ox軸上,給出點A的橫座標x,你可以向左或右移動點A(x+1/x-1),問你最小移動A的次數,以使得可以在ox軸上找到B點位置,B點滿足從O到B的距離與從A到B的距離之間的絕對差等於k。

題解:

先特判下:

if(k==0)
        {
            if(n%2)
                printf("1\n");
            else printf("0\n");
        }
        else if(k>=n)
        {
            printf("%d\n
",k-n); }

對於A點座標x,最終要移動到哪裡,x滿足x=2*min(OB,AB)+k

那麼列舉OB長度就行,找到距離x點最近那個點就行

為什麼不列舉AB,因為列舉它們兩個誰,程式碼都一樣

程式碼:

 1 #include<stack>
 2 #include<queue>
 3 #include<map>
 4 #include<cstdio>
 5 #include<cstring>
 6 #include<iostream>
 7 #include<algorithm>
 8
#include<vector> 9 #define fi first 10 #define se second 11 #define pb push_back 12 using namespace std; 13 typedef long long ll; 14 const int maxn=2000+10; 15 const int mod=1e9+7; 16 const double eps=1e-8; 17 const int INF = 0x3f3f3f3f; 18 int main() 19 { 20 int t; 21 scanf("%d",&t); 22
while(t--) 23 { 24 int n,k; 25 scanf("%d%d",&n,&k); 26 if(k==0) 27 { 28 if(n%2) 29 printf("1\n"); 30 else printf("0\n"); 31 } 32 else if(k>=n) 33 { 34 printf("%d\n",k-n); 35 } 36 else 37 { //k+2min(OB,AB) 38 int l=0,r=n,mid,minn=INF; 39 while(l<=r) 40 { 41 mid=(l+r)>>1; 42 int ans=mid*2+k; 43 if(ans>n) 44 { 45 r=mid-1; 46 minn=min(minn,ans-n); 47 } 48 else 49 { 50 l=mid+1; 51 minn=min(minn,n-ans); 52 } 53 } 54 printf("%d\n",minn); 55 } 56 } 57 return 0; 58 }
View Code

題目連結:Ternary Sequence

題意:

你有兩個序列a,b。序列是由0,1,2構成。給你x1,y1,z1表示a序列中0,1,2的數量,給你x2,y2,z2表示b序列中0,1,2的數量,

如果你構造的a,b序列中,ai==bi,那麼結果+0,如果ai>bi,結果加ai*bi。如果ai<bi,結果減ai*bi

讓你輸出最大結果

題解:

結果想盡可能大,那就先讓z1的2和y2的1結合,那麼結果這個時候為2*min(z1,y2)

然後如果結果變小,肯定是z2的2和y1的1結合了,那麼我們先讓z2和x1結合,再和進行完上面操作的z2結合,最後結果減去剩餘的z2*2

程式碼:

 1 #include<stack>
 2 #include<queue>
 3 #include<map>
 4 #include<cstdio>
 5 #include<cstring>
 6 #include<iostream>
 7 #include<algorithm>
 8 #include<vector>
 9 #define fi first
10 #define se second
11 #define pb push_back
12 using namespace std;
13 typedef long long ll;
14 const int maxn=2000+10;
15 const int mod=1e9+7;
16 const double eps=1e-8;
17 const int INF = 0x3f3f3f3f;
18 int main()
19 {
20     int t;
21     scanf("%d",&t);
22     while(t--)
23     {
24         int x1,x2,y1,y2,z1,z2;
25         scanf("%d%d%d",&x1,&y1,&z1);
26         scanf("%d%d%d",&x2,&y2,&z2);
27         int sum=0,ans=min(z1,y2);
28         z1-=ans;
29         y2-=ans;
30         sum=sum+ans*2;
31 
32         ans=min(x1,z2);
33         x1-=ans;
34         z2-=ans;
35 
36         if(z2)
37         {
38             ans=min(z1,z2);
39             z1-=ans;
40             z2-=ans;
41             if(z2)
42             {
43                 sum=sum-z2*2;
44             }
45         }
46         printf("%d\n",sum);
47     }
48     return 0;
49 }
View Code

題目連結:Mere Array

題意:

給你一個序列,如果gcd(ai,aj)==序列中的最小值,那麼ai和aj可以交換,問你最後能不能把原序列變成一個非遞減序列

題解:

我們設序列最小值為minn,我們把可以被minn整除的數找出來,那麼剩下數的位置是不能改變的

這個時候我們對原序列sort從小到大排序,判斷一下我們要構成的最終序列中,那麼不能被minn整除的數的位置改變了沒有

如果改變了,那就輸出NO,否則YES

畢竟對於可以被minn整除的數來說,它們的位置總是可以通過minn來達到間接互換

程式碼:

 1 #include<stack>
 2 #include<queue>
 3 #include<map>
 4 #include<cstdio>
 5 #include<cstring>
 6 #include<iostream>
 7 #include<algorithm>
 8 #include<vector>
 9 #define fi first
10 #define se second
11 #define pb push_back
12 using namespace std;
13 typedef long long ll;
14 const int maxn=1e5+10;
15 const int mod=1e9+7;
16 const double eps=1e-8;
17 const int INF = 0x3f3f3f3f;
18 ll v[maxn],w[maxn];
19 int main()
20 {
21     ll t;
22     scanf("%lld",&t);
23     while(t--)
24     {
25         ll n,minn=INF,flag=0;
26         scanf("%lld",&n);
27         for(ll i=1;i<=n;++i)
28             scanf("%lld",&v[i]),w[i]=v[i],minn=min(minn,v[i]);
29         sort(v+1,v+1+n);
30         ll last=0;
31         for(ll i=1;i<=n;++i)
32         {
33             if(v[i]%minn==0) continue;
34             if(v[i]%minn && v[i]==w[i] && last<=w[i]) last=w[i];
35             else
36             {
37                 flag=1;
38                 break;
39             }
40         }
41         if(flag) printf("NO\n");
42         else printf("YES\n");
43     }
44     return 0;
45 }
View Code