阿里笔试3.30
太久没做题,对题目的敏感度都没了,两题就做了一个题,哭了。
这是第一题的代码:
#include<stdio.h> #include<string.h> #include<string> #include<algorithm> #include<queue> #include<stack> #include<map> #include<set> #include<functional> #include<vector> #include<iostream> #include<time.h> #include<math.h> using namespace std; //------------------------------------------------------- #define LL long long //#define LL __int64 template<typename T>T Max(T a, T b){ return a>b ? a : b; } template<typename T>T Min(T a, T b){ return a<b ? a : b; } template<typename T>void Swap(T &a, T &b){ T c = a; a = b; b = c; } template<typename T>T Abs(T a){ return a >= 0 ? a : -a; } #define clr_all(a,b) memset(a,b,sizeof(a)) #define clr(a,b,n) memset(a,b,sizeof(a[0])*n) const int MAXN = 100000 + 5; const int MAXM = 500 + 5; const int mod = 1000000007; const int INF = 0x7FFFFFFF; //------------------------------------------------------- LL N, M, K; priority_queue<LL> q; int main(){ scanf("%lld%lld%lld", &N, &M, &K); for (LL i = 0; i < N; i++){ LL tmp; scanf("%lld", &tmp); q.push(tmp); } //printf("%d\n", q.top()); for (LL i = 1; i <= M; i++){ LL tmp = q.top(); q.pop(); tmp = tmp - i*K; if (tmp % 2 != 0) tmp -= 1; tmp /= 2; q.push(tmp); } LL ans = 0; while (!q.empty()){ ans += q.top(); q.pop(); } ans += M*K*N; printf("%lld\n", ans); return 0; }思路大概就是,每次每个厂都增加K,这个K让他保持不动,然后每次取出最大数的时候,将本来应该减少的K/2,从最大数上减去,这样就可以始终保持所有数的累计增加是K的倍数,为了防止超时,可以用优先队列来做。