锘??xml version="1.0" encoding="utf-8" standalone="yes"?> Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) House Man
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 446 Accepted Submission(s): 157
The man can travel for at most a certain horizontal distance D in a single jump. To make this as much fun as possible, the crazy man want to maximize the distance between the positions of the shortest house and the tallest house.
The crazy super man have an ability鈥攎ove houses. So he is going to move the houses subject to the following constraints:
1. All houses are to be moved along a one-dimensional path.
2. Houses must be moved at integer locations along the path, with no two houses at the same location.
3. Houses must be arranged so their moved ordering from left to right is the same as their ordering in the input. They must NOT be sorted by height, or reordered in any way. They must be kept in their stated order.
4. The super man can only jump so far, so every house must be moved close enough to the next taller house. Specifically, they must be no further than D apart on the ground (the difference in their heights doesn't matter).
Given N houses, in a specified order, each with a distinct integer height, help the super man figure out the maximum possible distance they can put between the shortest house and the tallest house, and be able to use the houses for training.
Each test case begins with a line containing two integers N (1 ≤ N ≤ 1000) and D (1 ≤ D ≤1000000). The next line contains N integer, giving the heights of the N houses, in the order that they should be moved. Within a test case, all heights will be unique.
#include <stdio.h>
#include <memory>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <map>
#include <cmath>
#include <set>
#include <queue>
#include <time.h>
#include <limits>
using namespace std;
#define typev int
#define N 1005
#define inf 0x7fffffff
#define E (N*5)
const double pi = acos(-1.0);
struct e{
int st, ed;
typev len;
void set(int _st, int _ed, typev _len){
st = _st;
ed = _ed;
len = _len;
}
}es[E];
struct node{
int h;
int i;
}nodes[N];
e* fir[N];
int n, en, d;
int st, ed;
int vis[N], t[N], que[N];
typev dist[N];
inline bool cmp(node n1, node n2){
return n1.h < n2.h;
}
inline bool relax(int st, int ed, int len){
if(dist[st] < inf && dist[ed] > dist[st] + len){
dist[ed] = dist[st] + len;
return true;
}
return false;
}
bool Bellman_Ford(int n, int st){ //榪斿洖true琛ㄧず鏈夎礋鐜?/span>
int i, j;
bool flag;
for(i = 0; i < n; i++) dist[i] = inf;
dist[st] = 0;
for(i = 0; i < n; i++){
flag = false;
for(j = 0; j < en; j++){
if(relax(es[j].st, es[j].ed, es[j].len)) flag = true;
}
if(!flag) break;
}
return flag;
}
int cnt = 0;
int main(){
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
#endif
int t, i, ans, u, v;
scanf("%d", &t);
while(t--){
scanf("%d%d", &n, &d);
for(i = 0; i < n; i++){
scanf("%d", &nodes[i].h);
nodes[i].i = i;
}
en = st = ed = 0;
sort(nodes, nodes+n, cmp);
st = nodes[0].i;
ed = nodes[n - 1].i;
if(st > ed) swap(st, ed);
for(i = 0; i < n; i++) fir[i] = NULL;
for(i = 1; i < n; i++){
es[en++].set(i, i-1, -1);
u = nodes[i].i;
v = nodes[i - 1].i;
if(u < v){
es[en++].set(u, v, d);
}else es[en++].set(v, u, d);
}
if(!Bellman_Ford(n, st)) ans = dist[ed];
else ans = -1;
printf("Case %d: %d\n", ++cnt, ans);
}
return 0;
}
]]>
#include <memory>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <map>
#include <cmath>
#include <set>
#include <queue>
#include <time.h>
#include <limits>
using namespace std;
#define N 100005
#define typev int
typev ar[N*2];
int ks[N], num[N], pos[N], id[N];
bool has[N];
int kn, n, qn, fr;
char qs[N];
int vMax;
int lowb(int t){ return t & (-t); }
void add(int i, typev v){
for(; i < vMax; ar[i] += v, i += lowb(i)) ;
}
typev sum(int i){
typev s = 0;
for(; i > 0; s += ar[i], i -= lowb(i));
return s;
}
int find_k(int k){ //find the kth number
int pos = 0, cnt = 0, i;
for(i = log((double)vMax) / log(2.0) + 1; i >= 0; i--){
pos += (1 << i);
if(pos >= vMax || cnt + ar[pos] >= k) pos -= (1 << i);
else cnt += ar[pos];
}
return pos + 1;
}
bool input(){
scanf("%d%d", &n, &qn);
int i;
char ops[10];
for(i = 0; i < qn; i++){
scanf("%s%d", ops, num+i);
ks[i] = num[i];
qs[i] = ops[0];
}
return true;
}
void init(){
kn = qn;
ks[kn++] = 1;
sort(ks, ks+kn);
int i, j;
j = 1;
for(i = 1; i < kn; i++){
if(ks[i] != ks[j-1]) ks[j++] = ks[i];
}
kn = j;
vMax = qn + kn + 1;
fill(ar, ar+vMax+1, 0);
fr = qn;
}
int cnt = 0;
void solve(){
int i, pMax, val, p, s, ans;
init();
ks[kn] = n+1;
for(i = 0; i < kn; i++){
has[i] = true;
val = ks[i+1]-ks[i];
pos[i] = qn + i + 1;
add(pos[i], val);
}
printf("Case %d:\n", ++cnt);
for(i = 0; i < qn; i++){
if(qs[i] == 'T'){
p = lower_bound(ks, ks+kn, num[i]) - ks;
has[p] = false;
val = -1;
add(pos[p], val);
val = 1;
id[fr] = num[i];
pos[p] = fr--;
add(pos[p], val);
}else if(qs[i] == 'R'){
p = find_k(num[i]);
if(p <= qn){
ans = id[p];
}else{
s = sum(p-1);
ans = num[i] - s + ks[p - qn - 1] - 1;
if(!has[p - qn - 1]) ans++;
}
printf("%d\n", ans);
}else{ //'Q'
p = lower_bound(ks, ks+kn, num[i]) - ks;
ans = sum(pos[p] - 1);
printf("%d\n", ans + 1);
}
}
}
int main(){
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
#endif
int t;
scanf("%d", &t);
while(t--){
input();
solve();
}
return 0;
}
]]>
棰樼洰鎻忚堪: N涓漢,絎琲涓漢瀹屾垚涓涓狝浠誨姟闇瑕佹椂闂碼i,瀹屾垚涓涓狟浠誨姟闇瑕佹椂闂碽i錛?br>鐜板湪鍙圶涓換鍔鍜孻涓換鍔錛屾眰瀹屾垚鎵鏈変換鍔℃墍闇瑕佺殑鏈鐭椂闂淬?br>瑙f硶錛氫簩鍒嗘椂闂磘錛宒p[i][j]琛ㄧず鍓峣涓漢瀹屾垚j涓狝浠誨姟鎵鑳藉瀹屾垚鐨凚浠誨姟鐨勬暟閲?br>*/
#include <stdio.h>
#include <memory>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <map>
#include <cmath>
#include <set>
#include <queue>
#include <time.h>
#include <limits>
using namespace std;
#define XY 205
#define N 55
#define inf 0x7fffffff
int a[N], b[N], dp[XY], n, X, Y;
bool check(int t){ //鍒ゆ柇鍦ㄦ椂闂磘鍐呮槸鍚﹀彲浠ュ畬鎴愭墍鏈夌殑浠誨姟
int i, j, k, kMax;
for(i = 1; i <= X; i++) dp[i] = -1;
dp[0] = 0;
for(i = 0; i < n; i++){
kMax = min(X, t / a[i]);
if(dp[X] >= Y) return true;
for(j = X; j >= 0; j--){
for(k = 0; k <= kMax && j - k>= 0; k++){ //絎琲涓漢瀹屾垚k浠?/span>
if(dp[j-k] < 0) continue;
dp[j] = max(dp[j], dp[j - k] + (t - k * a[i]) / b[i]);
}
}
}
return dp[X] >= Y;
}
int main(){
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
#endif
int t, i,ca, low, high, mid;
scanf("%d", &t);
for(ca = 1; ca <= t; ca++){
scanf("%d%d%d", &n, &X, &Y);
low = 0;
high = inf;
for(i = 0; i < n; i++){
scanf("%d%d", a+i, b+i);
high = min(high, a[i] * X + b[i] * Y);
}
while(low <= high){
mid = (low + high) >> 1;
if(check(mid)) high = mid - 1;
else low = mid + 1;
}
printf("Case %d: %d\n", ca, high + 1);
}
return 0;
}
]]>Sequence Adjustment
Total Submission(s): 463 Accepted Submission(s): 144
the least tries. You should calculate the number of the least tries
you needed and the number of different final sequences with the least tries.
In each case, the first line contain one integer N(1<=N<=10^6),
the second line contain N integers and each integer in the sequence is between [1,10^9].
There may be some blank lines between each case.
counted from one, x is the number of the least tries you need and y
is the number of different final sequences with the least tries.
/*
鏇磋灝界殑瑙i鎶ュ憡瑙侊細 http://hi.baidu.com/liwang112358/blog/item/3dac7e566f300f55d0090679.html
*/
#include <stdio.h>
#include <memory>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <map>
#include <cmath>
#include <set>
#include <queue>
#include <time.h>
#include <limits>
using namespace std;
#define N 1000005
#define ll long long
#define ABS(a) (a > 0 ? a : -a)
ll a[N], p[N];
int main(){
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
#endif
int t, n, ca;
ca = a[0] = p[0] = p[1] = 0;
scanf("%d", &t);
while(t--){
scanf("%d", &n);
int i, j;
ll sum, ans;
for(i = 1; i <= n; i++) scanf("%d", a+i);
for(i = 2, j = 1; i <= n; i++){
if(a[i] != a[j]){
a[++j] = a[i];
p[j] = a[j] - a[j - 1];
}
}
n = j;
ans = sum = 0;
for(i = 2; i <= n; i++){
if(p[i] * sum < 0) ans += min(ABS(sum), ABS(p[i]));
sum += p[i];
}
sum = ABS(sum);
ans += sum;
//printf("Case %d: %lld %lld\n", ++ca, ans, sum + 1);
printf("Case %d: %I64d %I64d\n", ++ca, ans, sum + 1);
}
return 0;
}
]]>
/*鏀寔鏁板瓧(鑰岄潪琛ㄨ揪寮?鍓嶉潰鏈夋璐熷彿 */
/*鏀寔嫻偣鏁扮殑+錛?錛?錛?,^ 榪愮畻 */
/*************************************/
#include <stdio.h>
#include <memory>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <map>
#include <cmath>
#include <set>
#include <queue>
#include <time.h>
#include <limits>
#include <stack>
using namespace std;
#define N 10005
#define eps 1e-9
#define vType double
struct node{
vType val;
char op;
node(vType _val=0, char _op = ' '):val(_val), op(_op){}
}nodes[N];
char str[N];
inline bool isDigit(char ch){
return ch >= '0' && ch <= '9';
}
map<char, int> ms;
void init(){
ms['('] = 0;
ms['-'] = ms['+'] = 1;
ms['*'] = ms['/'] = 2;
ms['^'] = 3;
}
vType calPoland(node* nodes, int k){
int i, j;
vType a, b;
stack<vType> s;
for(i = 0; i < k; i++){
if(nodes[i].op == ' '){
s.push(nodes[i].val);
}else{
a = s.top();
s.pop();
b = s.top();
s.pop();
switch(nodes[i].op){
case '+':
s.push(b + a);
break;
case '-':
s.push(b - a);
break;
case '*':
s.push(b * a);
break;
case '/':
if(fabs(a) < eps) throw true;
s.push(b / a);
break;
case '^':
s.push(pow(b, a));
break;
}
}
}
return s.top();
}
vType poland(char* str){
int i, k, sign;
bool inNum, hasDot; //inNum鏍囪褰撳墠鏄惁鍙互杈撳叆鏁板瓧, hasDot鏍囪鏄惁宸茬粡杈撳叆灝忔暟鐐?br> stack<char> oper;
for(i = k = 0, sign = 1, inNum = true; str[i]; i++){
if(isDigit(str[i]) || str[i] == '.'){
if(inNum){
vType val;
double w = 1;
if(str[i] == '.'){
hasDot = true;
val = 0;
}
else{
val = str[i] - '0';
hasDot = false;
}
i++;
while(isDigit(str[i]) || str[i] == '.'){
if(str[i] == '.'){
if(hasDot) throw true;
hasDot = true;
}else{
if(hasDot){
w *= 0.1;
val += (str[i] - '0') * w;
}
else val = val * 10 + str[i] - '0';
}
i++;
}
i--;
nodes[k++] = node(val * sign, ' ');
sign = 1;
inNum = false;
}else throw true;
}else{
switch(str[i]){
case '(':
oper.push(str[i]);
break;
case ')':
while(!oper.empty() && oper.top() != '('){
nodes[k++] = node(0, oper.top());
oper.pop();
}
if(oper.empty()) throw true; //娌℃湁涓?)'鍖歸厤鐨?('
oper.pop();
break;
case '+':
case '-':
case '*':
case '/':
case '^':
if(inNum){
if(str[i] != '+' && str[i] != '-') throw true;
while(str[i] == '+' || str[i] == '-'){
if(str[i] == '-') sign *= -1;
i++;
}
i--;
}else{
//while(!oper.empty() && ((str[i] != '^' && ms[str[i]] <= ms[oper.top()]) ||
// ((str[i] == '^' && ms[str[i]] < ms[oper.top()])))){ //濡傛灉^鏄彸緇撳悎鐨勮瘽灝辯敤榪欎釜
while(!oper.empty() && ms[str[i]] <= ms[oper.top()]){
nodes[k++] = node(0, oper.top());
oper.pop();
}
oper.push(str[i]);
inNum = true;
}
break;
}
}
}
while(!oper.empty()){
nodes[k++] = node(0, oper.top());
oper.pop();
}
return calPoland(nodes, k);
}
void Cal(char* str){
try{
vType ans = poland(str);
printf("%.8lf\n", ans);
}
catch(bool){
printf("The teacher is so lazy!\n");
}
}
int main(){
#ifndef ONLINE_JUDGE
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
#endif
init();
while(~scanf("%s", str)) Cal(str);
return 0;
}
]]>
1銆佸乏閫掑綊鍙互鐢ㄥ驚鐜潵瀹炵幇錛屽彸閫掑綊鍙互鐢ㄩ掑綊瀹炵幇(涔熷彲浠ョ敤寰幆瀹炵幇,涓嶈繃閫掑綊瀹炵幇璧鋒潵姣旇緝綆鍗?
2銆佸乏閫掑綊瀹炵幇宸︾粨鍚堣繍綆楋紝鍙抽掑綊瀹炵幇鍙崇粨鍚堣繍綆?br>hdu 3624(緗戝潃錛?http://acm.hdu.edu.cn/showproblem.php?pid=3624 ) 鐨勬枃娉?br>exp -> addTerm | exp addOp addTerm
addOp -> + | -
addTerm -> mulTerm | addTerm mulOp mulTerm
mulOp -> * | / | %
mulTerm -> eTerm ^ mulTerm | eTerm
eTerm ->sNum | (exp)
sNum -> num | -eTerm
*/
#include <stdio.h>
#include <memory>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <map>
#include <cmath>
#include <set>
#include <queue>
#include <time.h>
#include <limits>
using namespace std;
#define vType long long
#define MAXLEN 2005
const int vMax = 0x7fffffff;
const int vMin = ~vMax;
#define checkVal(val) (val >= vMin && val <= vMax)
char str[MAXLEN];
int pos, cnt; //pos涓哄綋鍓嶆壂鎻忎綅緗紝cnt涓哄凡緇忓垎閰嶇殑鑺傜偣鐨勬暟鐩?br>char ch; //ch涓哄綋鍓嶆壂鎻忕殑瀛楃
struct treeNode{
char op, flag;
vType val;
treeNode* ch[2];
void setFlag(){
if((ch[0] && !ch[0]->flag) || (ch[1] && !ch[1]->flag)){
flag = false;
}
}
}mem[MAXLEN*10];
treeNode* exp();
inline bool isDigit(char ch){
return ch >= '0' && ch <= '9';
}
inline treeNode* newNode(){
mem[cnt].flag = true;
mem[cnt].op = ' '; //op涓虹┖鏍艱〃紺轟笉闇瑕佽繍綆?br> mem[cnt].ch[0] = mem[cnt].ch[1] = NULL;
return &mem[cnt++];
}
treeNode* getSyntaxTree(){
treeNode* tree;
pos = cnt = 0;
int i, j;
i = j = 0;
ch = '\n';
do{ //蹇界暐絀烘牸
if(str[i] != ' ') str[j++] = str[i];
}while(str[i++]);
for(i = j = 0; str[i]; i++){ //鍔犺繖涓姝ユ槸涓轟簡澶勭悊2000涓?鐨勫彉鎬佹暟鎹?br> if(str[i] == '(') j++;
else if(str[i] == ')') j--;
}
if(j != 0){
tree = newNode();
tree->flag = false;
}else tree = exp();
return tree;
}
treeNode* eTerm(){
treeNode* t;
t = newNode();
ch = str[pos++];
if(ch == '('){
t = exp();
if(ch != ')') t->flag = false;
else ch = str[pos++];
}else if(isDigit(ch)){
vType a = ch - '0';
for(ch = str[pos++];isDigit(ch); ch = str[pos++]){
if(t->flag){
a = a * 10 + ch - '0';
}
if(!checkVal(a)){
t->flag = false;
}
}
t->val = a;
}else if(ch == '-'){
t->op = ch;
t->ch[0] = newNode();
t->ch[0]->val = 0;
t->ch[1] = eTerm();
t->setFlag();
}else t->flag = false;
return t;
}
treeNode* mulTerm(){
treeNode *t, *p, *r, *t0, *t1;;
r = t = eTerm();
p = NULL;
while(ch == '^'){ //寰幆瀹炵幇鍙抽掑綊鏂囨硶
t1 = newNode();
t1->op = ch;
t1->ch[0] = t;
t0 = eTerm();
t1->ch[1] = t0;
if(p){
p->ch[1] = t1;
}else r = t1;
p = t1;
t = t0;
}
return r;
}
treeNode* addTerm(){
treeNode *t, *p;
t = newNode();
t->ch[0] = mulTerm();
t->setFlag();
while(ch == '*' || ch == '/' || ch == '%'){
t->op = ch;
t->ch[1] = mulTerm();
t->setFlag();
p = t;
t = newNode();
t->ch[0] = p;
}
return t;
}
treeNode* exp(){
treeNode *t, *p;
t = newNode();
t->ch[0] = addTerm();
while(ch == '-' || ch == '+'){
t->op = ch;
t->ch[1] = addTerm();
p = t;
t = newNode();
t->ch[0] = p;
}
return t;
}
void cal(treeNode* root){
if(!root) return;
cal(root->ch[0]);
cal(root->ch[1]);
root->setFlag();
if(!root->flag) return;
if(root->op != ' '){
vType a = 0;
switch(root->op){
case '-':
a = root->ch[0]->val - root->ch[1]->val;
break;
case '+':
a = root->ch[0]->val + root->ch[1]->val;
break;
case '*':
a = root->ch[0]->val * root->ch[1]->val;
break;
case '/':
if(root->ch[1]->val == 0){
root->flag = false;
return;
}
a = root->ch[0]->val / root->ch[1]->val;
break;
case '%':
if(root->ch[1]->val == 0){
root->flag = 0;
return;
}
a = root->ch[0]->val % root->ch[1]->val;
break;
case '^':
if(root->ch[1]->val < 0 || (root->ch[0]->val == 0 && root->ch[1]->val == 0)){
root->flag = false;
return;
}
double b = pow((double)root->ch[0]->val, (double)root->ch[1]->val);
if(checkVal(b)) a = (vType)b;
else{
root->flag = false;
return;
}
break;
}
if(checkVal(a)){
root->val = a;
root->setFlag();
}else{
root->flag = false;
}
}else if(root->ch[0]){
root->flag = root->ch[0]->flag;
root->val = root->ch[0]->val;
}
}
int main(){
#ifndef ONLINE_JUDGE
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
#endif
int i, c;
treeNode* root;
scanf("%d", &c);
getchar();
for(i = 1; i <= c; i++){
gets(str);
root = getSyntaxTree();
printf("Case %d: ", i);
cal(root);
if(root->flag){
printf("%I64d\n", root->val);
}
else printf("ERROR!\n");
}
return 0;
}