A:需要注意的是sort函数排序时,当两个元素相等,排完序之后不一定还是按原先方式排列,所以在满足题目前两个条件
之后还要按他们序号排
/***********************************************
* Author: fisty
* Created Time: 2015/3/7 19:27:19
* File Name : 1001.cpp
*********************************************** */
#include <iostream>
#include <cstring>
#include <deque>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <algorithm>
using namespace std;
#define Debug(x) cout << #x << " " << x <<endl
#define Memset(x, a) memset(x, a, sizeof(x))
const int INF = 0x3f3f3f3f;
typedef long long LL;
typedef pair<int, int> P;
#define FOR(i, a, b) for(int i = a;i < b; i++)
#define MAX_N 105
struct node{
int id;
int first;
int second;
}a[MAX_N];
bool cmp(const node &e1,const node &e2){
int x = (e1.first - e1.second);
int y = (e2.first - e2.second);
return x > y || (x == y && e1.second < e2.second) || (x == y && e1.second == e2.second && e1.id < e2.id);
}
int main(){
//freopen("in.cpp", "r", stdin);
int n;
while(scanf("%d", &n) != EOF){
Memset(a, 0);
FOR(i, 0, n){
scanf("%d%d", &a[i].first, &a[i].second);
a[i].id = i;
}
sort(a, a + n, cmp);
for(int i = 0;i < n-1; i++){
printf("%d ", a[i].id);
}
printf("%d\n", a[n-1].id);
}
return 0;
}
B:set+快速输入。效率是不如手写哈希的。。
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <set>
#define LL long long
using namespace std;
const int N = 10000100;
set <LL> st;
int a[N];
int n, k;
inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int solve () {
st.clear ();
LL sum = 0;
st.insert(0);
for (int i = n, t = 1; i >= 1; i--, t ^= 1) {
if (t) sum += a[i]; else sum -= a[i];
if (t) {
if (st.find(sum - k) != st.end()) {
return 1;
}
}else{
if (st.find(k + sum) != st.end()) {
return 1;
}
}
st.insert (sum);
}
return 0;
}
int main () {
// freopen ("in.txt", "r", stdin);
int T, cas = 1;
cin >> T;
while (T--) {
n = read (); k = read ();
for (int i = 1; i <= n; i++) a[i] = read ();
printf ("Case #%d: ", cas++);
if (solve ()) printf ("Yes.\n");
else printf ("No.\n");
}
}
D.
/***********************************************
* Author: fisty
* Created Time: 2015/3/9 16:58:04
* File Name : 1004.cpp
*********************************************** */
#include <iostream>
#include <cstring>
#include <deque>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <algorithm>
using namespace std;
#define Debug(x) cout << #x << " " << x <<endl
#define Memset(x, a) memset(x, a, sizeof(x))
const int INF = 0x3f3f3f3f;
typedef long long LL;
typedef pair<int, int> P;
#define FOR(i, a, b) for(int i = a;i < b; i++)
#define MAX_N 50000
int n, m;
int dp[300][MAX_N];
int main() {
//freopen("in.cpp", "r", stdin);
cin.tie(0);
ios::sync_with_stdio(false);
int cnt = 1;
int t;
cin >> t;
while(t--){
cin >> n >> m;
cout << "Case #" << cnt++ << ": ";
Memset(dp, 0);
dp[0][0] = 1;
int _max = (sqrt(8 * n + 1) - 1) / 2;
for(int j = 1;j <= n; j++){
for(int i = 0;i <= min(_max, j); i++){
dp[i][j] = dp[i][j-i];
if(i) dp[i][j] = (dp[i][j] + dp[i-1][j-i]) % m;
}
}
int ans = 0;
for(int i = 0;i <= _max; i++){
ans = (ans + dp[i][n]) % m;
}
cout << ans << endl;
}
return 0;
}