codeforces 792E —— Colored Balls (貪心,不定方程,數學)
There are n boxes with colored balls on the table. Colors are numbered from 1 to n. i-th box contains ai balls, all of which have color i. You have to write a program that will divide all balls into sets such that:
- each ball belongs to exactly one of the sets,
- there are no empty sets,
- there is no set containing two (or more) balls of different colors (each set contains only balls of one color),
- there are no two sets such that the difference between their sizes is greater than 1.
Print the minimum possible number of sets.
InputThe first line contains one integer number n (1 ≤ n ≤ 500).
The second line contains n integer numbers a1, a2, ... , an (1 ≤ ai ≤ 109).
OutputPrint one integer number — the minimum possible number of sets.
3 4 7 8output
5input
2 2 7output
4Note
In the first example the balls can be divided into sets like that: one set with 4 balls of the first color, two sets with 3 and 4 balls, respectively, of the second color, and two sets with 4 balls of the third color.
看到了一份能看懂的程式碼,先貼上來。再說我自己的想法。
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#include <iostream>
using namespace std;
const int MAXN = 500 +10;
const long long INF=0x7fffffffffffffff;
long long ans=INF;
long long a[MAXN];
int n;
void solve(long long x){
long long s=0;
if(x==0)
return;
for(int i=0;i<n;i++){
long long p=a[i]/x;
long long q=a[i]%x;
if(p==0)
return;
if(p>=q){
s+=(a[i]+x)/(x+1);
}else
return;
}
ans=min(ans,s);
}
int main(){
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%I64d",a+i);
}
sort(a,a+n);
for(int i=1;i*i<=a[0];i++){
solve(a[0]/i);
solve(a[0]/i+1);
solve(i);
solve(a[0]/i-1);
}
printf("%I64d\n",ans);
}
其實就是暴力,因為每一份的個數肯定不能超過最小的數,所以針對最小的數列舉一下。不過在這個程式碼裡面那個a[i]+x/(x+1)不太懂
我是用不定方程做的。
對於ax+by=n,x=x'-bt,y=y'+at.x',y'為一組特解。
對於p=a[I]/x,q=a[I]%x.
只要p>=q,就能把q分成q個1加到其他集合裡面。(p-q)*x+q*(x+1)=a[i],總個數就是p+t,求出t的範圍即可。
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#include <iostream>
using namespace std;
const int MAXN = 500 +10;
const long long INF=0x7fffffffffffffff;
long long ans=INF;
long long a[MAXN];
int n;
void solve(long long x){
long long s=0;
if(x==0)
return;
for(int i=0;i<n;i++){
long long p=a[i]/x;
long long q=a[i]%x;
if(p<q){
return;
}
long long X=(a[i]-q)/x;
long long Y=(p-q)/(x+1);
long long temp=min(X,Y);
s+=p-temp;
}
ans=min(ans,s);
}
int main(){
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%I64d",a+i);
}
sort(a,a+n);
for(int i=1;i*i<=a[0];i++){
solve(a[0]/i);
solve(a[0]/i+1);
solve(i);
solve(a[0]/i-1);
}
printf("%I64d\n",ans);
}
然而居然被卡常樂。。
氣哭
相關推薦
codeforces 792E —— Colored Balls (貪心,不定方程,數學)
E. Colored Balls time limit per test 1 second memory limit per test 256 megabytes input standard input output standard out
Codeforces-792E Colored Balls(貪心/數學)
E. Colored Balls time limit per test 1 second memory limit per test 256 megabytes input stan
Codeforces 911F Tree Destruction(貪心 && 樹的直徑)
span aps con lan dfs delta scanf auto ems 題目鏈接 Tree Destructi 題意 給定一棵樹,每次可以選定樹上的兩個葉子,並刪去其中的一個。答案每次加上兩個選定的葉子之間的距離。 求最後答案的最大值。 首先
(CF 792E Colored Balls) 思維題 貪心
E. Colored Balls time limit per test: 1 second memory limit per test: 256 megabytes input: standard input output: standar
Codeforces 196C Paint Tree(貪心+極角排序)
cpp namespace paint 進行 滿足 sin its force 一個個 題目鏈接 Paint Tree 給你一棵n個點的樹和n個直角坐標系上的點,現在要把樹上的n個點映射到直角坐標系的n個點中,要求是除了在頂點處不能有線段的相交。 我們先選一個在直角坐標
ZOJ ~ 3593 ~ One Person Game (擴充套件歐幾里得,不定方程)
題意 你要從A走到B,你每次可以走a步,b步,a+b步問最小需要走多少步?無法到達輸出 -1。 題解 先不考慮a+b步的情況,那麼我們要求解的就是:,如果,證明無解。 假設原方程一組解為x0,y0,那麼通解(x,y)為:,。 其實也就是兩條直線:, 取一條平行於
CodeForces-1040B Shashlik Cooking(貪心)
題目大意: n個節點,選擇一個節點的時候翻轉這個節點與兩側k個節點(夠的話),求最少幾次全部翻到反面。 ps:同一個節點翻兩次會翻回正面。 思路: 貪心,儘量多的選能翻翻2k+1個節點的節點,即在中間儘量多的劃分長度為2k+1的區間。為使中間節點數恰好為(2k+1)的
Codeforces 337C:Quiz(貪心+規律+快速冪)
Note Sample 1. Manao answered 3 questions out of 5, and his score would double for each two consecutive correct answers. If Manao had answered the first
poj 1456 Supermarket(貪心+並查集,貪心+優先佇列)
Supermarket Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7677 Accepted: 3252 Description A supermarket has a set Prod o
codeforces 913C Party Lemonade (貪心 + DFS)
題目大意: 有 n 種檸檬汁,第 i 種每瓶的容量為 2^(i-1) 升,價格為 Ci,問至少買 L 升檸檬汁最少花多少錢? 思路: 一看題,第一感覺是 DP,但是由於購買數可以超過 L 升,並
codeforces 700B Connecting Universities (貪心詳解)
codeforces 700B Connecting Universities 結論思路: 這個題一眼看上去很難,但是正著做不行,我們換個角度:考慮每條邊的貢獻。 因為是一棵樹,所以一條邊把樹分成兩個集合,假如左邊有x個學校,右邊有y個學校。 貪心地想,
POJ 3687:Labeling Balls(優先隊列+拓撲排序)
avi sat 一個 排序 com script memory std from id=3687">Labeling Balls Time Limit: 1000MS Memory Limit: 65536K T
CodeForces 659E New Reform (圖的遍歷判環)
max pri output cond mes ret middle height block Description Berland has n cities connected by m bidirectional roads. No road
Wannafly挑戰賽26-B 冥土追魂(貪心?思維?模擬?)
轉載出自 https://blog.csdn.net/c_13579/article/details/83039005 貪心不行,數學思維尋找它的選取規律 #include<iostream> #include<cstdio> #include<a
HDU 3635 Dragon Balls(並查集:路徑壓縮)
Problem Description Five hundred years later, the number of dragon balls will increase unexpectedly, so it's too difficult for Monkey King(WuKong) t
最小生成樹kruskal演算法(貪心+並查集+堆優化)
kruskal演算法克魯斯卡爾演算法的基本思想是以邊為主導地位,始終選擇當前可用(所選的邊不能構成迴路)的最小權植邊。所以Kruskal演算法的第一步是給所有的邊按照從小到大的順序排序。這一步可以直接使用庫函式qsort或者sort。接下來從小到大依次考察每一條邊(u,v)。
C. Line(擴充套件歐幾里得求不定方程的解)
time limit per test 1 second memory limit per test 256 megabytes input standard input outp
51 nod 砝碼稱重(貪心+進位制轉換思想)
現在有好多種砝碼,他們的重量是 w0,w1,w2,...w0,w1,w2,... 每種各一個。問用這些砝碼能不能表示一個重量為m的東西。 樣例解釋:可以將重物和3放到一個托盤中,9和1放到另
Codeforces 852D Exploration plan(最短路+二分+二分圖匹配)
The competitors of Bubble Cup X gathered after the competition and discussed what is the best way to get to know the host country and its cities. After
CodeForces 631E Product Sum(斜率優化DP+二分|三分) ★
題意:給出n個數,現在可以移動一個數的位置,現在要使和sigma(ai*i)最大,詢問這個最大和。 思路:將一個數向左移動和向右移動是一樣的,現在考慮向左移動。 先預處理出字首和,將一個數向左移動後,那麼改變數為sum[r-1]-sum[l-1]+a[r]*(r-l),考慮