Nettle最近在玩《艦これ》,因此Nettle收集了很多很多的船(这里我们假设Nettle氪了很多金,开了无数个船位)。去除掉重复的船之后,还剩下N(1≤N≤1,000,000)种不同的船。每一艘船有一个稀有值,任意两艘船的稀有值都不相同,稀有值越小的船越稀有,价值也就越高。
Nettle现在通过大建又造出了一艘船,他想知道这艘船是不是重复的。如果是重复的,那么这艘船在Nettle所有的船里面稀有值排多少位。
问题一
Nettle已经先把自己所有船按照稀有值从小到大排列好了(a[1..N]),我们要做的是看看新得到的船(假设稀有值为K)是否在这个序列中,且有对应的a[i]=K时,i为多少?
提示一:有序数组的二分查找
问题二
因为Nettle的船太多了,他不愿意去给所有船按照稀有值排序,而是直接告诉了我们每一艘船的稀有值。在这种情况下我们该如何解决这个问题呢?
提示二:非有序数组的二分查找
输入
第1行:2个整数N,K。N表示数组长度,K表示需要查找的数;
第2行:N个整数,表示a[1..N],保证不会出现重复的数,1≤a[i]≤2,000,000,000。
输出
第1行:一个整数t,表示K在数组中是第t小的数,若K不在数组中,输出-1。
样例输入
10 5180
2970 663 5480 4192 4949 1 1387 4428 5180 2761
样例输出
9
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int N, M;
cin >> N >> M;
vector<int> arr(N);
for (int i=0; i< N; i++)
cin >> arr[i];
sort(arr.begin(), arr.end());
int left = 0, right = N-1;
while (left<=right) {
int mid = left+(right-left)/2;
if (arr[mid] == M) {
cout << mid+1 << endl;
break;
}
else if (arr[mid] < M)
left = mid+1;
else
right = mid-1;
}
if (left>right) cout << -1 << endl;
return 0;
}
What is latest time you can make with 4 digits A, B, C and D?
For example if the 4 digits are 1, 0, 0, 0, you can make 4 times with them: 00:01, 00:10, 01:00, 10:00. The lastest time will be 10:00. Note a valid time is between 00:00 and 23:59.
输入
One line with 4 digits A, B, C and D, separated by a space. (0 <= A, B, C, D <= 9)
输出
Output the lastest time in the format “hh:mm”. If there is no valid time output NOT POSSIBLE.
样例输入
0 9 0 0
样例输出
09:00
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool compare(int i, int j) {
return i>j;
}
bool isValid(vector<int> vec) {
if (vec[0] >2) return false;
if (vec[0] == 2 && vec[1]>3) return false;
if (vec[2] > 5) return false;
return true;
}
int main() {
vector<int> input(4);
for (int i=0; i<4; i++)
cin >> input[i];
bool flag = false;
sort(input.begin(), input.end(), compare);
do {
if (isValid(input)) {
flag = true;
cout << input[0] << input[1] << ":" << input[2] << input[3] << endl;
break;
}
} while (prev_permutation(input.begin(), input.end()));
if (!flag) cout << "NOT POSSIBLE" << endl;
return 0;
}
描述#1268 九宫
小Hi最近在教邻居家的小朋友小学奥数,而最近正好讲述到了三阶幻方这个部分,三阶幻方指的是将1~9不重复的填入一个3*3的矩阵当中,使得每一行、每一列和每一条对角线的和都是相同的。
三阶幻方又被称作九宫格,在小学奥数里有一句非常有名的口诀:“二四为肩,六八为足,左三右七,戴九履一,五居其中”,通过这样的一句口诀就能够非常完美的构造出一个九宫格来。
有意思的是,所有的三阶幻方,都可以通过这样一个九宫格进行若干镜像和旋转操作之后得到。现在小Hi准备将一个三阶幻方(不一定是上图中的那个)中的一些数组抹掉,交给邻居家的小朋友来进行还原,并且希望她能够判断出究竟是不是只有一组解。
而你呢,也被小Hi交付了同样的任务,但是不同的是,你需要写一个程序~
输入
输入仅包含单组测试数据。
每组测试数据为一个3*3的矩阵,其中为0的部分表示被小Hi抹去的部分。
对于100%的数据,满足给出的矩阵至少能还原出一组可行的三阶幻方。
输出
如果仅能还原出一组可行的三阶幻方,则将其输出,否则输出“Too Many”(不包含引号)。
样例输入
0 7 2
0 5 0
0 3 0
样例输出
6 7 2
1 5 9
8 3 4
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int board[3][3];
int ans[3][3];
int record[9] = {0};
int cnt = 0;
bool flag;
bool isValid() {
for (int i=0; i<3; i++) {
if (board[i][0] + board[i][1] + board[i][2] != 15)
return false;
if (board[0][i] + board[1][i] + board[2][i] != 15)
return false;
}
if (board[0][0] + board[1][1] + board[2][2] != 15) {
return false;
}
if (board[2][0] + board[1][1] + board[0][2] != 15) {
return false;
}
return true;
}
void dfs(int i, int j) {
if (i==3 && isValid()) {
cnt++;
memcpy(ans, board, sizeof(board));
return;
}
else if (board[i][j]) {
if (j==2) dfs(i+1, 0);
else dfs(i, j+1);
}
else {
for (int k=0; k<9; k++) {
if (!record[k]) {
record[k] = 1;
board[i][j] = k+1;
if (j==2) dfs(i+1, 0);
else dfs(i, j+1);
record[k] = 0;
board[i][j] = 0;
}
}
}
}
int main() {
flag = false;
int a;
for (int i=0; i<3; i++)
for (int j=0; j<3; j++) {
cin >> a;
board[i][j] = a;
if (a!=0) record[a-1] = 1;
}
dfs(0, 0);
if (cnt == 1 )
for (int i=0; i<3; i++) {
for (int j=0; j<3; j++)
cout << ans[i][j] << " ";
cout << endl;
}
else
cout << "Too Many" << endl;
return 0;
}
Topcoder : TopCoder is indeed the world’s largest competitive software development community where developers from all over the world take part in.
CodeChef : CodeChef is a non-commercial organization operated by DirectI, an Indian software company based in Mumbai, India.
Google Code Jam : Google Code Jam is an annual programming competition sponsored and supported by Google itself.
Codeforces : Codeforces is an online programming platform where you can practice variety of problems and submit competitive ones and compete on problems submitted by other users.
ACM-ICPC : ACM – ICPC is one of the world’s largest programming contest conducted annually.
Leetcode(https://leetcode.com) : LeetCode Online Judge is a platform for preparing technical coding interviews and assessing your knowledge of data structures and algorithms.
Hihocoder : Learn and Practise, Compete and Make Friends, Stand out and Get hired. register. or login with. Hiho weekly.
SQL Backup
-- pgSQL
-- Get the table full description and columns
SELECT DISTINCT
a.attnum as num,
a.attname as name,
format_type(a.atttypid, a.atttypmod) as typ,
a.attnotnull as notnull,
com.description as comment,
coalesce(i.indisprimary,false) as primary_key,
def.adsrc as default
FROM pg_attribute a
JOIN pg_class pgc ON pgc.oid = a.attrelid
LEFT JOIN pg_index i ON
(pgc.oid = i.indrelid AND i.indkey[0] = a.attnum)
LEFT JOIN pg_description com on
(pgc.oid = com.objoid AND a.attnum = com.objsubid)
LEFT JOIN pg_attrdef def ON
(a.attrelid = def.adrelid AND a.attnum = def.adnum)
WHERE a.attnum > 0 AND pgc.oid = a.attrelid
AND pg_table_is_visible(pgc.oid)
AND NOT a.attisdropped
AND pgc.relname = 'bvd_etl_log' -- Your table name here
ORDER BY a.attnum;
-- TIME
select cus_prov_id, amount, yoy, mom, ((the_year||the_month||'01')::date + interval'1 month' - interval'1 day')::date as data_date from ebd_stgcustoms_stat_prov_e where the_year||the_month < '201512' and the_year||the_month >= '201212'
-- ORACLE
select * from ebd_signal_score_result where update_date = to_date('2016/11/26', 'YYYY/MM/DD')
-- INNER JOIN to get the full info as long as one is in one of the two tables
SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo
FROM Persons
INNER JOIN Orders
ON Persons.Id_P=Orders.Id_P
ORDER BY Persons.LastName
-- LEFT JOIN
SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo
FROM Persons
LEFT JOIN Orders
ON Persons.Id_P=Orders.Id_P
ORDER BY Persons.LastName
-- RIGHT JOIN
SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo
FROM Persons
RIGHT JOIN Orders
ON Persons.Id_P=Orders.Id_P
ORDER BY Persons.LastName
-- FULL JOIN
SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo
FROM Persons
FULL JOIN Orders
ON Persons.Id_P=Orders.Id_P
ORDER BY Persons.LastName
-- UNION
-- UNION SELECT RESULTS
-- UNION ALL ALLOW THE DUPLICATE
select distinct a.company_id from (select distinct company_id from ebd_stgcustoms_com_e) a
inner join (select distinct company_id from ebd_stgcustoms_com_i) b
on a.company_id = b.company_id limit 1000
-- Three table joins
select b.id, c.province, a.amount from table1 b join table2 c on b.id = c.id join table3 a on a.province = c.province