(1)问题描述:Count the number of non-decreasing subarrays of the given array A[].
(2)要点:如果A[L] <= A[L+1] <= .. <= A[R],则非降子数组个数为(R-L+1)*(R-L+2)/2
(3)代码:
#include <stdio.h>
#include <vector>
using std::vector;
int main()
{
unsigned int nCases = 0;scanf("%d",&nCases);
for(unsigned int iCases = 1;iCases <= nCases;++iCases)
{
unsigned int n = 0;scanf("%d",&n);
vector<unsigned int> data(n,0);
for(unsigned int i = 0;i < n;++i) scanf("%d",&data[i]);
unsigned long long ans = 0;
for(unsigned int i = 0;i < n;)
{
unsigned int from = i;
for(++i;i < n && data[i] >= data[i-1];++i);
unsigned int len = i - from;
unsigned long long count = len;
count *= (len + 1);count /= 2;
ans += count;
}
printf("%llu\n",ans);
//printf("%I64u\n",ans);
}
return 0;
}
(1)问题描述:
(2)要点:
(3)代码:
#include <stdio.h>
#include <vector>
#include <algorithm>
using std::vector;
int main()
{
static const size_t buff_size = 1001;
unsigned int nCases = 0;scanf("%d",&nCases);
for(unsigned int iCases = 1;iCases <= nCases;++iCases)
{
char ans_buff[buff_size+1] = { 0 },chef_buff[buff_size+1] = { 0 };
unsigned int n = 0;scanf("%d%s%s",&n,ans_buff,chef_buff);
unsigned int correct = 0;
for(unsigned int i = 0;i < n;++i) correct += (ans_buff[i] == chef_buff[i]);
vector<unsigned int> data(n+1,0);
for(unsigned int i = 0;i <= n;++i) scanf("%d",&data[i]);
unsigned int ans = 0;
for(unsigned int i = 0;i <= correct;++i)
{
if(data[i] > ans) ans = data[i];
}
if(correct == n) ans = data[n]; //
printf("%u\n",ans);
}
return 0;
}
(1)问题描述:Find all times in the HH:MM format, such that the absolute value of the angle between hours' and minutes' hands is equal to the given value.(误差要求不超过1/120)
(2)要点:预计算每个时间点的角度,并且可以明确知道每个角度乘以2必然是整数。
(3)代码:
#include <stdio.h>
#include <vector>
#include <string.h>
#include <string>
#include <algorithm>
using std::vector;
using std::string;
#include <math.h>
double round(double v) { return (v>0.0)?floor(v+0.5):ceil(v-0.5); }
#include <assert.h>
int main()
{
static const double hourDegPerHour = 30;
static const double hourDegPerMin = 0.5;
static const double minDegPerMin = 6;
static const unsigned int mindegree = 0;
static const unsigned int maxdegree = 180;
static const unsigned int minh = 0;
static const unsigned int maxh = 11;
static const unsigned int minm = 0;
static const unsigned int maxm = 59;
static const unsigned int trans = 2;
static const unsigned int coef = 120;
string ansstr[maxdegree*trans+1];
for(unsigned int ih = minh;ih <= maxh;++ih)
{
for(unsigned int im = minm;im <= maxm;++im)
{
double dh = hourDegPerHour*ih + hourDegPerMin*im;
double dm = minDegPerMin*im;
double dis = dh - dm;
if(dis < 0) dis = 0 - dis;
if(dis > 180) dis = 360 - dis;
unsigned int d = (unsigned int)(round(dis*trans));
assert(d >= 0 && d <= 360);
char buff[0x100] = { 0 };
sprintf(buff,"%02d:%02d\n",ih,im);
ansstr[d] += buff;
}
}
unsigned int nCases = 0;scanf("%d",&nCases);
for(unsigned int iCases = 1;iCases <= nCases;++iCases)
{
float v = 0;scanf("%f",&v);
v *= trans;
unsigned int iv = (unsigned int)(round(v));
double delta = v - iv;
if(delta < 0) delta = 0 - delta;
delta *= coef;
if(delta < trans) printf("%s",ansstr[iv].c_str());
}
return 0;
}
(1)问题描述:
(2)要点:动态规划,参考代码中的状态迁移方程。
(3)代码:
#include <stdio.h>
#include <vector>
#include <algorithm>
using std::vector;
int main()
{
static const unsigned int module = 1000000007;
//
unsigned int n = 0,m = 0,c = 0,v = 0;scanf("%d%d%d",&n,&m,&c);
vector<unsigned long long> ucounts(c+1,0),lcounts(c+1,0);
for(unsigned int i = 0;i < n;++i) { scanf("%d",&v);++ucounts[v]; }
for(unsigned int i = 0;i < m;++i) { scanf("%d",&v);++lcounts[v]; }
vector<unsigned long long> spheres(c+1,0);
for(unsigned int r = 0;r <= c;++r) spheres[r] = (ucounts[r]*lcounts[r])%module;
vector< vector<unsigned long long> > dps(c+1,vector<unsigned long long>(c+1,0));
for(unsigned int i = 1;i <= c;++i)
{
dps[i][0] = (dps[i-1][0] + spheres[i])%module;
for(unsigned int k = 1;k < i;++k)
{
dps[i][k] = (dps[i-1][k-1]*spheres[i])%module;
dps[i][k] = (dps[i][k] + dps[i-1][k])%module;
}
}
for(unsigned int i = 1;i <= c;++i)
{
printf("%u",(unsigned int)(dps[c][i]));
if(i != c) printf(" ");
}
printf("\n");
return 0;
}
(1)问题描述:Given an equilateral triangle having length of each side as an integer N. Is it possible to alter one of its' sides such that the height drawn to the altered side has integral length and the altered side has an even integer length?
(2)要点:问题本质上是给定寻找一个在线算法,判断给定的一个数是否是勾股数的斜边。
(3)代码:
#include <stdio.h>
#include <vector>
#include <algorithm>
using std::vector;
class CSythagorasDecision
{
CSythagorasDecision(const CSythagorasDecision&);
CSythagorasDecision& operator=(const CSythagorasDecision&);
private:
vector<bool> _sythagoras;
public:
CSythagorasDecision() { ; }
~CSythagorasDecision() { ; }
public:
bool init(unsigned int maxn)
{
unsigned int maxp = 10;
for(;maxp*maxp <= maxn;++maxp);
vector<unsigned int> squares(maxp+1,0);
for(unsigned int i = 0;i <= maxp;++i) squares[i] = i*i;
_sythagoras.clear();_sythagoras.resize(maxn+1,false);
for(unsigned int i = 1;i <= maxp;++i)
{
for(unsigned int k = 1;k < i;++k)
{
unsigned int u = squares[i] + squares[k];
for(unsigned int v = u;v <= maxn;v += u) _sythagoras[v] = true;
}
}
return true;
}
public:
bool decide(unsigned int n)
{
return _sythagoras[n];
//bool ans = false;
//for(unsigned int id = 1;id*id <= n && !ans;++id)
//{
// if(0 != (n%id)) continue;
// ans |= _sythagoras[id];
// ans |= _sythagoras[n/id];
//}
//_sythagoras[n] = ans;
//return ans;
}
};
int main()
{
static const unsigned int maxn = 5000000;
CSythagorasDecision sd;sd.init(maxn);
unsigned int nCases = 0;scanf("%d",&nCases);
for(unsigned int iCases = 1;iCases <= nCases;++iCases)
{
unsigned int n = 0;scanf("%d",&n);
bool ans = sd.decide(n);
printf("%s\n",ans?"YES":"NO");
}
return 0;
}