昨晚D被隊友給HACK了,上分半路夭折…
A:
就是給你兩個數a,b
可以對a進行操作:加一個奇數,或者減一個偶數。
問最少操作多少次,使a變成b
直接看ab的差值b-a
差值=0,不需要操作了
差值>0,說明a需要加來達到b,判斷差值是不是奇數。
是·奇數,加一個奇數就可以了
是偶數,則需要加兩個奇數
差值<0,a需要減來變成b,同上
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while(t--)
{
long long a,b;
cin >> a >> b;
long long ans;
long long cha = b-a;
if(cha == 0)
ans = 0;
else if(cha > 0)
{
if(cha % 2 == 0)
{
ans = 2;
}
else
{
ans = 1;
}
}
else if(cha < 0)
{
if(cha % 2 == 0)
{
ans = 1;
}
else
{
ans = 2;
}
}
cout << ans << endl;
}
return 0;
}
B:意思就是給你一個待處理的數組a[],在給你一個操作數組p[]
p[]的意義在於你可以對p[]裏面的數當成a[]的下標進行交換操作
問能不能通過p[]來把a[]給排序。
簡單地暴力進行冒泡排序即可。
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 105;
int a[maxn],p[maxn];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while(t--)
{
int n,m;
cin >> n >> m;
for(int i = 0; i < n; i++)
{
cin >> a[i];
}
for(int i = 0; i < m; i++)
{
cin >> p[i];
}
sort(p,p+m);
for(int j = 0; j < n; j++)
{
for(int i = 0; i < m; i++)
{
if(a[p[i]-1] > a[p[i]])
{
int t = a[p[i]-1];
a[p[i]-1] = a[p[i]];
a[p[i]] = t;
}
}
}
int ans = 0;
for(int i = 0; i < n; i++)
{
if(a[i] > a[i+1] && i != n-1)
{
ans = 1;
break;
}
}
if(ans)
cout << "NO" << endl;
else
cout << "YES" << endl;
}
return 0;
}
C:
給你一個字符串,再給你一個數組。
你開始寫這個字符串,數組裏的數表示你會在這個數的字符處,重新寫這個字符串。
例如:abca, 1,3
我要寫字符串:a(在1處要重新寫)
abc(在3處要重新寫)
abca,最終把真個字符串完整寫完
問:寫完之後,要寫26個字母多少次。
建立26個前綴和即可。
#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
const int maxn = 2e5 + 50;
int p[maxn];
int he[26][maxn] = {0};
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while(t--)
{
long long n,m;
cin >> n >> m;
long long ans[30] = {0};
for(int i = 0; i < 26; i++)
{
for(int j = 0; j < n; j++)
{
he[i][j] = 0;
}
}
string s;
cin >> s;
for(int i = 0; i < m; i++)
{
cin >> p[i];
}
sort(p,p+m);
for(int i = 0; i < n; i++)
{
for(int j = 0; j < 26; j++)
{
if(j == s[i]-'a')
{
he[j][i] = he[j][i-1] + 1;
}
else
he[j][i] = he[j][i-1];
}
}
for(int i = 0; i < m; i++)
{
for(int j = 0; j < 26; j++)
{
ans[j] += he[j][p[i]-1];
}
}
for(int i = 0; i < n; i++)
{
ans[s[i]-'a']++;
}
for(int i = 0; i < 26; i++)
{
if(i != 25)
cout << ans[i] << " ";
else
cout << ans[i] << endl;
}
}
return 0;
}
D:魔鬼們,在我被HACK後,我用我被HACK的樣例HACK了6個,隊友幹掉20個。。。。
我們還幹掉了不少橙名,紫名的,太可怕了。
題目:3個數abc,可以操作一個數+1或者-1.
最終目標:b%a=0,c%b=0
暴力b,對於每一個b的可能取值,去計算b的操作數。
之後c需要是b的倍數,計算c的最少操作數(向上擴大,還是向下)
接着暴力a,範圍是1到根號b即可,需要b%a=0,計算a的操作數(a到當前暴力的a,還有a到b/a)
這道題最坑的就是數據範圍,1e4是一個關鍵點。
abc的範圍屬於1e4,但是經過操作之後,abc的大小可能會超過1e4,我就是暴力的1e4被HACK了。
樣例:
1
73 10000 10000
最後的結果bc應該是10001,10001.
#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while(t--)
{
long long a,b,c;
cin >> a >> b >> c;
long long ansa,ansb,ansc;
long long ans = 3e4 + 500;
int x,y,z;
for(y = 1; y <= 2e4; y++)
{
long long bcha = abs(b-y);
long long ccha = 0;
if(abs(c-c/y*y) < abs(c-(c/y+1)*y)) z = c/y*y;
else z = (c/y+1)*y;
ccha = abs(z-c);
long long acha = 1e4;
for(x = 1; x*x <= y; x++)
{
if(y % x == 0)
{
if(abs(a-x) < abs(a-y/x))
{
acha = abs(a-x);
if(acha+bcha+ccha < ans)
{
ans = acha+bcha+ccha;
ansa = x;
ansb = y;
ansc = z;
}
}
else
{
acha = abs(a-y/x);
if(acha+bcha+ccha < ans)
{
ans = acha+bcha+ccha;
ansa = y/x;
ansb = y;
ansc = z;
}
}
}
}
}
cout << ans << endl;
cout << ansa << " " << ansb << " " << ansc << endl;
}
return 0;
}