/*
Application of map
05/13 2019
*/
#include <iostream>
#include <map>
#include<string>
using namespace std;
struct Stu
{
string name;
int score;
};
Stu students[5] = { {"jack",89},
{ "tom",74 },{ "cindy",87 },{ "aisa",87 },{ "michale",98 } };
typedef map<string, int> MAP;
int main()
{
MAP mp;
for (int i = 0; i < 5; i++)
mp.insert(make_pair(students[i].name, students[i].score));
cout << mp["jack"] << endl;
mp["jack"] = 60;
for (MAP::iterator i = mp.begin(); i !=mp.end(); ++i)
{
cout << i->first << ',' << i->second << ' ';
}
cout << endl;
//Can't insert the same element in a set!
Stu newst;
newst.name = "jack";
newst.score = 99;
pair<MAP::iterator, bool> p =
mp.insert(make_pair(newst.name,newst.score));
if (p.second)
cout << "successful to insert!" << endl;
else
cout << "failed to insert!" << endl;
//insert and find element.
mp["harry"] = 78;
MAP::iterator q = mp.find("harry");
cout << q->first << "," << q->second << endl;
return 0;
}
/*
Application of set and map
Count the numbers of words
05/13 2019
*/
#include <iostream>
#include <map>
#include<set>
#include<string>
using namespace std;
struct Word
{
int times;
string content;
};
struct Rule
{
bool operator()(const Word &w1, const Word &w2)
{
if (w1.times != w2.times)
return w1.times > w2.times;
else
return w1.content < w2.content;
}
};
int main()
{
string worcon;
set<Word, Rule> word_set;
map<string, int> word_map;
//txt->word_map
freopen("f:\\freopen.txt", "r", stdin);
while (cin >> worcon)
{
++word_map[worcon];
}
//map->set
for (map<string, int>::iterator i = word_map.begin();
i != word_map.end(); ++i)
{
Word tmp;
tmp.content = i->first;
tmp.times = i->second;
word_set.insert(tmp);
}
//set->screen
for (set<Word, Rule>::iterator i = word_set.begin();
i != word_set.end(); ++i)
cout << i->content << " " << i->times << endl;
return 0;
}
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <cmath>
using namespace std;
//const double PI = 3.1415927; PI取3.1415926都不行,结果偏小, PI取3.1415927可以
const double PI=acos(-1.0);
const double eps = 1e-6;
int r[10010];
int N,F;
bool Valid(double V)
{
if( V < eps )
return true;
int total = 0;
for(int i = 0;i < N; ++i) {
double n = r[i]*r[i] / V;
total += n;
if( total >= F)
return true;
}
return false;
}
int main()
{
cin >> N >> F;
++F;
double maxV = 0;
for(int i = 0;i < N; ++i) {
cin >> r[i];
maxV = max(maxV,(double)r[i]*r[i]);
}
double L = 0,R = maxV;
while( R - L > eps ) {
double midV = L + (R-L )/2;
if( Valid(midV) )
L = midV;
else
R = midV;
}
cout << fixed << setprecision(3) << PI * L ;
return 0;
}
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <cmath>
using namespace std;
int N,M ;
int cost[100100];
bool Valid(int c)
{
int m = 1; //总月数
int curCost = 0; //本月花销
for(int i = 0;i < N; ++i) {
if(cost[i] > c )
return false;
if( curCost + cost[i] > c ) {
curCost = cost[i];
++m;
if( m > M )
return false;
}
else
curCost += cost[i];
}
return true;
}
int main()
{
cin >> N >> M;
int L = 1 << 30,R = 0;
for(int i = 0;i < N; ++i) {
cin >> cost[i];
L = min(L,cost[i]);
R += cost[i];
}
int lastValid = 0;
while( L <= R) {
int mid = L + (R-L)/2;
if(Valid(mid)) {
lastValid = mid;
R = mid - 1;
}
else
L = mid +1;
}
cout << lastValid ;
return 0;
}
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <cmath>
#include <cstdio>
using namespace std;
int N,C;
int x[100010];
bool Valid(int d)
{
int last = x[0];
int settled = 1;
for(int i = 1; i < N; ++i ) {
if( x[i] - last >= d ) {
++ settled ;
last = x[i];
if( settled >= C)
return true;
}
}
return false;
}
int main()
{
scanf("%d%d",&N,&C);
for(int i = 0;i < N; ++i)
scanf("%d",&x[i]);
sort(x,x+N);
int L = 1;
int R = x[N-1] / C + 1;
int lastValid = 0;
while(L <= R) {
int mid = L + (R-L)/2;
if (Valid(mid)) {
lastValid = mid;
L = mid + 1;
}
else
R = mid -1;
}
printf("%d", lastValid );
return 0;
}