B - Popular Vote
題意:
有n個商品要你挑選出前m個最多票的,且要滿足大於等於 tot/4*m(tot爲總票數)。
思路1:
- 降序排序,之後看第m個是否滿足題意。
思路2:
- 先求出總票數,之後算出條件 tot/4*m。
- 遍歷一遍,看看是否有滿足條件的存在,滿足就cnt++;
- 最後看cnt>=m是否成立。
反思
雖然題目沒明說,但其實是要向上取整。
AC(思路2)
#include <iostream>
#include <vector>
#define For(i,x,y) for(register int i=(x); i<=(y); i++)
using namespace std;
int main()
{
int n, m,sum,x;
vector<int>v;
cin>>n>>m;
sum=0;
For(i,1,n)
{
cin>>x;
sum+=x;
v.push_back(x);
}
sum=(sum+m*4-1)/(m*4);//注意要向上取整!!!!!
int cnt=0;
int len=v.size();
For(i,0,len-1)if(v[i]>=sum)cnt++;
if(cnt>=m)cout<<"Yes"<<endl;
else cout<<"No"<<endl;
return 0;
}
C - Replacing Integer
題意:
給你一個數,問你最終轉換成多少次可以使n最小。
思路:
- 對於當n>=k時,肯定不是最小,所以要先縮小到小於k。
- 縮小即是n%k。這裏設t=n%k。
- 對於縮小了的n,即t,繼續縮小,n=|t-k|=k-t。
- 可是再縮小呢 n=k-(k-t)=t,即變回t了。
- 這裏想起數電的, t 和 k-t 以模數 k 互補。
- 所以ans=min(t,k-t)。
反思
寫這題時,其實可以拿草稿紙出來推一下,
AC(官方)
#include <iostream>
#include <cmath>
#define For(i,x,y) for(register int i=(x); i<=(y); i++)
using namespace std;
typedef long long ll;
const ll INF =0x3f3f3f3f3f3f3f3f;
int main()
{
ll n, k,ans;
cin>>n>>k;
n=n%k;
ll temp=abs(n-k);
ans=min(temp,n);
cout<<ans<<endl;
return 0;
}
AC(博主contest時的)
#include <iostream>
#include <cmath>
#define For(i,x,y) for(register int i=(x); i<=(y); i++)
using namespace std;
typedef long long ll;
const ll INF =0x3f3f3f3f3f3f3f3f;
int main()
{
ll n, k,ans;
cin>>n>>k;
ans=n;
if(n>k)
{
n=n%k;
ans=min(ans,n);//想清楚,這裏餘數
}
//走到這裏肯定n比k小,所以再判定一下
ll temp=abs(n-k);
ans=min(temp,ans);
cout<<ans<<endl;
return 0;
}
D - Lunlun Number
題意:
數字要滿足每一位上的數和相鄰的數差距最多是1。
思路&&反思
- 起初我無從下手。
- 後面看了題解,其實可以用隊列去維護。
- 先把1-10入隊,之後第k次出隊就是答案了
哎哎哎,對於這道題完美的符合了先進先出的原則。
1->10/11/12
其中10->100/101.
當十位都出隊 保證 考慮完了10位,緊接着就是最小的百位。
AC(官方)
#include <iostream>
#include <cmath>
#include <queue>
#define For(i,x,y) for(register int i=(x); i<=(y); i++)
using namespace std;
typedef unsigned long long ull;
queue<ull>q;
int main()
{
int k;
For(i,1,9)q.push(i);
cin>>k;
ull temp,pre;
For(i,1,k)
{
pre=q.front();q.pop();
if(pre%10)//最後一位是10
{
temp=pre*10+(pre%10)-1;
q.push(temp);
}
temp=pre*10+pre%10;
q.push(temp);cnt++;
if(pre%10!=9)//最後一位是9,就沒有進位的可能了
{
temp=pre*10+pre%10+1;
q.push(temp);
}
}
cout<<pre<<endl;
return 0;
}